Chujinze commited on
Commit
8bd9e6d
1 Parent(s): 37ef462

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +332 -0
app.py ADDED
@@ -0,0 +1,332 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch.nn as nn
3
+ import math
4
+ import torch.utils.model_zoo as model_zoo
5
+ import torch
6
+ import torch.nn.functional as F
7
+
8
+ __all__ = ['Res2Net', 'res2net50_v1b', 'res2net101_v1b']
9
+
10
+ model_urls = {
11
+ 'res2net50_v1b_26w_4s': 'https://shanghuagao.oss-cn-beijing.aliyuncs.com/res2net/res2net50_v1b_26w_4s-3cf99910.pth',
12
+ 'res2net101_v1b_26w_4s': 'https://shanghuagao.oss-cn-beijing.aliyuncs.com/res2net/res2net101_v1b_26w_4s-0812c246.pth',
13
+ }
14
+
15
+
16
+ class Bottle2neck(nn.Module):
17
+ expansion = 4
18
+
19
+ def __init__(self, inplanes, planes, stride=1, downsample=None, baseWidth=26, scale=4, stype='normal'):
20
+ """ Constructor
21
+ Args:
22
+ inplanes: input channel dimensionality
23
+ planes: output channel dimensionality
24
+ stride: conv stride. Replaces pooling layer.
25
+ downsample: None when stride = 1
26
+ baseWidth: basic width of conv3x3
27
+ scale: number of scale.
28
+ type: 'normal': normal set. 'stage': first block of a new stage.
29
+ """
30
+ super(Bottle2neck, self).__init__()
31
+
32
+ width = int(math.floor(planes * (baseWidth / 64.0)))
33
+ self.conv1 = nn.Conv2d(inplanes, width * scale, kernel_size=1, bias=False)
34
+ self.bn1 = nn.BatchNorm2d(width * scale)
35
+
36
+ if scale == 1:
37
+ self.nums = 1
38
+ else:
39
+ self.nums = scale - 1
40
+ if stype == 'stage':
41
+ self.pool = nn.AvgPool2d(kernel_size=3, stride=stride, padding=1)
42
+ convs = []
43
+ bns = []
44
+ for i in range(self.nums):
45
+ convs.append(nn.Conv2d(width, width, kernel_size=3, stride=stride, padding=1, bias=False))
46
+ bns.append(nn.BatchNorm2d(width))
47
+ self.convs = nn.ModuleList(convs)
48
+ self.bns = nn.ModuleList(bns)
49
+
50
+ self.conv3 = nn.Conv2d(width * scale, planes * self.expansion, kernel_size=1, bias=False)
51
+ self.bn3 = nn.BatchNorm2d(planes * self.expansion)
52
+
53
+ self.relu = nn.ReLU(inplace=True)
54
+ self.downsample = downsample
55
+ self.stype = stype
56
+ self.scale = scale
57
+ self.width = width
58
+
59
+ def forward(self, x):
60
+ residual = x
61
+
62
+ out = self.conv1(x)
63
+ out = self.bn1(out)
64
+ out = self.relu(out)
65
+
66
+ spx = torch.split(out, self.width, 1)
67
+ for i in range(self.nums):
68
+ if i == 0 or self.stype == 'stage':
69
+ sp = spx[i]
70
+ else:
71
+ sp = sp + spx[i]
72
+ sp = self.convs[i](sp)
73
+ sp = self.relu(self.bns[i](sp))
74
+ if i == 0:
75
+ out = sp
76
+ else:
77
+ out = torch.cat((out, sp), 1)
78
+ if self.scale != 1 and self.stype == 'normal':
79
+ out = torch.cat((out, spx[self.nums]), 1)
80
+ elif self.scale != 1 and self.stype == 'stage':
81
+ out = torch.cat((out, self.pool(spx[self.nums])), 1)
82
+
83
+ out = self.conv3(out)
84
+ out = self.bn3(out)
85
+
86
+ if self.downsample is not None:
87
+ residual = self.downsample(x)
88
+
89
+ out += residual
90
+ out = self.relu(out)
91
+
92
+ return out
93
+
94
+
95
+ class Res2Net(nn.Module):
96
+
97
+ def __init__(self, block, layers, baseWidth=26, scale=4, num_classes=1000):
98
+ self.inplanes = 64
99
+ super(Res2Net, self).__init__()
100
+ self.baseWidth = baseWidth
101
+ self.scale = scale
102
+ self.conv1 = nn.Sequential(
103
+ nn.Conv2d(3, 32, 3, 2, 1, bias=False),
104
+ nn.BatchNorm2d(32),
105
+ nn.ReLU(inplace=True),
106
+ nn.Conv2d(32, 32, 3, 1, 1, bias=False),
107
+ nn.BatchNorm2d(32),
108
+ nn.ReLU(inplace=True),
109
+ nn.Conv2d(32, 64, 3, 1, 1, bias=False)
110
+ )
111
+ self.bn1 = nn.BatchNorm2d(64)
112
+ self.relu = nn.ReLU()
113
+ self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
114
+ self.layer1 = self._make_layer(block, 64, layers[0])
115
+ self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
116
+ self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
117
+ self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
118
+ self.avgpool = nn.AdaptiveAvgPool2d(1)
119
+ self.fc = nn.Linear(512 * block.expansion, num_classes)
120
+
121
+ for m in self.modules():
122
+ if isinstance(m, nn.Conv2d):
123
+ nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
124
+ elif isinstance(m, nn.BatchNorm2d):
125
+ nn.init.constant_(m.weight, 1)
126
+ nn.init.constant_(m.bias, 0)
127
+
128
+ def _make_layer(self, block, planes, blocks, stride=1):
129
+ downsample = None
130
+ if stride != 1 or self.inplanes != planes * block.expansion:
131
+ downsample = nn.Sequential(
132
+ nn.AvgPool2d(kernel_size=stride, stride=stride,
133
+ ceil_mode=True, count_include_pad=False),
134
+ nn.Conv2d(self.inplanes, planes * block.expansion,
135
+ kernel_size=1, stride=1, bias=False),
136
+ nn.BatchNorm2d(planes * block.expansion),
137
+ )
138
+
139
+ layers = []
140
+ layers.append(block(self.inplanes, planes, stride, downsample=downsample,
141
+ stype='stage', baseWidth=self.baseWidth, scale=self.scale))
142
+ self.inplanes = planes * block.expansion
143
+ for i in range(1, blocks):
144
+ layers.append(block(self.inplanes, planes, baseWidth=self.baseWidth, scale=self.scale))
145
+
146
+ return nn.Sequential(*layers)
147
+
148
+ def forward(self, x):
149
+ x = self.conv1(x)
150
+ x = self.bn1(x)
151
+ x = self.relu(x)
152
+ x = self.maxpool(x)
153
+
154
+ x = self.layer1(x)
155
+ x = self.layer2(x)
156
+ x = self.layer3(x)
157
+ x = self.layer4(x)
158
+
159
+ x = self.avgpool(x)
160
+ x = x.view(x.size(0), -1)
161
+ x = self.fc(x)
162
+
163
+ return x
164
+
165
+
166
+ def res2net50_v1b(pretrained=False, **kwargs):
167
+ """Constructs a Res2Net-50_v1b model.
168
+ Res2Net-50 refers to the Res2Net-50_v1b_26w_4s.
169
+ Args:
170
+ pretrained (bool): If True, returns a model pre-trained on ImageNet
171
+ """
172
+ model = Res2Net(Bottle2neck, [3, 4, 6, 3], baseWidth=26, scale=4, **kwargs)
173
+ if pretrained:
174
+ model.load_state_dict(model_zoo.load_url(model_urls['res2net50_v1b_26w_4s']))
175
+ return model
176
+
177
+
178
+ def res2net101_v1b(pretrained=False, **kwargs):
179
+ """Constructs a Res2Net-50_v1b_26w_4s model.
180
+ Args:
181
+ pretrained (bool): If True, returns a model pre-trained on ImageNet
182
+ """
183
+ model = Res2Net(Bottle2neck, [3, 4, 23, 3], baseWidth=26, scale=4, **kwargs)
184
+ if pretrained:
185
+ model.load_state_dict(model_zoo.load_url(model_urls['res2net101_v1b_26w_4s']))
186
+ return model
187
+
188
+
189
+ def res2net50_v1b_26w_4s(pretrained=False, **kwargs):
190
+ """Constructs a Res2Net-50_v1b_26w_4s model.
191
+ Args:
192
+ pretrained (bool): If True, returns a model pre-trained on ImageNet
193
+ """
194
+ model = Res2Net(Bottle2neck, [3, 4, 6, 3], baseWidth=26, scale=4, **kwargs)
195
+ if pretrained:
196
+ model.load_state_dict(torch.load(pthfile, map_location='cpu')) # load model
197
+ return model
198
+
199
+
200
+ def res2net101_v1b_26w_4s(pretrained=False, **kwargs):
201
+ """Constructs a Res2Net-50_v1b_26w_4s model.
202
+ Args:
203
+ pretrained (bool): If True, returns a model pre-trained on ImageNet
204
+ """
205
+ model = Res2Net(Bottle2neck, [3, 4, 23, 3], baseWidth=26, scale=4, **kwargs)
206
+ if pretrained:
207
+ model.load_state_dict(model_zoo.load_url(model_urls['res2net101_v1b_26w_4s']))
208
+ return model
209
+
210
+
211
+ def res2net152_v1b_26w_4s(pretrained=False, **kwargs):
212
+ """Constructs a Res2Net-50_v1b_26w_4s model.
213
+ Args:
214
+ pretrained (bool): If True, returns a model pre-trained on ImageNet
215
+ """
216
+ model = Res2Net(Bottle2neck, [3, 8, 36, 3], baseWidth=26, scale=4, **kwargs)
217
+ if pretrained:
218
+ model.load_state_dict(model_zoo.load_url(model_urls['res2net152_v1b_26w_4s']))
219
+ return model
220
+
221
+
222
+ class mutil_model(nn.Module):
223
+
224
+ def __init__(self, category_num=10):
225
+ super(mutil_model, self).__init__()
226
+ self.model1 = res2net50_v1b_26w_4s(pretrained=False)
227
+ self.model1.fc = nn.Sequential(
228
+ nn.Linear(in_features=2048, out_features=category_num, bias=True),
229
+ )
230
+ self.model2 = torch.load('./enet_b2_8' + '.pt', map_location=torch.device('cpu'))
231
+ self.model2.classifier = nn.Sequential(
232
+ nn.Linear(in_features=1408, out_features=category_num, bias=True),
233
+ )
234
+ self.fc = nn.Linear(in_features=category_num * 2, out_features=category_num, bias=True)
235
+
236
+ def forward(self, x):
237
+ x1 = self.model1(x)
238
+ x2 = self.model2(x)
239
+ x = torch.cat((x1, x2), 1)
240
+ x = self.fc(x)
241
+ return x
242
+
243
+
244
+ pth_path = './image_loader10new_model.pt'
245
+ category_num = 10
246
+
247
+ # "cuda" only when GPUs are available.
248
+ #device = "cuda" if torch.cuda.is_available() else "cpu"
249
+ device = "cpu"
250
+ #Initialize a model, and put it on the device specified.
251
+ # 导入res2net预训练模型
252
+ pthfile = './res2net50_v1b.pth'
253
+ model = res2net50_v1b_26w_4s(pretrained=False)
254
+ # 修改全连接层,输出维度为预测 分类
255
+ num_ftrs = model.fc.in_features
256
+ model.fc = nn.Sequential(
257
+ nn.Linear(in_features=2048, out_features=1000, bias=True),
258
+ nn.Dropout(0.5),
259
+ nn.Linear(1000, out_features=category_num)
260
+ )
261
+ model.fc = nn.Sequential(
262
+ nn.Linear(in_features=2048, out_features=category_num, bias=True),
263
+ )
264
+
265
+ model = model.to(device)
266
+ model.device = device
267
+ model.load_state_dict(torch.load(pth_path,torch.device('cpu')))
268
+ model.eval()
269
+
270
+
271
+ # 增加人脸识别模型
272
+ #model = mutil_model(category_num=7)
273
+ #model_state = torch.load('./add_face_emotion_model_7.pt', map_location=torch.device('cpu')).state_dict()
274
+ #model.load_state_dict(model_state) # 加载模型参数
275
+ #model.eval()
276
+
277
+ labels = ['中国风', '古典', '电子', '摇滚', '乡村', '说唱', '民谣', '二次元', '轻音乐', '儿歌']
278
+
279
+ import requests
280
+ import torch
281
+
282
+ import gradio as gr
283
+ import torchvision.transforms as transforms
284
+
285
+ # import cv2
286
+ # from PIL import Image
287
+ # PIL
288
+ # from PIL import Image
289
+ # inception_net = tf.keras.applications.MobileNetV2() # load the model
290
+
291
+ # Download human-readable labels for ImageNet.
292
+ # response = requests.get("https://git.io/JJkYN")
293
+ # labels = response.text.split("\n")
294
+ print(len(labels))
295
+
296
+
297
+ def classify_image(inp):
298
+ # inp = inp.convert('RGB')
299
+ # inp = Image.fromarray(inp.astype('uint8'), 'RGB')
300
+ transform_test = transforms.Compose([
301
+ # transforms.ToPILImage(),
302
+ transforms.Resize((256, 256)),
303
+ transforms.ToTensor(),
304
+ transforms.Normalize((0.485, 0.456, 0.406),
305
+ (0.229, 0.224, 0.225)),
306
+ ])
307
+ inp = transform_test(inp)
308
+ print(inp)
309
+ with torch.no_grad():
310
+ prediction = model(torch.unsqueeze(inp, 0)).flatten()
311
+ print(prediction)
312
+ prediction = torch.nn.Softmax(dim=0)(prediction)
313
+ print(prediction)
314
+ return {labels[i]: float(prediction[i].item()) for i in range(len(labels))}
315
+
316
+
317
+ # print(classify_image("/jj.jpg"))
318
+ # image = gr.inputs.Image(shape=(256, 256))
319
+ # image = gr.inputs.Image()
320
+ # print(image)
321
+ # label = gr.outputs.Label(num_top_classes=6)
322
+
323
+ gr.Interface(
324
+ classify_image,
325
+ # gr.inputs.Image(),
326
+ gr.inputs.Image(type='pil'),
327
+ outputs='label'
328
+ # inputs='image',
329
+ # outputs='label',
330
+ # examples=[["images/cheetah1.jpg"], ["images/lion.jpg"]],
331
+ ).launch(share=True)
332
+ # share=True