Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,151 @@
|
|
1 |
import os
|
|
|
2 |
import torch
|
|
|
3 |
from PIL import Image
|
|
|
4 |
from torchvision import transforms
|
|
|
5 |
import gradio as gr
|
|
|
6 |
#https://huggingface.co/spaces/yuhe6/final_project/blob/main/Net_Rotate9.pth
|
|
|
7 |
#os.system("wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt")
|
8 |
|
|
|
|
|
9 |
#model = torch.hub.load('huawei-noah/ghostnet', 'ghostnet_1x', pretrained=True)
|
|
|
10 |
#model = torch.jit.load('https://huggingface.co/spaces/yuhe6/final_project/blob/main/Net_Rotate9.pth').eval().to(device)
|
11 |
|
|
|
12 |
|
13 |
model = torch.jit.load('Net2_Blur_jit.pt', map_location = torch.device('cpu'))
|
|
|
14 |
model.eval()
|
15 |
|
|
|
16 |
|
17 |
#torch.hub.download_url_to_file('https://huggingface.co/spaces/yuhe6/final_project/blob/main/Net_Rotate9.pth', '/tmp/temporary_file')
|
|
|
18 |
#model = torch.hub.load('/tmp', 'temporary_file', pretrained=True)
|
19 |
|
|
|
|
|
20 |
#model.eval()
|
|
|
21 |
# Download an example image from the pytorch website
|
|
|
22 |
torch.hub.download_url_to_file("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg")
|
23 |
|
|
|
|
|
24 |
def inference(input_image):
|
|
|
25 |
preprocess = transforms.Compose([
|
26 |
-
|
|
|
|
|
27 |
#transforms.CenterCrop(224),
|
|
|
28 |
transforms.ToTensor(),
|
|
|
29 |
#transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
|
|
30 |
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
input_tensor = preprocess(input_image)
|
|
|
32 |
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
33 |
|
|
|
|
|
34 |
# move the input and model to GPU for speed if available
|
|
|
35 |
if torch.cuda.is_available():
|
|
|
36 |
input_batch = input_batch.to('cuda')
|
|
|
37 |
model.to('cuda')
|
38 |
|
|
|
|
|
39 |
with torch.no_grad():
|
40 |
-
|
|
|
|
|
|
|
|
|
41 |
# The output has unnormalized scores. To get probabilities, you can run a softmax on it.
|
|
|
42 |
probabilities = torch.nn.functional.softmax(output[0])
|
43 |
|
44 |
-
|
|
|
|
|
|
|
45 |
#with open("dog_cat.txt", "r") as f:
|
|
|
46 |
#categories = [s.strip() for s in f.readlines()]
|
|
|
47 |
#with open("dog_cat.txt", "r") as f:
|
|
|
48 |
categories = ["cat","dog"]
|
49 |
|
|
|
|
|
50 |
#categories = [s.strip() for s in f.readlines()]
|
|
|
51 |
# Show top categories per image
|
|
|
52 |
top1_prob, top1_catid = torch.topk(probabilities, 1)
|
|
|
53 |
result = {}
|
|
|
54 |
for i in range(top1_prob.size(0)):
|
|
|
55 |
result[categories[top1_catid[i]]] = top1_prob[i].item()
|
|
|
56 |
return result
|
57 |
|
|
|
|
|
58 |
inputs = gr.inputs.Image(type='pil')
|
|
|
59 |
outputs = gr.outputs.Label(type="confidences",num_top_classes=1)
|
60 |
|
|
|
|
|
61 |
title = "GHOSTNET"
|
|
|
62 |
description = "Gradio demo for GHOSTNET, Efficient networks by generating more features from cheap operations. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
|
|
|
63 |
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1911.11907'>GhostNet: More Features from Cheap Operations</a> | <a href='https://github.com/huawei-noah/CV-Backbones'>Github Repo</a></p>"
|
64 |
|
|
|
|
|
65 |
examples = [
|
|
|
66 |
['dog.jpg']
|
|
|
67 |
]
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
|
3 |
import torch
|
4 |
+
|
5 |
from PIL import Image
|
6 |
+
|
7 |
from torchvision import transforms
|
8 |
+
|
9 |
import gradio as gr
|
10 |
+
|
11 |
#https://huggingface.co/spaces/yuhe6/final_project/blob/main/Net_Rotate9.pth
|
12 |
+
|
13 |
#os.system("wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt")
|
14 |
|
15 |
+
|
16 |
+
|
17 |
#model = torch.hub.load('huawei-noah/ghostnet', 'ghostnet_1x', pretrained=True)
|
18 |
+
|
19 |
#model = torch.jit.load('https://huggingface.co/spaces/yuhe6/final_project/blob/main/Net_Rotate9.pth').eval().to(device)
|
20 |
|
21 |
+
|
22 |
|
23 |
model = torch.jit.load('Net2_Blur_jit.pt', map_location = torch.device('cpu'))
|
24 |
+
|
25 |
model.eval()
|
26 |
|
27 |
+
|
28 |
|
29 |
#torch.hub.download_url_to_file('https://huggingface.co/spaces/yuhe6/final_project/blob/main/Net_Rotate9.pth', '/tmp/temporary_file')
|
30 |
+
|
31 |
#model = torch.hub.load('/tmp', 'temporary_file', pretrained=True)
|
32 |
|
33 |
+
|
34 |
+
|
35 |
#model.eval()
|
36 |
+
|
37 |
# Download an example image from the pytorch website
|
38 |
+
|
39 |
torch.hub.download_url_to_file("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg")
|
40 |
|
41 |
+
|
42 |
+
|
43 |
def inference(input_image):
|
44 |
+
|
45 |
preprocess = transforms.Compose([
|
46 |
+
|
47 |
+
transforms.Resize(size = (256, 256)), # Fixed resize from transforms.Resize(256)
|
48 |
+
|
49 |
#transforms.CenterCrop(224),
|
50 |
+
|
51 |
transforms.ToTensor(),
|
52 |
+
|
53 |
#transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
54 |
+
|
55 |
])
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
# Used print statements to detect shapes between input tensor & batch
|
60 |
+
|
61 |
+
# e.g. input_tensor.shape
|
62 |
+
|
63 |
input_tensor = preprocess(input_image)
|
64 |
+
|
65 |
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
66 |
|
67 |
+
|
68 |
+
|
69 |
# move the input and model to GPU for speed if available
|
70 |
+
|
71 |
if torch.cuda.is_available():
|
72 |
+
|
73 |
input_batch = input_batch.to('cuda')
|
74 |
+
|
75 |
model.to('cuda')
|
76 |
|
77 |
+
|
78 |
+
|
79 |
with torch.no_grad():
|
80 |
+
|
81 |
+
output = model(input_batch) # model(input_tensor) # needed to have batch dimension
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
# The output has unnormalized scores. To get probabilities, you can run a softmax on it.
|
86 |
+
|
87 |
probabilities = torch.nn.functional.softmax(output[0])
|
88 |
|
89 |
+
|
90 |
+
|
91 |
+
# Read the categories
|
92 |
+
|
93 |
#with open("dog_cat.txt", "r") as f:
|
94 |
+
|
95 |
#categories = [s.strip() for s in f.readlines()]
|
96 |
+
|
97 |
#with open("dog_cat.txt", "r") as f:
|
98 |
+
|
99 |
categories = ["cat","dog"]
|
100 |
|
101 |
+
|
102 |
+
|
103 |
#categories = [s.strip() for s in f.readlines()]
|
104 |
+
|
105 |
# Show top categories per image
|
106 |
+
|
107 |
top1_prob, top1_catid = torch.topk(probabilities, 1)
|
108 |
+
|
109 |
result = {}
|
110 |
+
|
111 |
for i in range(top1_prob.size(0)):
|
112 |
+
|
113 |
result[categories[top1_catid[i]]] = top1_prob[i].item()
|
114 |
+
|
115 |
return result
|
116 |
|
117 |
+
|
118 |
+
|
119 |
inputs = gr.inputs.Image(type='pil')
|
120 |
+
|
121 |
outputs = gr.outputs.Label(type="confidences",num_top_classes=1)
|
122 |
|
123 |
+
|
124 |
+
|
125 |
title = "GHOSTNET"
|
126 |
+
|
127 |
description = "Gradio demo for GHOSTNET, Efficient networks by generating more features from cheap operations. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
|
128 |
+
|
129 |
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1911.11907'>GhostNet: More Features from Cheap Operations</a> | <a href='https://github.com/huawei-noah/CV-Backbones'>Github Repo</a></p>"
|
130 |
|
131 |
+
|
132 |
+
|
133 |
examples = [
|
134 |
+
|
135 |
['dog.jpg']
|
136 |
+
|
137 |
]
|
138 |
+
|
139 |
+
gr.Interface(
|
140 |
+
|
141 |
+
inference, inputs, outputs,
|
142 |
+
|
143 |
+
title = title, description = description,
|
144 |
+
|
145 |
+
article = article, examples = examples,
|
146 |
+
|
147 |
+
analytics_enabled = False).launch(
|
148 |
+
|
149 |
+
#debug = True # Enabled debug mode to see the stacktrace on Google Colab.
|
150 |
+
|
151 |
+
)
|