-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone_network.py
467 lines (406 loc) · 16.3 KB
/
backbone_network.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
"""
Copyright (C) 2018 NVIDIA Corporation. All rights reserved.
Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode).
"""
import pdb
import torch
import torch.nn as nn
from torch.nn import init
from torchvision import models
import os
from utils import weights_init
######################################################################
def weights_init_kaiming(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
elif classname.find('Linear') != -1:
init.kaiming_normal_(m.weight.data, a=0, mode='fan_out')
init.constant_(m.bias.data, 0.0)
elif classname.find('InstanceNorm1d') != -1:
init.normal_(m.weight.data, 1.0, 0.02)
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 load_network(network):
save_path = os.path.join('./models/Duke_best/net_last.pth')
network.load_state_dict(torch.load(save_path),strict=False)
return network
def fix_bn(m):
classname = m.__class__.__name__
if classname.find('BatchNorm') != -1:
m.eval()
# Defines the new fc layer and classification layer
# |--Linear--|--bn--|--relu--|--Linear--|
class ClassBlock(nn.Module):
def __init__(self, input_dim, class_num, droprate=0.5, relu=False, num_bottleneck=512):
super(ClassBlock, self).__init__()
add_block = []
add_block += [nn.Linear(input_dim, num_bottleneck)]
#num_bottleneck = input_dim # We remove the input_dim
add_block += [nn.BatchNorm1d(num_bottleneck, affine=True)]
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(num_bottleneck, 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)
x = self.classifier(x)
return x
class cam_dis(nn.Module):
def __init__(self):
super(cam_dis, self).__init__()
#self.cnns = nn.ModuleList()
self.model = []
self.model += [Conv2dBlock(3, 64, 3, 2, 1, norm='in', activation='lrelu', pad_type='reflect')]
self.model += [Conv2dBlock(64, 128, 3, 1, 1, norm='in', activation='lrelu', pad_type='reflect')]
self.model += [Conv2dBlock(128, 256, 3, 2, 1, norm='in', activation='lrelu', pad_type='reflect')]
self.model += [Conv2dBlock(256, 512, 3, 1, 1, norm='in', activation='lrelu', pad_type='reflect')]
self.model += [nn.AvgPool2d(3)]
self.model += [nn.Dropout(p=0.999)]
self.model += [nn.Linear(512,8)]
self.model = nn.Sequential(*self.model)
#self.cnns.apply(weights_init('gaussian'))
#self.cnns.append(self.model)
def forward(self,x):
x = self.model(x)
return x
class StyleEncoder(nn.Module):
def __init__(self, n_downsample, input_dim, dim, style_dim, norm, activ, pad_type):
super(StyleEncoder, self).__init__()
self.model = []
# Here I change the stride to 2.
self.model += [Conv2dBlock(input_dim, dim, 3, 2, 1, norm=norm, activation=activ, pad_type=pad_type)]
self.model += [Conv2dBlock(dim, dim, 3, 1, 1, norm=norm, activation=activ, pad_type=pad_type)]
for i in range(2):
self.model += [Conv2dBlock(dim, 2 * dim, 3, 2, 1, norm=norm, activation=activ, pad_type=pad_type)]
dim *= 2
for i in range(n_downsample - 2):
self.model += [Conv2dBlock(dim, dim, 3, 2, 1, norm=norm, activation=activ, pad_type=pad_type)]
self.model += [nn.AdaptiveAvgPool2d(1)] # global average pooling
self.model += [nn.Conv2d(dim, 128, 1, 1, 0)]
self.model = nn.Sequential(*self.model)
self.classifier = ClassBlock(128, 8)
def forward(self, x):
#pdb.set_trace()
c_bf = self.model(x)
#print(c_bf.shape)
#print(x.shape)
#pdb.set_trace()
c_bf = c_bf.view(c_bf.shape[0],c_bf.shape[1])
c_b = self.classifier(c_bf)
return c_b
class Conv2dBlock(nn.Module):
def __init__(self, input_dim ,output_dim, kernel_size, stride,
padding=0, norm='none', activation='relu', pad_type='zero', dilation=1, fp16 = False):
super(Conv2dBlock, self).__init__()
self.use_bias = True
# initialize padding
if pad_type == 'reflect':
self.pad = nn.ReflectionPad2d(padding)
elif pad_type == 'replicate':
self.pad = nn.ReplicationPad2d(padding)
elif pad_type == 'zero':
self.pad = nn.ZeroPad2d(padding)
else:
assert 0, "Unsupported padding type: {}".format(pad_type)
# initialize normalization
norm_dim = output_dim
if norm == 'bn':
self.norm = nn.BatchNorm2d(norm_dim)
elif norm == 'in':
self.norm = nn.InstanceNorm2d(norm_dim)
elif norm == 'ln':
self.norm = LayerNorm(norm_dim, fp16 = fp16)
elif norm == 'adain':
self.norm = AdaptiveInstanceNorm2d(norm_dim)
elif norm == 'none' or norm == 'sn':
self.norm = None
else:
assert 0, "Unsupported normalization: {}".format(norm)
# initialize activation
if activation == 'relu':
self.activation = nn.ReLU(inplace=True)
elif activation == 'lrelu':
self.activation = nn.LeakyReLU(0.2, inplace=True)
elif activation == 'prelu':
self.activation = nn.PReLU()
elif activation == 'selu':
self.activation = nn.SELU(inplace=True)
elif activation == 'tanh':
self.activation = nn.Tanh()
elif activation == 'none':
self.activation = None
else:
assert 0, "Unsupported activation: {}".format(activation)
# initialize convolution
if norm == 'sn':
self.conv = SpectralNorm(nn.Conv2d(input_dim, output_dim, kernel_size, stride, dilation=dilation, bias=self.use_bias))
else:
self.conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride, dilation=dilation, bias=self.use_bias)
def forward(self, x):
x = self.conv(self.pad(x))
if self.norm:
x = self.norm(x)
if self.activation:
x = self.activation(x)
return x
# Define the ResNet50-based Model
class ft_net(nn.Module):
def __init__(self, class_num, norm=False, pool='avg', stride=2):
super(ft_net, self).__init__()
if norm:
self.norm = True
else:
self.norm = False
model_ft = models.resnet50(pretrained=True)
# avg pooling to global pooling
self.part = 4
if pool=='max':
model_ft.partpool = nn.AdaptiveMaxPool2d((self.part,1))
model_ft.avgpool = nn.AdaptiveMaxPool2d((1,1))
else:
model_ft.partpool = nn.AdaptiveAvgPool2d((self.part,1))
model_ft.avgpool = nn.AdaptiveAvgPool2d((1,1))
# remove the final downsample
if stride == 1:
model_ft.layer4[0].downsample[0].stride = (1,1)
model_ft.layer4[0].conv2.stride = (1,1)
self.model = model_ft
self.classifier = ClassBlock(2048, class_num)
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) # -> 512 32*16
x = self.model.layer3(x)
x = self.model.layer4(x)
f = self.model.partpool(x) # 8 * 2048 4*1
x = self.model.avgpool(x) # 8 * 2048 1*1
x = x.view(x.size(0),x.size(1))
f = f.view(f.size(0),f.size(1)*self.part)
if self.norm:
fnorm = torch.norm(f, p=2, dim=1, keepdim=True) + 1e-8
f = f.div(fnorm.expand_as(f))
x = self.classifier(x)
return f, x
class ft_netAB1(nn.Module):
def __init__(self, class_num, norm=False, stride=2, droprate=0.5, pool='avg'):
super(ft_netAB1, self).__init__()
model_tmp = ft_net(702, stride = stride)
model_ft = load_network(model_tmp)
model_ft.model.fc = nn.Sequential()
model_ft.classifier.classifier = nn.Linear(512, 8)
#model_ft = models.resnet50(pretrained=True)
self.part = 4
if pool=='max':
model_ft.partpool = nn.AdaptiveMaxPool2d((self.part,1))
model_ft.avgpool = nn.AdaptiveMaxPool2d((1,1))
else:
model_ft.partpool = nn.AdaptiveAvgPool2d((self.part,1))
model_ft.avgpool = nn.AdaptiveAvgPool2d((1,1))
self.model = model_ft
if stride == 1:
self.model.model.layer4[0].downsample[0].stride = (1,1)
self.model.model.layer4[0].conv2.stride = (1,1)
self.classifier1 = ClassBlock(2048, class_num, 0.5)
self.classifier2 = ClassBlock(2048, class_num, 0.75)
print("Duke_cam_classes",class_num)
def forward(self, x):
x = self.model.model.conv1(x)
x = self.model.model.bn1(x)
x = self.model.model.relu(x)
x = self.model.model.maxpool(x)
x = self.model.model.layer1(x)
x = self.model.model.layer2(x)
x = self.model.model.layer3(x)
x = self.model.model.layer4(x)
f = self.model.model.partpool(x)
f = f.view(f.size(0),f.size(1)*self.part)
f = f.detach() # no gradient
x = self.model.model.avgpool(x)
x = x.view(x.size(0), x.size(1))
x1 = self.classifier1(x)
x2 = self.classifier2(x)
x=[]
x.append(x1)
x.append(x2)
return f, x
# Define the AB Model
class ft_netAB(nn.Module):
def __init__(self, class_num, norm=False, stride=2, droprate=0.5, pool='avg'):
super(ft_netAB, self).__init__()
model_ft = models.resnet50(pretrained=True)
self.part = 4
if pool=='max':
model_ft.partpool = nn.AdaptiveMaxPool2d((self.part,1))
model_ft.avgpool = nn.AdaptiveMaxPool2d((1,1))
else:
model_ft.partpool = nn.AdaptiveAvgPool2d((self.part,1))
model_ft.avgpool = nn.AdaptiveAvgPool2d((1,1))
self.model = model_ft
if stride == 1:
self.model.layer4[0].downsample[0].stride = (1,1)
self.model.layer4[0].conv2.stride = (1,1)
# pdb.set_trace()
self.classifier1 = ClassBlock(2048, class_num, 0.5)
self.classifier2 = ClassBlock(2048, class_num, 0.75)
# self.classifier3 = ClassBlock(2048, market_class_num, 0.5)
# self.classifier4 = ClassBlock(2048, market_class_num, 0.75)
# self.classifier5 = ClassBlock(2048, duke_class_num, 0.5)
# self.classifier6 = ClassBlock(2048, duke_class_num, 0.75)
def forward(self, x):
#pdb.set_trace()
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)
#pdb.set_trace()
#print(x.shape)
# part = {}
# p=0
# for i in range(2):
# part[i] = x[:,:,p:p+8,:]
# part[i] = self.model.avgpool(part[i])
# p=p+4
# part[i] = part[i].view(part[i].size(0), part[i].size(1))
#x_2 = self.model.avgpool(part[1])
#x_3 = self.model.avgpool(part[2])
#x_4 = self.model.avgpool(part[3])
# x3 = torch.cat((part[0],part[1],part[2],part[3]),dim=1)
f = self.model.partpool(x)
f = f.view(f.size(0),f.size(1)*self.part)
f = f.detach() # no gradient
x = self.model.avgpool(x)
x = x.view(x.size(0), x.size(1))
x1 = self.classifier1(x)
x2 = self.classifier2(x)
x=[]
x.append(x1)
x.append(x2)
return f, x
# Define the DenseNet121-based Model
class ft_net_dense(nn.Module):
def __init__(self, class_num ):
super().__init__()
model_ft = models.densenet121(pretrained=True)
model_ft.features.avgpool = nn.AdaptiveAvgPool2d((1,1))
model_ft.fc = nn.Sequential()
self.model = model_ft
# For DenseNet, the feature dim is 1024
self.classifier = ClassBlock(1024, class_num)
def forward(self, x):
x = self.model.features(x)
x = torch.squeeze(x)
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 ):
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+1024, class_num)
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)
# x0 n*1024*1*1
x0 = self.model.avgpool(x)
x = self.model.layer4(x)
# x1 n*2048*1*1
x1 = self.model.avgpool(x)
x = torch.cat((x0,x1),1)
x = torch.squeeze(x)
x = self.classifier(x)
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 = 4 # We cut the pool5 to 4 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)
self.softmax = nn.Softmax(dim=1)
# define 4 classifiers
for i in range(self.part):
name = 'classifier'+str(i)
setattr(self, name, ClassBlock(2048, class_num, True, False, 256))
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)
f = x
f = f.view(f.size(0),f.size(1)*self.part)
x = self.dropout(x)
part = {}
predict = {}
# get part feature batchsize*2048*4
for i in range(self.part):
part[i] = x[:,:,i].contiguous()
part[i] = part[i].view(x.size(0), x.size(1))
name = 'classifier'+str(i)
c = getattr(self,name)
predict[i] = c(part[i])
y=[]
for i in range(self.part):
y.append(predict[i])
return f, 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.layer3[0].downsample[0].stride = (1,1)
self.model.layer3[0].conv2.stride = (1,1)
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