Upload app.py
Browse files
app.py
CHANGED
@@ -1,182 +1,181 @@
|
|
1 |
-
import torch
|
2 |
-
import gradio as gd
|
3 |
-
import torch.nn as nn
|
4 |
-
import torch.nn.functional as F
|
5 |
-
from torch.nn import init
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
out
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
out
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
nn.
|
34 |
-
nn.
|
35 |
-
nn.
|
36 |
-
nn.
|
37 |
-
nn.
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
self.
|
51 |
-
|
52 |
-
|
53 |
-
self.
|
54 |
-
self.
|
55 |
-
self.
|
56 |
-
self.
|
57 |
-
self.
|
58 |
-
self.
|
59 |
-
self.
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
nn.
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
out = self.
|
71 |
-
out = self.
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
self.
|
82 |
-
self.
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
Block(3, 16,
|
87 |
-
Block(3,
|
88 |
-
Block(
|
89 |
-
Block(5,
|
90 |
-
Block(5, 40, 240, 40, hswish(), SeModule(40), 1),
|
91 |
-
Block(5, 40,
|
92 |
-
Block(5,
|
93 |
-
Block(5, 48,
|
94 |
-
Block(5,
|
95 |
-
Block(5, 96, 576, 96, hswish(), SeModule(96), 1),
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
self.
|
101 |
-
self.
|
102 |
-
self.
|
103 |
-
self.
|
104 |
-
self.
|
105 |
-
self.
|
106 |
-
self.
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
init.constant_(m.
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
out = self.
|
125 |
-
out = self.
|
126 |
-
out =
|
127 |
-
out =
|
128 |
-
out =
|
129 |
-
out = self.
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
model
|
137 |
-
|
138 |
-
|
139 |
-
'
|
140 |
-
'
|
141 |
-
'
|
142 |
-
'
|
143 |
-
'
|
144 |
-
'
|
145 |
-
'
|
146 |
-
'
|
147 |
-
'
|
148 |
-
'
|
149 |
-
'
|
150 |
-
'
|
151 |
-
'
|
152 |
-
'
|
153 |
-
'
|
154 |
-
'
|
155 |
-
'
|
156 |
-
'
|
157 |
-
'
|
158 |
-
'
|
159 |
-
'
|
160 |
-
'
|
161 |
-
'
|
162 |
-
'
|
163 |
-
'
|
164 |
-
'
|
165 |
-
'
|
166 |
-
'
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
preds
|
174 |
-
|
175 |
-
|
176 |
-
""
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
if __name__ == "__main__":
|
182 |
demo.launch()
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gd
|
3 |
+
import torch.nn as nn
|
4 |
+
import torch.nn.functional as F
|
5 |
+
from torch.nn import init
|
6 |
+
import torchvision.transforms as transforms
|
7 |
+
|
8 |
+
'''MobileNetV3 in PyTorch.
|
9 |
+
|
10 |
+
See the paper "Inverted Residuals and Linear Bottlenecks:
|
11 |
+
Mobile Networks for Classification, Detection and Segmentation" for more details.
|
12 |
+
'''
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
class hswish(nn.Module):
|
17 |
+
def forward(self, x):
|
18 |
+
out = x * F.relu6(x + 3, inplace=True) / 6
|
19 |
+
return out
|
20 |
+
|
21 |
+
|
22 |
+
class hsigmoid(nn.Module):
|
23 |
+
def forward(self, x):
|
24 |
+
out = F.relu6(x + 3, inplace=True) / 6
|
25 |
+
return out
|
26 |
+
|
27 |
+
|
28 |
+
class SeModule(nn.Module):
|
29 |
+
def __init__(self, in_size, reduction=4):
|
30 |
+
super(SeModule, self).__init__()
|
31 |
+
self.se = nn.Sequential(
|
32 |
+
nn.AdaptiveAvgPool2d(1),
|
33 |
+
nn.Conv2d(in_size, in_size // reduction, kernel_size=1, stride=1, padding=0, bias=False),
|
34 |
+
nn.BatchNorm2d(in_size // reduction),
|
35 |
+
nn.ReLU(inplace=True),
|
36 |
+
nn.Conv2d(in_size // reduction, in_size, kernel_size=1, stride=1, padding=0, bias=False),
|
37 |
+
nn.BatchNorm2d(in_size),
|
38 |
+
hsigmoid()
|
39 |
+
)
|
40 |
+
|
41 |
+
def forward(self, x):
|
42 |
+
return x * self.se(x)
|
43 |
+
|
44 |
+
|
45 |
+
class Block(nn.Module):
|
46 |
+
'''expand + depthwise + pointwise'''
|
47 |
+
def __init__(self, kernel_size, in_size, expand_size, out_size, nolinear, semodule, stride):
|
48 |
+
super(Block, self).__init__()
|
49 |
+
self.stride = stride
|
50 |
+
self.se = semodule
|
51 |
+
|
52 |
+
self.conv1 = nn.Conv2d(in_size, expand_size, kernel_size=1, stride=1, padding=0, bias=False)
|
53 |
+
self.bn1 = nn.BatchNorm2d(expand_size)
|
54 |
+
self.nolinear1 = nolinear
|
55 |
+
self.conv2 = nn.Conv2d(expand_size, expand_size, kernel_size=kernel_size, stride=stride, padding=kernel_size//2, groups=expand_size, bias=False)
|
56 |
+
self.bn2 = nn.BatchNorm2d(expand_size)
|
57 |
+
self.nolinear2 = nolinear
|
58 |
+
self.conv3 = nn.Conv2d(expand_size, out_size, kernel_size=1, stride=1, padding=0, bias=False)
|
59 |
+
self.bn3 = nn.BatchNorm2d(out_size)
|
60 |
+
|
61 |
+
self.shortcut = nn.Sequential()
|
62 |
+
if stride == 1 and in_size != out_size:
|
63 |
+
self.shortcut = nn.Sequential(
|
64 |
+
nn.Conv2d(in_size, out_size, kernel_size=1, stride=1, padding=0, bias=False),
|
65 |
+
nn.BatchNorm2d(out_size),
|
66 |
+
)
|
67 |
+
|
68 |
+
def forward(self, x):
|
69 |
+
out = self.nolinear1(self.bn1(self.conv1(x)))
|
70 |
+
out = self.nolinear2(self.bn2(self.conv2(out)))
|
71 |
+
out = self.bn3(self.conv3(out))
|
72 |
+
if self.se != None:
|
73 |
+
out = self.se(out)
|
74 |
+
out = out + self.shortcut(x) if self.stride==1 else out
|
75 |
+
return out
|
76 |
+
|
77 |
+
class MobileNetV3_Small(nn.Module):
|
78 |
+
def __init__(self, num_classes= 30):
|
79 |
+
super(MobileNetV3_Small, self).__init__()
|
80 |
+
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1, bias=False)
|
81 |
+
self.bn1 = nn.BatchNorm2d(16)
|
82 |
+
self.hs1 = hswish()
|
83 |
+
|
84 |
+
self.bneck = nn.Sequential(
|
85 |
+
Block(3, 16, 16, 16, nn.ReLU(inplace=True), SeModule(16), 2),
|
86 |
+
Block(3, 16, 72, 24, nn.ReLU(inplace=True), None, 2),
|
87 |
+
Block(3, 24, 88, 24, nn.ReLU(inplace=True), None, 1),
|
88 |
+
Block(5, 24, 96, 40, hswish(), SeModule(40), 2),
|
89 |
+
Block(5, 40, 240, 40, hswish(), SeModule(40), 1),
|
90 |
+
Block(5, 40, 240, 40, hswish(), SeModule(40), 1),
|
91 |
+
Block(5, 40, 120, 48, hswish(), SeModule(48), 1),
|
92 |
+
Block(5, 48, 144, 48, hswish(), SeModule(48), 1),
|
93 |
+
Block(5, 48, 288, 96, hswish(), SeModule(96), 2),
|
94 |
+
Block(5, 96, 576, 96, hswish(), SeModule(96), 1),
|
95 |
+
Block(5, 96, 576, 96, hswish(), SeModule(96), 1),
|
96 |
+
)
|
97 |
+
|
98 |
+
|
99 |
+
self.conv2 = nn.Conv2d(96, 576, kernel_size=1, stride=1, padding=0, bias=False)
|
100 |
+
self.bn2 = nn.BatchNorm2d(576)
|
101 |
+
self.hs2 = hswish()
|
102 |
+
self.linear3 = nn.Linear(576, 1280)
|
103 |
+
self.bn3 = nn.BatchNorm1d(1280)
|
104 |
+
self.hs3 = hswish()
|
105 |
+
self.linear4 = nn.Linear(1280, num_classes)
|
106 |
+
self.init_params()
|
107 |
+
|
108 |
+
def init_params(self):
|
109 |
+
for m in self.modules():
|
110 |
+
if isinstance(m, nn.Conv2d):
|
111 |
+
init.kaiming_normal_(m.weight, mode='fan_out')
|
112 |
+
if m.bias is not None:
|
113 |
+
init.constant_(m.bias, 0)
|
114 |
+
elif isinstance(m, nn.BatchNorm2d):
|
115 |
+
init.constant_(m.weight, 1)
|
116 |
+
init.constant_(m.bias, 0)
|
117 |
+
elif isinstance(m, nn.Linear):
|
118 |
+
init.normal_(m.weight, std=0.001)
|
119 |
+
if m.bias is not None:
|
120 |
+
init.constant_(m.bias, 0)
|
121 |
+
|
122 |
+
def forward(self, x):
|
123 |
+
out = self.hs1(self.bn1(self.conv1(x)))
|
124 |
+
out = self.bneck(out)
|
125 |
+
out = self.hs2(self.bn2(self.conv2(out)))
|
126 |
+
out = F.avg_pool2d(out, 7)
|
127 |
+
out = out.view(out.size(0), -1)
|
128 |
+
out = self.hs3(self.bn3(self.linear3(out)))
|
129 |
+
out = self.linear4(out)
|
130 |
+
return out
|
131 |
+
|
132 |
+
|
133 |
+
|
134 |
+
"""creating modelinstance"""
|
135 |
+
model = MobileNetV3_Small().to("cpu")
|
136 |
+
model.load_state_dict( torch.load("MobileNet3_small_full.pth"))
|
137 |
+
classes = ['antelope',
|
138 |
+
'buffalo',
|
139 |
+
'chimpanzee',
|
140 |
+
'cow',
|
141 |
+
'deer',
|
142 |
+
'dolphin',
|
143 |
+
'elephant',
|
144 |
+
'fox',
|
145 |
+
'giant+panda',
|
146 |
+
'giraffe',
|
147 |
+
'gorilla',
|
148 |
+
'grizzly+bear',
|
149 |
+
'hamster',
|
150 |
+
'hippopotamus',
|
151 |
+
'horse',
|
152 |
+
'humpback+whale',
|
153 |
+
'leopard',
|
154 |
+
'lion',
|
155 |
+
'moose',
|
156 |
+
'otter',
|
157 |
+
'ox',
|
158 |
+
'pig',
|
159 |
+
'polar+bear',
|
160 |
+
'rabbit',
|
161 |
+
'rhinoceros',
|
162 |
+
'seal',
|
163 |
+
'sheep',
|
164 |
+
'squirrel',
|
165 |
+
'tiger',
|
166 |
+
'zebra']
|
167 |
+
|
168 |
+
def predicts(img):
|
169 |
+
model.eval()
|
170 |
+
with torch.inference_mode:
|
171 |
+
logits = model(img.unsqueez(dim=0))
|
172 |
+
preds = logits.argmax(dim=1)
|
173 |
+
return classes[preds]
|
174 |
+
|
175 |
+
"""gradio inteface"""
|
176 |
+
demo = gd.Interface(predicts ,gd.Image("Image", width=244, height=244, image_mode="RGB", ))
|
177 |
+
|
178 |
+
|
179 |
+
"""launch interface"""
|
180 |
+
if __name__ == "__main__":
|
|
|
181 |
demo.launch()
|