akhaliq HF staff commited on
Commit
4b52ff7
1 Parent(s): e094a06

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from PIL import Image
3
+ from torchvision import transforms
4
+ import gradio as gr
5
+ model = torch.hub.load('facebookresearch/WSL-Images', 'resnext101_32x8d_wsl')
6
+ # or
7
+ # model = torch.hub.load('facebookresearch/WSL-Images', 'resnext101_32x16d_wsl')
8
+ # or
9
+ # model = torch.hub.load('facebookresearch/WSL-Images', 'resnext101_32x32d_wsl')
10
+ # or
11
+ #model = torch.hub.load('facebookresearch/WSL-Images', 'resnext101_32x48d_wsl')
12
+ model.eval()
13
+ # Download an example image from the pytorch website
14
+ import urllib
15
+ url, filename = ("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg")
16
+ try: urllib.URLopener().retrieve(url, filename)
17
+ except: urllib.request.urlretrieve(url, filename)
18
+
19
+ def inference(input_image):
20
+ preprocess = transforms.Compose([
21
+ transforms.Resize(256),
22
+ transforms.CenterCrop(224),
23
+ transforms.ToTensor(),
24
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
25
+ ])
26
+ input_tensor = preprocess(input_image)
27
+ input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
28
+
29
+ # move the input and model to GPU for speed if available
30
+ if torch.cuda.is_available():
31
+ input_batch = input_batch.to('cuda')
32
+ model.to('cuda')
33
+
34
+ with torch.no_grad():
35
+ output = model(input_batch)
36
+ # The output has unnormalized scores. To get probabilities, you can run a softmax on it.
37
+ probabilities = torch.nn.functional.softmax(output[0], dim=0)
38
+ # Download ImageNet labels
39
+ !wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt
40
+ # Read the categories
41
+ with open("imagenet_classes.txt", "r") as f:
42
+ categories = [s.strip() for s in f.readlines()]
43
+ # Show top categories per image
44
+ top5_prob, top5_catid = torch.topk(probabilities, 5)
45
+ result = {}
46
+ for i in range(top5_prob.size(0)):
47
+ result[categories[top5_catid[i]]] = top5_prob[i].item()
48
+ return result
49
+
50
+ inputs = gr.inputs.Image(type='pil')
51
+ outputs = gr.outputs.Label(type="confidences",num_top_classes=5)
52
+
53
+ title = "RESNEXT WSL"
54
+ description = "Gradio demo for RESNEXT WSL, ResNext models trained with billion scale weakly-supervised data. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
55
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1805.00932'>Exploring the Limits of Weakly Supervised Pretraining</a> | <a href='https://github.com/facebookresearch/WSL-Images/blob/master/hubconf.py'>Github Repo</a></p>"
56
+
57
+ examples = [
58
+ ['dog.jpg']
59
+ ]
60
+ gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch()