Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from PIL import Image
|
3 |
+
from torchvision import transforms
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# === SEMI-WEAKLY SUPERVISED MODELSP RETRAINED WITH 940 HASHTAGGED PUBLIC CONTENT ===
|
7 |
+
model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnet18_swsl')
|
8 |
+
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnet50_swsl')
|
9 |
+
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext50_32x4d_swsl')
|
10 |
+
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext101_32x4d_swsl')
|
11 |
+
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext101_32x8d_swsl')
|
12 |
+
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext101_32x16d_swsl')
|
13 |
+
# ================= SEMI-SUPERVISED MODELS PRETRAINED WITH YFCC100M ==================
|
14 |
+
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnet18_ssl')
|
15 |
+
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnet50_ssl')
|
16 |
+
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext50_32x4d_ssl')
|
17 |
+
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext101_32x4d_ssl')
|
18 |
+
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext101_32x8d_ssl')
|
19 |
+
# model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext101_32x16d_ssl')
|
20 |
+
model.eval()
|
21 |
+
# Download an example image from the pytorch website
|
22 |
+
import urllib
|
23 |
+
url, filename = ("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg")
|
24 |
+
try: urllib.URLopener().retrieve(url, filename)
|
25 |
+
except: urllib.request.urlretrieve(url, filename)
|
26 |
+
# sample execution (requires torchvision)
|
27 |
+
|
28 |
+
def inference(input_image):
|
29 |
+
input_image = Image.open(filename)
|
30 |
+
preprocess = transforms.Compose([
|
31 |
+
transforms.Resize(256),
|
32 |
+
transforms.CenterCrop(224),
|
33 |
+
transforms.ToTensor(),
|
34 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
35 |
+
])
|
36 |
+
input_tensor = preprocess(input_image)
|
37 |
+
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
38 |
+
|
39 |
+
# move the input and model to GPU for speed if available
|
40 |
+
if torch.cuda.is_available():
|
41 |
+
input_batch = input_batch.to('cuda')
|
42 |
+
model.to('cuda')
|
43 |
+
|
44 |
+
with torch.no_grad():
|
45 |
+
output = model(input_batch)
|
46 |
+
# The output has unnormalized scores. To get probabilities, you can run a softmax on it.
|
47 |
+
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
48 |
+
# Download ImageNet labels
|
49 |
+
!wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt
|
50 |
+
# Read the categories
|
51 |
+
with open("imagenet_classes.txt", "r") as f:
|
52 |
+
categories = [s.strip() for s in f.readlines()]
|
53 |
+
# Show top categories per image
|
54 |
+
top5_prob, top5_catid = torch.topk(probabilities, 5)
|
55 |
+
result = {}
|
56 |
+
for i in range(top5_prob.size(0)):
|
57 |
+
result[categories[top5_catid[i]]] = top5_prob[i].item()
|
58 |
+
return result
|
59 |
+
|
60 |
+
inputs = gr.inputs.Image(type='pil')
|
61 |
+
outputs = gr.outputs.Label(type="confidences",num_top_classes=5)
|
62 |
+
|
63 |
+
title = "SEMI-SUPERVISED AND SEMI-WEAKLY SUPERVISED IMAGENET MODELS"
|
64 |
+
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."
|
65 |
+
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>"
|
66 |
+
|
67 |
+
examples = [
|
68 |
+
['dog.jpg']
|
69 |
+
]
|
70 |
+
gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch()
|