Spaces:
Runtime error
Runtime error
File size: 2,399 Bytes
839facb 5b7bd21 7d5de97 839facb d54327b 839facb 1d1e250 839facb 81909d9 981c905 81909d9 839facb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
import cv2, torch
import gradio as gr
import numpy as np
from PIL import Image
import torch.nn as nn
import torchvision.models as models
from torchvision import transforms as T
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
new_model = models.resnet18()
num_ftrs = new_model.fc.in_features
new_model.fc = nn.Linear(num_ftrs, 75)
checkpoint = torch.load('model_best_checkpoint.pth.tar', map_location=torch.device('cpu'))
new_model.load_state_dict(checkpoint['model'])
new_model.to(device)
we_are = ['INDRA SWALLOW',
'MALACHITE',
'COMMON BANDED AWL',
'DANAID EGGFLY',
'EASTERN PINE ELFIN',
'YELLOW SWALLOW TAIL',
'WOOD SATYR',
'ULYSES',
'MESTRA',
'MANGROVE SKIPPER',
'BECKERS WHITE',
'CRECENT',
'RED SPOTTED PURPLE',
'SOOTYWING',
'BLACK HAIRSTREAK',
'STRAITED QUEEN',
'ELBOWED PIERROT',
'ORANGE OAKLEAF',
'CHESTNUT',
'POPINJAY',
'COMMON WOOD-NYMPH',
'BROWN SIPROETA',
'QUESTION MARK',
'ADONIS',
'CLOUDED SULPHUR',
'TWO BARRED FLASHER',
'GOLD BANDED',
'BANDED ORANGE HELICONIAN',
'PURPLISH COPPER',
'VICEROY',
'RED CRACKER',
'SILVER SPOT SKIPPER',
'ZEBRA LONG WING',
'ORCHARD SWALLOW',
'RED POSTMAN',
'SOUTHERN DOGFACE',
'SCARCE SWALLOW',
'EASTERN COMA',
'CAIRNS BIRDWING',
'GREEN CELLED CATTLEHEART',
'METALMARK',
'LARGE MARBLE',
'AMERICAN SNOOT',
'COPPER TAIL',
'AN 88',
'AFRICAN GIANT SWALLOWTAIL',
'PAPER KITE',
'EASTERN DAPPLE WHITE',
'PEACOCK',
'ATALA',
'JULIA',
'RED ADMIRAL',
'GREAT JAY',
'GREAT EGGFLY',
'GREY HAIRSTREAK',
'PIPEVINE SWALLOW',
'PURPLE HAIRSTREAK',
'ORANGE TIP',
'BLUE SPOTTED CROW',
'TROPICAL LEAFWING',
'CLEOPATRA',
'APPOLLO',
'IPHICLUS SISTER',
'CABBAGE WHITE',
'BANDED PEACOCK',
'MONARCH',
'CRIMSON PATCH',
'BLUE MORPHO',
'MOURNING CLOAK',
'SLEEPY ORANGE',
'CLODIUS PARNASSIAN',
'MILBERTS TORTOISESHELL',
'PINE WHITE',
'CHECQUERED SKIPPER',
'PAINTED LADY']
def classify(image_):
model = new_model.eval()
image = Image.open(image_)
image = image_transforms(image).float().to(device)
image = image.unsqueeze(0)
output = model(image)
_, predicted = torch.max(output, 1)
return we_are[predicted]
label = gr.outputs.Label(num_top_classes=75)
gr.Interface(fn=classify, inputs='image', outputs=label,interpretation='default', title = 'Butterfly Classification detection ', description = 'It will classify 75 different species ').launch()
|