forked from layumi/Person_reID_baseline_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
386 lines (345 loc) · 14.2 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import torch
import torch.nn as nn
from torch.nn import init
from torchvision import models
from torch.autograd import Variable
import pretrainedmodels
import timm
from utils import load_state_dict_mute
######################################################################
def weights_init_kaiming(m):
classname = m.__class__.__name__
# print(classname)
if classname.find('Conv') != -1:
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in') # For old pytorch, you may use kaiming_normal.
elif classname.find('Linear') != -1:
init.kaiming_normal_(m.weight.data, a=0, mode='fan_out')
elif classname.find('BatchNorm1d') != -1:
init.normal_(m.weight.data, 1.0, 0.02)
if hasattr(m, 'bias') and m.bias is not None:
init.constant_(m.bias.data, 0.0)
def weights_init_classifier(m):
classname = m.__class__.__name__
if classname.find('Linear') != -1:
init.normal_(m.weight.data, std=0.001)
init.constant_(m.bias.data, 0.0)
def activate_drop(m):
classname = m.__class__.__name__
if classname.find('Drop') != -1:
m.p = 0.1
m.inplace = True
# Defines the new fc layer and classification layer
# |--Linear--|--bn--|--relu--|--Linear--|
class ClassBlock(nn.Module):
def __init__(self, input_dim, class_num, droprate, relu=False, bnorm=True, linear=512, return_f = False):
super(ClassBlock, self).__init__()
self.return_f = return_f
add_block = []
if linear>0:
add_block += [nn.Linear(input_dim, linear)]
else:
linear = input_dim
if bnorm:
add_block += [nn.BatchNorm1d(linear)]
if relu:
add_block += [nn.LeakyReLU(0.1)]
if droprate>0:
add_block += [nn.Dropout(p=droprate)]
add_block = nn.Sequential(*add_block)
add_block.apply(weights_init_kaiming)
classifier = []
classifier += [nn.Linear(linear, class_num)]
classifier = nn.Sequential(*classifier)
classifier.apply(weights_init_classifier)
self.add_block = add_block
self.classifier = classifier
def forward(self, x):
x = self.add_block(x)
if self.return_f:
f = x
x = self.classifier(x)
return [x,f]
else:
x = self.classifier(x)
return x
# Define the ResNet50-based Model
class ft_net(nn.Module):
def __init__(self, class_num=751, droprate=0.5, stride=2, circle=False, ibn=False, linear_num=512):
super(ft_net, self).__init__()
model_ft = models.resnet50(pretrained=True)
if ibn==True:
model_ft = torch.hub.load('XingangPan/IBN-Net', 'resnet50_ibn_a', pretrained=True)
# avg pooling to global pooling
if stride == 1:
model_ft.layer4[0].downsample[0].stride = (1,1)
model_ft.layer4[0].conv2.stride = (1,1)
model_ft.avgpool = nn.AdaptiveAvgPool2d((1,1))
self.model = model_ft
self.circle = circle
self.classifier = ClassBlock(2048, class_num, droprate, linear=linear_num, return_f = circle)
def forward(self, x):
x = self.model.conv1(x)
x = self.model.bn1(x)
x = self.model.relu(x)
x = self.model.maxpool(x)
x = self.model.layer1(x)
x = self.model.layer2(x)
x = self.model.layer3(x)
x = self.model.layer4(x)
x = self.model.avgpool(x)
x = x.view(x.size(0), x.size(1))
x = self.classifier(x)
return x
# Define the swin_base_patch4_window7_224 Model
# pytorch > 1.6
class ft_net_swin(nn.Module):
def __init__(self, class_num, droprate=0.5, stride=2, circle=False, linear_num=512):
super(ft_net_swin, self).__init__()
model_ft = timm.create_model('swin_base_patch4_window7_224', pretrained=True, drop_path_rate = 0.2)
# avg pooling to global pooling
#model_ft.avgpool = nn.AdaptiveAvgPool2d((1,1))
model_ft.head = nn.Sequential() # save memory
self.model = model_ft
self.circle = circle
self.avgpool1d = nn.AdaptiveAvgPool1d(1)
self.avgpool2d = nn.AdaptiveAvgPool2d((1,1))
self.classifier = ClassBlock(1024, class_num, droprate, linear=linear_num, return_f = circle)
print('Make sure timm > 0.6.0 and you can install latest timm version by pip install git+https://github.com/rwightman/pytorch-image-models.git')
def forward(self, x):
x = self.model.forward_features(x)
# swin is update in latest timm>0.6.0, so I add the following two lines.
if x.dim()==3:
x = self.avgpool1d(x.permute((0,2,1)))
else:
x = self.avgpool2d(x.permute((0,3,1,2)))
x = x.view(x.size(0), x.size(1))
x = self.classifier(x)
return x
class ft_net_swinv2(nn.Module):
def __init__(self, class_num, input_size=(256, 128), droprate=0.5, stride=2, circle=False, linear_num=512):
super(ft_net_swinv2, self).__init__()
model_ft = timm.create_model('swinv2_base_window8_256', pretrained=False, img_size = input_size, drop_path_rate = 0.2)
model_full = timm.create_model('swinv2_base_window8_256', pretrained=True)
load_state_dict_mute(model_ft, model_full.state_dict(), strict=False)
#model_ft = timm.create_model('swinv2_cr_small_224', pretrained=True, img_size = input_size, drop_path_rate = 0.2)
# avg pooling to global pooling
model_ft.head = nn.Sequential() # save memory
self.model = model_ft
self.circle = circle
self.avgpool1d = nn.AdaptiveAvgPool1d(1)
self.avgpool2d = nn.AdaptiveAvgPool2d((1,1))
self.classifier = ClassBlock(1024, class_num, droprate, linear=linear_num, return_f = circle)
print('Make sure timm > 0.6.0 and you can install latest timm version by pip install git+https://github.com/rwightman/pytorch-image-models.git')
def forward(self, x):
x = self.model.forward_features(x)
if x.dim()==3:
x = self.avgpool1d(x.permute((0,2,1)))
else:
x = self.avgpool2d(x.permute((0,3,1,2)))
x = x.view(x.size(0), x.size(1))
x = self.classifier(x)
return x
class ft_net_convnext(nn.Module):
def __init__(self, class_num, droprate=0.5, stride=2, circle=False, linear_num=512):
super(ft_net_convnext, self).__init__()
model_ft = timm.create_model('convnext_base', pretrained=True, drop_path_rate = 0.2)
# avg pooling to global pooling
#model_ft.avgpool = nn.AdaptiveAvgPool2d((1,1))
model_ft.head = nn.Sequential() # save memory
self.model = model_ft
#self.model.apply(activate_drop)
self.circle = circle
self.avgpool = nn.AdaptiveAvgPool2d((1,1))
self.classifier = ClassBlock(1024, class_num, droprate, linear=linear_num, return_f = circle)
def forward(self, x):
x = self.model.forward_features(x)
x = self.avgpool(x)
x = x.view(x.size(0), x.size(1))
x = self.classifier(x)
return x
# Define the HRNet18-based Model
class ft_net_hr(nn.Module):
def __init__(self, class_num, droprate=0.5, circle=False, linear_num=512):
super().__init__()
model_ft = timm.create_model('hrnet_w18', pretrained=True)
# avg pooling to global pooling
#model_ft.avgpool = nn.AdaptiveAvgPool2d((1,1))
model_ft.classifier = nn.Sequential() # save memory
self.model = model_ft
self.circle = circle
self.avgpool = nn.AdaptiveAvgPool2d((1,1))
self.classifier = ClassBlock(2048, class_num, droprate, linear=linear_num, return_f = circle)
def forward(self, x):
x = self.model.forward_features(x)
x = self.avgpool(x)
x = x.view(x.size(0), x.size(1))
x = self.classifier(x)
return x
# Define the DenseNet121-based Model
class ft_net_dense(nn.Module):
def __init__(self, class_num, droprate=0.5, stride = 2, circle=False, linear_num=512):
super().__init__()
model_ft = models.densenet121(pretrained=True)
model_ft.features.avgpool = nn.AdaptiveAvgPool2d((1,1))
model_ft.fc = nn.Sequential()
if stride == 1:
model_ft.features.transition3.pool.stride = 1
self.model = model_ft
self.circle = circle
# For DenseNet, the feature dim is 1024
self.classifier = ClassBlock(1024, class_num, droprate, linear=linear_num, return_f=circle)
def forward(self, x):
x = self.model.features(x)
x = x.view(x.size(0), x.size(1))
x = self.classifier(x)
return x
# Define the Efficient-b4-based Model
class ft_net_efficient(nn.Module):
def __init__(self, class_num, droprate=0.5, circle=False, linear_num=512):
super().__init__()
#model_ft = timm.create_model('tf_efficientnet_b4', pretrained=True)
try:
from efficientnet_pytorch import EfficientNet
except ImportError:
print('Please pip install efficientnet_pytorch')
model_ft = EfficientNet.from_pretrained('efficientnet-b4')
# avg pooling to global pooling
#model_ft.avgpool = nn.AdaptiveAvgPool2d((1,1))
model_ft.head = nn.Sequential() # save memory
model_ft.avgpool = nn.AdaptiveAvgPool2d((1,1))
model_ft.classifier = nn.Sequential()
self.model = model_ft
self.circle = circle
# For EfficientNet, the feature dim is not fixed
# for efficientnet_b2 1408
# for efficientnet_b4 1792
self.classifier = ClassBlock(1792, class_num, droprate, linear=linear_num, return_f=circle)
def forward(self, x):
#x = self.model.forward_features(x)
x = self.model.extract_features(x)
x = self.model.avgpool(x)
x = x.view(x.size(0), x.size(1))
x = self.classifier(x)
return x
# Define the NAS-based Model
class ft_net_NAS(nn.Module):
def __init__(self, class_num, droprate=0.5, linear_num=512):
super().__init__()
model_name = 'nasnetalarge'
# pip install pretrainedmodels
model_ft = pretrainedmodels.__dict__[model_name](num_classes=1000, pretrained='imagenet')
model_ft.avg_pool = nn.AdaptiveAvgPool2d((1,1))
model_ft.dropout = nn.Sequential()
model_ft.last_linear = nn.Sequential()
self.model = model_ft
# For DenseNet, the feature dim is 4032
self.classifier = ClassBlock(4032, class_num, droprate, linear=linear_num)
def forward(self, x):
x = self.model.features(x)
x = self.model.avg_pool(x)
x = x.view(x.size(0), x.size(1))
x = self.classifier(x)
return x
# Define the ResNet50-based Model (Middle-Concat)
# In the spirit of "The Devil is in the Middle: Exploiting Mid-level Representations for Cross-Domain Instance Matching." Yu, Qian, et al. arXiv:1711.08106 (2017).
class ft_net_middle(nn.Module):
def __init__(self, class_num=751, droprate=0.5):
super(ft_net_middle, self).__init__()
model_ft = models.resnet50(pretrained=True)
# avg pooling to global pooling
model_ft.avgpool = nn.AdaptiveAvgPool2d((1,1))
self.model = model_ft
self.classifier = ClassBlock(2048, class_num, droprate)
def forward(self, x):
x = self.model.conv1(x)
x = self.model.bn1(x)
x = self.model.relu(x)
x = self.model.maxpool(x)
x = self.model.layer1(x)
x = self.model.layer2(x)
x = self.model.layer3(x)
x = self.model.layer4(x)
x = self.model.avgpool(x)
x = torch.squeeze(x)
x = self.classifier(x) #use our classifier.
return x
# Part Model proposed in Yifan Sun etal. (2018)
class PCB(nn.Module):
def __init__(self, class_num ):
super(PCB, self).__init__()
self.part = 6 # We cut the pool5 to 6 parts
model_ft = models.resnet50(pretrained=True)
self.model = model_ft
self.avgpool = nn.AdaptiveAvgPool2d((self.part,1))
self.dropout = nn.Dropout(p=0.5)
# remove the final downsample
self.model.layer4[0].downsample[0].stride = (1,1)
self.model.layer4[0].conv2.stride = (1,1)
# define 6 classifiers
for i in range(self.part):
name = 'classifier'+str(i)
setattr(self, name, ClassBlock(2048, class_num, droprate=0.5, linear=256, relu=False, bnorm=True))
def forward(self, x):
x = self.model.conv1(x)
x = self.model.bn1(x)
x = self.model.relu(x)
x = self.model.maxpool(x)
x = self.model.layer1(x)
x = self.model.layer2(x)
x = self.model.layer3(x)
x = self.model.layer4(x)
x = self.avgpool(x)
x = self.dropout(x)
part = {}
predict = {}
# get six part feature batchsize*2048*6
for i in range(self.part):
part[i] = x[:,:,i].view(x.size(0), x.size(1))
name = 'classifier'+str(i)
c = getattr(self,name)
predict[i] = c(part[i])
# sum prediction
#y = predict[0]
#for i in range(self.part-1):
# y += predict[i+1]
y = []
for i in range(self.part):
y.append(predict[i])
return y
class PCB_test(nn.Module):
def __init__(self,model):
super(PCB_test,self).__init__()
self.part = 6
self.model = model.model
self.avgpool = nn.AdaptiveAvgPool2d((self.part,1))
# remove the final downsample
self.model.layer4[0].downsample[0].stride = (1,1)
self.model.layer4[0].conv2.stride = (1,1)
def forward(self, x):
x = self.model.conv1(x)
x = self.model.bn1(x)
x = self.model.relu(x)
x = self.model.maxpool(x)
x = self.model.layer1(x)
x = self.model.layer2(x)
x = self.model.layer3(x)
x = self.model.layer4(x)
x = self.avgpool(x)
y = x.view(x.size(0),x.size(1),x.size(2))
return y
'''
# debug model structure
# Run this code with:
python model.py
'''
if __name__ == '__main__':
# Here I left a simple forward function.
# Test the model, before you train it.
net = ft_net_hr(751)
#net = ft_net_swin(751, stride=1)
net.classifier = nn.Sequential()
print(net)
input = Variable(torch.FloatTensor(8, 3, 224, 224))
output = net(input)
print('net output size:')
print(output.shape)