Spaces:
Runtime error
Runtime error
import gradio as gr | |
import os | |
from PIL import Image | |
import torch | |
from torchvision import transforms | |
import torch | |
model = torch.hub.load('pytorch/vision:v0.10.0', 'wide_resnet50_2', pretrained=True) | |
model = torch.hub.load('pytorch/vision:v0.10.0', 'wide_resnet101_2', pretrained=True) | |
model.eval() | |
os.system("wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt") | |
torch.hub.download_url_to_file("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg") | |
def inference(input_image): | |
preprocess = transforms.Compose([ | |
transforms.Resize(256), | |
transforms.CenterCrop(224), | |
transforms.ToTensor(), | |
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), | |
]) | |
input_tensor = preprocess(input_image) | |
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model | |
if torch.cuda.is_available(): | |
input_batch = input_batch.to('cuda') | |
model.to('cuda') | |
with torch.no_grad(): | |
output = model(input_batch) | |
probabilities = torch.nn.functional.softmax(output[0], dim=0) | |
with open("imagenet_classes.txt", "r") as f: | |
categories = [s.strip() for s in f.readlines()] | |
top5_prob, top5_catid = torch.topk(probabilities, 5) | |
result = {} | |
for i in range(top5_prob.size(0)): | |
result[categories[top5_catid[i]]] = top5_prob[i].item() | |
return result | |
inputs = gr.inputs.Image(type='pil') | |
outputs = gr.outputs.Label(type="confidences",num_top_classes=5) | |
title = "WRN - Wide Residual Networks" | |
description = "ResNet blocks based architecture where depth is decreased and width of residual networks is increased." | |
article = "<p style='text-align: center'><a href='https://paperswithcode.com/paper/wide-residual-networks'>Wide Residual Networks on Papers With Code</a></p>" | |
examples = [ | |
['1.jpg'], | |
['2.jpg'], | |
['3.jpg'], | |
['4.jpg'], | |
['5.jpg'], | |
['20190210_171436.jpg'], | |
['20190211_215501.jpg'], | |
['20190220_220143.jpg'], | |
['20190223_181415.jpg'], | |
['20190404_193912.jpg'], | |
['20190413_021309.jpg'], | |
['20190413_115659.jpg'] | |
] | |
gr.Interface( | |
inference, | |
inputs, | |
outputs, | |
title=title, | |
description=description, | |
article=article, | |
examples=examples, | |
analytics_enabled=True | |
).launch() |