Spaces:
Runtime error
Runtime error
File size: 1,566 Bytes
75fbcd5 |
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 |
import os
import copy
import torch
import gradio
import gradio as gr
from PIL import Image
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
os.system("wget https://www.dropbox.com/s/grcragozd4x79zc/model_ok.pth")
model = torch.load("./model_ok.pth", map_location=device)
# img = Image.open(path).convert('RGB')
from torchvision import transforms
transforms2 = transforms.Compose([
transforms.Resize(256),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
# img = transforms(img)
# img = img.unsqueeze(0)
model.eval()
labels = ['aunts','bees']
# with torch.no_grad():
# # preds =
# preds = model(img)
# score, indices = torch.max(preds, 1)
def recognize_digit(image):
image = transforms2(image)
image = image.unsqueeze(0)
# image = image.unsqueeze(0)
# image = image.reshape(1, -1)
# with torch.no_grad():
# preds =
# img = image.reshape((-1, 3, 256, 256))
preds = model(image).flatten()
# prediction = model.predict(image).tolist()[0]
# score, indices = torch.max(preds, 1)
# return {str(indices.item())}
return {labels[i]: float(preds[i]) for i in range(2)}
im = gradio.inputs.Image(
shape=(256, 256), image_mode="RGB", type="pil")
iface = gr.Interface(
recognize_digit,
im,
gradio.outputs.Label(num_top_classes=3),
live=True,
interpretation="default",
examples=[["images/cheetah1.jpg"], ["images/lion.jpg"]],
capture_session=True,
)
iface.test_launch()
iface.launch() |