Spaces:
Build error
Build error
import os | |
import torch | |
from PIL import Image | |
from torchvision import transforms | |
import gradio as gr | |
# === SEMI-WEAKLY SUPERVISED MODELSP RETRAINED WITH 940 HASHTAGGED PUBLIC CONTENT === | |
model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnet18_swsl') | |
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnet50_swsl') | |
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext50_32x4d_swsl') | |
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext101_32x4d_swsl') | |
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext101_32x8d_swsl') | |
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext101_32x16d_swsl') | |
# ================= SEMI-SUPERVISED MODELS PRETRAINED WITH YFCC100M ================== | |
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnet18_ssl') | |
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnet50_ssl') | |
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext50_32x4d_ssl') | |
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext101_32x4d_ssl') | |
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext101_32x8d_ssl') | |
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext101_32x16d_ssl') | |
model.eval() | |
# Download an example image from the pytorch website | |
import urllib | |
url, filename = ("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg") | |
try: urllib.URLopener().retrieve(url, filename) | |
except: urllib.request.urlretrieve(url, filename) | |
# sample execution (requires torchvision) | |
def inference(input_image): | |
input_image = Image.open(filename) | |
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 | |
# move the input and model to GPU for speed if available | |
if torch.cuda.is_available(): | |
input_batch = input_batch.to('cuda') | |
model.to('cuda') | |
with torch.no_grad(): | |
output = model(input_batch) | |
# The output has unnormalized scores. To get probabilities, you can run a softmax on it. | |
probabilities = torch.nn.functional.softmax(output[0], dim=0) | |
# Download ImageNet labels | |
os.system("wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt") | |
# Read the categories | |
with open("imagenet_classes.txt", "r") as f: | |
categories = [s.strip() for s in f.readlines()] | |
# Show top categories per image | |
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 = "SEMI-SUPERVISED AND SEMI-WEAKLY SUPERVISED IMAGENET MODELS" | |
description = "Gradio demo for SEMI-SUPERVISED AND SEMI-WEAKLY SUPERVISED IMAGENET MODELS, ResNet and ResNext models introduced in the 'Billion scale semi-supervised learning for image classification' paper. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below." | |
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1905.00546'>Billion-scale semi-supervised learning for image classification</a> | <a href='https://github.com/facebookresearch/semi-supervised-ImageNet1K-models/blob/master/hubconf.py'>Github Repo</a></p>" | |
examples = [ | |
['dog.jpg'] | |
] | |
gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch() |