akhaliq HF staff commited on
Commit
95c84f5
1 Parent(s): 2d64c09

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from PIL import Image
3
+ from torchvision import transforms
4
+ import gradio as gr
5
+ import os
6
+ # list of models: 'mealv1_resnest50', 'mealv2_resnest50', 'mealv2_resnest50_cutmix', 'mealv2_resnest50_380x380', 'mealv2_mobilenetv3_small_075', 'mealv2_mobilenetv3_small_100', 'mealv2_mobilenet_v3_large_100', 'mealv2_efficientnet_b0'
7
+ # load pretrained models, using "mealv2_resnest50_cutmix" as an example
8
+ model = torch.hub.load('szq0214/MEAL-V2','meal_v2', 'mealv2_resnest50_cutmix', pretrained=True)
9
+ model.eval()
10
+ # Download an example image from the pytorch website
11
+ torch.hub.download_url_to_file("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg")
12
+
13
+ # Download ImageNet labels
14
+ os.system("wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt")
15
+
16
+ def inference(input_image):
17
+ preprocess = transforms.Compose([
18
+ transforms.Resize(256),
19
+ transforms.CenterCrop(224),
20
+ transforms.ToTensor(),
21
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
22
+ ])
23
+ input_tensor = preprocess(input_image)
24
+ input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
25
+
26
+ # move the input and model to GPU for speed if available
27
+ if torch.cuda.is_available():
28
+ input_batch = input_batch.to('cuda')
29
+ model.to('cuda')
30
+
31
+ with torch.no_grad():
32
+ output = model(input_batch)
33
+ # The output has unnormalized scores. To get probabilities, you can run a softmax on it.
34
+ probabilities = torch.nn.functional.softmax(output[0], dim=0)
35
+
36
+ # Read the categories
37
+ with open("imagenet_classes.txt", "r") as f:
38
+ categories = [s.strip() for s in f.readlines()]
39
+ # Show top categories per image
40
+ top5_prob, top5_catid = torch.topk(probabilities, 5)
41
+ result = {}
42
+ for i in range(top5_prob.size(0)):
43
+ result[categories[top5_catid[i]]] = top5_prob[i].item()
44
+ return result
45
+
46
+ inputs = gr.inputs.Image(type='pil')
47
+ outputs = gr.outputs.Label(type="confidences",num_top_classes=5)
48
+
49
+ title = "MEAL_V2"
50
+ description = "Gradio demo for MEAL_V2, Boosting Tiny and Efficient Models using Knowledge Distillation. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
51
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2009.08453'>MEAL V2: Boosting Vanilla ResNet-50 to 80%+ Top-1 Accuracy on ImageNet without Tricks</a> | <a href='https://github.com/szq0214/MEAL-V2'>Github Repo</a></p>"
52
+
53
+ examples = [
54
+ ['dog.jpg']
55
+ ]
56
+ gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch()