coderx7 commited on
Commit
96f406b
1 Parent(s): 069287b

initial commit adding 5m_m1 variant demo

Browse files
Files changed (2) hide show
  1. app.py +67 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from PIL import Image
3
+ from torchvision import transforms
4
+ import gradio as gr
5
+ import os
6
+
7
+ os.system("wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt")
8
+
9
+
10
+ import torch
11
+ model = torch.hub.load("coderx7/simplenet_pytorch","simplenetv1_5m_m1", pretrained=True)
12
+ # or any of these variants
13
+ # model = torch.hub.load("coderx7/simplenet_pytorch","simplenetv1_5m_m2", pretrained=True)
14
+ # model = torch.hub.load("coderx7/simplenet_pytorch","simplenetv1_9m_m1", pretrained=True)
15
+ # model = torch.hub.load("coderx7/simplenet_pytorch","simplenetv1_9m_m2", pretrained=True)
16
+ # model = torch.hub.load("coderx7/simplenet_pytorch","simplenetv1_small_m1_05", pretrained=True)
17
+ # model = torch.hub.load("coderx7/simplenet_pytorch","simplenetv1_small_m2_05", pretrained=True)
18
+ # model = torch.hub.load("coderx7/simplenet_pytorch","simplenetv1_small_m1_075", pretrained=True)
19
+ # model = torch.hub.load("coderx7/simplenet_pytorch","simplenetv1_small_m2_075", pretrained=True)
20
+ model.eval()
21
+
22
+ # Download an example image from the pytorch website
23
+ torch.hub.download_url_to_file("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg")
24
+
25
+ def inference(input_image):
26
+ preprocess = transforms.Compose([
27
+ transforms.Resize(256),
28
+ transforms.CenterCrop(224),
29
+ transforms.ToTensor(),
30
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
31
+ ])
32
+ input_tensor = preprocess(input_image)
33
+ input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
34
+
35
+ # move the input and model to GPU for speed if available
36
+ if torch.cuda.is_available():
37
+ input_batch = input_batch.to('cuda')
38
+ model.to('cuda')
39
+
40
+ with torch.no_grad():
41
+ output = model(input_batch)
42
+ # The output has unnormalized scores. To get probabilities, you can run a softmax on it.
43
+ probabilities = torch.nn.functional.softmax(output[0], dim=0)
44
+
45
+ # Read the categories
46
+ with open("imagenet_classes.txt", "r") as f:
47
+ categories = [s.strip() for s in f.readlines()]
48
+ # Show top categories per image
49
+ top5_prob, top5_catid = torch.topk(probabilities, 5)
50
+ result = {}
51
+ for i in range(top5_prob.size(0)):
52
+ result[categories[top5_catid[i]]] = top5_prob[i].item()
53
+ return result
54
+
55
+ inputs = gr.inputs.Image(type='pil')
56
+ outputs = gr.outputs.Label(type="confidences",num_top_classes=5)
57
+
58
+ title = "SimpleNet"
59
+ description = "Gradio demo for SimpleNet network pre-trained on ImageNet. This uses the simplenet_5m_m1 variant. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
60
+
61
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1608.06037' target='_blank'>Lets Keep it simple, Using simple architectures to outperform deeper and more complex architectures</a> | <a href='https://github.com/Coderx7/SimpleNet_Pytorch/blob/master/imagenet/simplenet.py' target='_blank'>Github Repo</a></p>"
62
+
63
+ examples = [
64
+ ['dog.jpg']
65
+ ]
66
+
67
+ gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ torchvision
3
+ Pillow