-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodel.py
More file actions
1241 lines (1148 loc) · 52.6 KB
/
Copy pathmodel.py
File metadata and controls
1241 lines (1148 loc) · 52.6 KB
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import math
import torch
import os
import torch.nn as nn
from torch.nn import init
from torchvision import models
from torch.autograd import Variable
from torch.nn import functional as F
from resnet_adaibn import resnet50_adaibn_a, pretrained_in_weight
# from resnet_paibn import resnet50_ibn_a_adapter
######################################################################
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')
init.constant_(m.bias.data, 0.0)
elif classname.find('BatchNorm1d') != -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)
elif classname.find('BatchNorm1d') != -1:
init.normal_(m.weight.data, 1.0, 0.02)
init.constant_(m.bias.data, 0.0)
def fix_relu(m):
classname = m.__class__.__name__
if classname.find('ReLU') != -1:
m.inplace=True
def assign_adain_params(adain_params_w, adain_params_b, model, dim=32, init_w=None, init_b=None):
# assign the adain_params to the AdaIN layers in model
# dim = self.output_dim
for m in model.modules():
if m.__class__.__name__ == "AdaptiveInstanceNorm2d":
mean = adain_params_b[:, :dim].contiguous()
std = adain_params_w[:, :dim].contiguous()
m.bias = mean.view(-1)
m.weight = std.view(-1)
if adain_params_w.size(1) > dim: # Pop the parameters
adain_params_b = adain_params_b[:, dim:]
adain_params_w = adain_params_w[:, dim:]
def spade_norm(layer, x, mod_f):
_, channel, _, _ = x.shape
half = int(0.5 * channel)
split = torch.split(x, half, 1)
# if mod_f[0].size()[2:] != split[0].size()[2:]:
# mod_f[0] = F.interpolate(mod_f[0], size=split[0].size()[2:], mode='nearest')
# mod_f[1] = F.interpolate(mod_f[1], size=split[0].size()[2:], mode='nearest')
out1 = layer.IN(split[0].contiguous())
out1 = out1 * (mod_f[0] + 1) + mod_f[1]
# out1 = out1 * (mod_f[0] + 1)
# out1 = out1 * torch.exp(mod_f[0]) + mod_f[1]
out2 = layer.BN(split[1].contiguous())
out = torch.cat((out1, out2), 1)
return out
def extract_spade_feature(layer, x, mod_f):
input = [x]
for i, c_layer in enumerate(layer.children()):
residual = input[-1]
out = c_layer.conv1(input[-1])
if hasattr(c_layer.bn1, 'IN') and mod_f[i][0] != None:
out = spade_norm(c_layer.bn1, out, mod_f[i])
else:
out = c_layer.bn1(out)
out = c_layer.relu(out)
out = c_layer.conv2(out)
out = c_layer.bn2(out)
out = c_layer.relu(out)
out = c_layer.conv3(out)
out = c_layer.bn3(out)
if c_layer.downsample is not None:
residual = c_layer.downsample(residual)
out += residual
out = c_layer.relu(out)
input.append(out)
return input[-1]
# Defines the new fc layer and classification layer
# |--Linear--|--bn--|--relu--|--Linear--|
class ClassBlock(nn.Module):
def __init__(self, input_dim, class_num, droprate, num_bottleneck=512, return_f=False):
super(ClassBlock, self).__init__()
self.return_f = return_f
self.Linear = nn.Linear(input_dim, num_bottleneck)
self.bnorm = nn.BatchNorm1d(num_bottleneck)
self.dropout = nn.Dropout(p=droprate)
init.kaiming_normal_(self.Linear.weight.data, a=0, mode='fan_out')
init.constant_(self.Linear.bias.data, 0.0)
init.normal_(self.bnorm.weight.data, 1.0, 0.02)
init.constant_(self.bnorm.bias.data, 0.0)
classifier = []
classifier += [nn.Linear(num_bottleneck, class_num)]
classifier = nn.Sequential(*classifier)
classifier.apply(weights_init_classifier)
self.classifier = classifier
def forward(self, x):
x = self.Linear(x)
x = self.bnorm(x)
x = self.dropout(x)
if self.return_f:
f = x
x = self.classifier(x)
return [x,f]
else:
x = self.classifier(x)
return x
class LinearBlock(nn.Module):
def __init__(self, input_dim, output_dim, norm='none', activation='relu', init_mode='kaiming'):
super(LinearBlock, self).__init__()
use_bias = True
# initialize fully connected layer
self.fc = nn.Linear(input_dim, output_dim, bias=use_bias)
if init_mode == 'kaiming':
init.kaiming_normal_(self.fc.weight.data, a=0, mode='fan_out')
init.constant_(self.fc.bias.data, 0.0)
elif init_mode == 'normal':
init.normal_(self.fc.weight.data, std=0.001)
init.constant_(self.fc.bias.data, 0.0)
elif init_mode == 'none':
pass
# initialize normalization
norm_dim = output_dim
if norm == 'bn':
self.norm = nn.BatchNorm1d(norm_dim)
elif norm == 'in':
self.norm = nn.InstanceNorm1d(norm_dim)
elif norm == 'ln':
self.norm = nn.LayerNorm(norm_dim)
elif norm == 'none':
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)
def forward(self, x):
out = self.fc(x)
if self.norm:
#reshape input
# out = out.unsqueeze(1)
out = self.norm(out)
out = out.view(out.size(0),out.size(1))
if self.activation:
out = self.activation(out)
return out
class MLP(nn.Module):
def __init__(self, input_dim, output_dim, dim, n_blk, norm='in', activ='relu'):
super(MLP, self).__init__()
self.model = []
self.model += [LinearBlock(input_dim, dim, norm=norm, activation=activ, init_mode='kaiming')]
for i in range(n_blk - 2):
self.model += [LinearBlock(dim, dim, norm=norm, activation=activ, init_mode='kaiming')]
self.model = nn.Sequential(*self.model)
self.Gen = []
self.Gen += [LinearBlock(dim, output_dim, norm='none', activation='none', init_mode='normal')]
self.Gen = nn.Sequential(*self.Gen)
# self.model += [LinearBlock(dim, output_dim, norm='none', activation='none')] # no output activations
# self.model = nn.Sequential(*self.model)
def forward(self, x):
x = self.model(x.view(x.size(0), -1))
x = self.Gen(x)
return x
class ConvBlock(nn.Module):
def __init__(self, input_dim, output_dim, kernel_size, stride, padding, norm='none', activation='relu', init_mode='normal'):
super(ConvBlock, self).__init__()
use_bias = True
# initialize fully connected layer
self.conv2d = nn.Conv2d(input_dim, output_dim, kernel_size, stride, padding, bias=use_bias)
if init_mode == 'kaiming':
init.kaiming_normal_(self.conv2d.weight.data, mode='fan_in', nonlinearity="relu")
init.constant_(self.conv2d.bias.data, 0.0)
elif init_mode == 'normal':
init.normal_(self.conv2d.weight.data, std=0.001)
init.constant_(self.conv2d.bias.data, 0.0)
elif init_mode == 'none':
pass
# 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 = nn.LayerNorm([norm_dim,64,64])
elif norm == 'none':
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)
def forward(self, x):
out = self.conv2d(x)
if self.norm:
#reshape input
# out = out.unsqueeze(1)
out = self.norm(out)
# out = out.view(out.size(0),out.size(1))
if self.activation:
out = self.activation(out)
return out
class MOD_(nn.Module):
def __init__(self, input_dim, output_dim, kernel_size=3, stride=1, padding=1, dim=0, n_blk=0, norm='in', activ='relu', init_mode=['normal']):
super(MOD_, self).__init__()
# self.model = []
# self.model += [LinearBlock(input_dim, dim, norm=norm, activation=activ, init_mode='kaiming')]
# for i in range(n_blk - 2):
# self.model += [LinearBlock(dim, dim, norm=norm, activation=activ, init_mode='kaiming')]
# self.model = nn.Sequential(*self.model)
self.Gen = []
self.Gen += [ConvBlock(input_dim, output_dim, kernel_size, stride, padding, norm=norm, activation='none', init_mode=init_mode[-1])]
self.Gen = nn.Sequential(*self.Gen)
def forward(self, x):
# x = self.model(x.view(x.size(0), -1))
x = self.Gen(x)
return x
class Pt_ResNet50(nn.Module):
def __init__(self, pool='avg', init_model=None, norm='ada-ibn', init_mode=['normal'], btnk=[1,0,1], conv_norm='in'):
super(Pt_ResNet50, self).__init__()
# model_ft = models.vgg16_bn(pretrained=True)
model_ft = models.resnet50(pretrained=True)
model_ft.layer2[3].relu = nn.Sequential()
self.pool = pool
if pool=='avg':
model_ft.avgpool2 = nn.AdaptiveAvgPool2d((1,1))
self.model = model_ft
elif pool=='max':
model_ft.maxpool2 = nn.AdaptiveMaxPool2d((1,1))
self.model = model_ft
self.norm = norm
self.init_mode = init_mode
self.btnk = btnk
if norm == 'spade':
if btnk[0] == 1:
self.layer1_0_w = MOD_(512, 32, kernel_size=3, stride=1, padding=1, norm=conv_norm, activ='none', init_mode=init_mode)
self.layer1_0_b = MOD_(512, 32, kernel_size=3, stride=1, padding=1, norm=conv_norm, activ='none', init_mode=init_mode)
if btnk[1] == 1:
self.layer1_1_w = MOD_(512, 32, kernel_size=3, stride=1, padding=1, norm=conv_norm, activ='none', init_mode=init_mode)
self.layer1_1_b = MOD_(512, 32, kernel_size=3, stride=1, padding=1, norm=conv_norm, activ='none', init_mode=init_mode)
if btnk[2] == 1:
self.layer1_2_w = MOD_(512, 32, kernel_size=3, stride=1, padding=1, norm=conv_norm, activ='none', init_mode=init_mode)
self.layer1_2_b = MOD_(512, 32, kernel_size=3, stride=1, padding=1, norm=conv_norm, activ='none', init_mode=init_mode)
if btnk[3] == 1:
self.layer2_0_w = MOD_(512, 64, kernel_size=3, stride=1, padding=1, norm=conv_norm, activ='none', init_mode=init_mode)
self.layer2_0_b = MOD_(512, 64, kernel_size=3, stride=1, padding=1, norm=conv_norm, activ='none', init_mode=init_mode)
if btnk[4] == 1:
self.layer2_1_w = MOD_(512, 64, kernel_size=3, stride=1, padding=1, norm=conv_norm, activ='none', init_mode=init_mode)
self.layer2_1_b = MOD_(512, 64, kernel_size=3, stride=1, padding=1, norm=conv_norm, activ='none', init_mode=init_mode)
if btnk[5] == 1:
self.layer2_2_w = MOD_(512, 64, kernel_size=3, stride=1, padding=1, norm=conv_norm, activ='none', init_mode=init_mode)
self.layer2_2_b = MOD_(512, 64, kernel_size=3, stride=1, padding=1, norm=conv_norm, activ='none', init_mode=init_mode)
if btnk[6] == 1:
self.layer2_3_w = MOD_(512, 64, kernel_size=3, stride=1, padding=1, norm=conv_norm, activ='none', init_mode=init_mode)
self.layer2_3_b = MOD_(512, 64, kernel_size=3, stride=1, padding=1, norm=conv_norm, activ='none', init_mode=init_mode)
else:
self.layer1_w = MLP(512, 32, 512, 3, norm='none', activ='lrelu')
self.layer1_b = MLP(512, 32, 512, 3, norm='none', activ='lrelu')
# self.layer2_w = MLP(512, 64, 512, 3, norm='none', activ='lrelu')
# self.layer2_b = MLP(512, 64, 512, 3, norm='none', activ='lrelu')
# self.layer3_w = MLP(512, 128, 512, 3, norm='none', activ='lrelu')
# self.layer3_b = MLP(512, 128, 512, 3, norm='none', activ='lrelu')
# self.layer1_1_w = MLP(512, 32, 512, 3, norm='none', activ='lrelu')
# self.layer1_1_b = MLP(512, 32, 512, 3, norm='none', activ='lrelu')
self.layer1_2_w = MLP(512, 32, 512, 3, norm='none', activ='lrelu')
self.layer1_2_b = MLP(512, 32, 512, 3, norm='none', activ='lrelu')
# self.layer2_3_w = MLP(512, 64, 512, 3, norm='none', activ='lrelu')
# self.layer2_3_b = MLP(512, 64, 512, 3, norm='none', activ='lrelu')
# self.layer3_5_w = MLP(512, 128, 512, 3, norm='none', activ='lrelu')
# self.layer3_5_b = MLP(512, 128, 512, 3, norm='none', activ='lrelu')
# self.layer3_3_w = MLP(512, 128, 512, 3, norm='none', activ='lrelu')
# self.layer3_3_b = MLP(512, 128, 512, 3, norm='none', activ='lrelu')
# classification
# self.bn = nn.BatchNorm1d(512)
# init.normal_(self.bn.weight.data, 1.0, 0.02)
# init.constant_(self.bn.bias.data, 0.0)
classifier = []
classifier += [nn.BatchNorm1d(512)]
classifier += [nn.Dropout(p=0.5)]
# classifier += [nn.Linear(512, 10)]
classifier += [nn.Linear(512, 11)]
self.classifier = nn.Sequential(*classifier)
self.classifier.apply(weights_init_classifier)
if init_model!=None:
self.model = init_model.model
self.pool = init_model.pool
def forward(self, x):
# x = self.model.features(x)
x = self.model.conv1(x)
x = self.model.bn1(x)
x = self.model.relu(x)
x0 = self.model.maxpool(x)
x1 = self.model.layer1(x0)
x2 = self.model.layer2(x1)
if self.pool == 'avg':
x = self.model.avgpool2(x2)
x = x.view(x.size(0), x.size(1))
elif self.pool == 'max':
x = self.model.maxpool2(x2)
x = x.view(x.size(0), x.size(1))
if self.norm == 'spade':
B, C, H, W = x2.size()
x2_ = F.interpolate(x2, size=(2*H, 2*W), mode='nearest')
if self.btnk[0] == 1:
w1 = self.layer1_0_w(x2_)
b1 = self.layer1_0_b(x2_)
else:
w1 = b1 = None
if self.btnk[1] == 1:
w1_1 = self.layer1_1_w(x2_)
b1_1 = self.layer1_1_b(x2_)
else:
w1_1 = b1_1 = None
if self.btnk[2] == 1:
w1_2 = self.layer1_2_w(x2_)
b1_2 = self.layer1_2_b(x2_)
else:
w1_2 = b1_2 = None
if self.btnk[3] == 1:
w2_0 = self.layer2_0_w(x2_)
b2_0 = self.layer2_0_b(x2_)
else:
w2_0 = b2_0 = None
if self.btnk[4] == 1:
w2_1 = self.layer2_1_w(x2)
b2_1 = self.layer2_1_b(x2)
else:
w2_1 = b2_1 = None
if self.btnk[5] == 1:
w2_2 = self.layer2_2_w(x2)
b2_2 = self.layer2_2_b(x2)
else:
w2_2 = b2_2 = None
if self.btnk[6] == 1:
w2_3 = self.layer2_3_w(x2)
b2_3 = self.layer2_3_b(x2)
else:
w2_3 = b2_3 = None
else:
w1 = self.layer1_w(x)
b1 = self.layer1_b(x)
# w2 = self.layer2_w(x)
# b2 = self.layer2_b(x)
# w3 = self.layer3_w(x)
# b3 = self.layer3_b(x)
# w1_1 = self.layer1_1_w(x)
# b1_1 = self.layer1_1_b(x)
w1_2 = self.layer1_2_w(x)
b1_2 = self.layer1_2_b(x)
# w2_3 = self.layer2_3_w(x)
# b2_3 = self.layer2_3_b(x)
# w3_5 = self.layer3_5_w(x)
# b3_5 = self.layer3_5_b(x)
# w3_3 = self.layer3_3_w(x)
# b3_3 = self.layer3_3_b(x)
w2 = None
b2 = None
w3 = None
b3 = None
w1_1 = None
b1_1 = None
w2_3 = None
b2_3 = None
w3_5 = None
b3_5 = None
w3_3 = None
b3_3 = None
# weather classification
out_w = self.classifier(x)
if self.norm == 'spade':
return [[[w1, b1], [w1_1, b1_1], [w1_2, b1_2]], [[w2_0, b2_0], [w2_1,b2_1], [w2_2,b2_2], [w2_3, b2_3]]], out_w
return [w1, w1_1, w1_2], [b1, b1_1, b1_2], [w2, w2_3], [b2, b2_3], [w3, w3_3, w3_5], [b3, b3_3, b3_5], out_w
# Define the ResNet50-based Model
class ft_net(nn.Module):
def __init__(self, class_num, droprate=0.5, stride=2, init_model=None, pool='avg', norm='bn', adain='a'):
super(ft_net, self).__init__()
if norm == 'bn':
model_ft = models.resnet50(pretrained=True)
elif norm == 'ibn':
model_ft = torch.hub.load('XingangPan/IBN-Net', 'resnet50_ibn_a', pretrained=True)
elif norm == 'ada-ibn':
model_ft = resnet50_adaibn_a(pretrained=True, adain=adain)
# self.pt_model = Pt_ResNet50()
self.norm = norm
# 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)
self.pool = pool
if pool =='avg+max':
model_ft.avgpool2 = nn.AdaptiveAvgPool2d((1,1))
model_ft.maxpool2 = nn.AdaptiveMaxPool2d((1,1))
self.model = model_ft
#self.classifier = ClassBlock(4096, class_num, droprate)
elif pool=='avg':
model_ft.avgpool2 = nn.AdaptiveAvgPool2d((1,1))
self.model = model_ft
#self.classifier = ClassBlock(2048, class_num, droprate)
elif pool=='max':
model_ft.maxpool2 = nn.AdaptiveMaxPool2d((1,1))
self.model = model_ft
if init_model!=None:
self.model = init_model.model
self.pool = init_model.pool
#self.classifier.add_block = init_model.classifier.add_block
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)
# print(x.size())
if self.pool == 'avg+max':
x1 = self.model.avgpool2(x)
x2 = self.model.maxpool2(x)
x = torch.cat((x1,x2), dim = 1)
x = x.view(x.size(0), x.size(1))
elif self.pool == 'avg':
x = self.model.avgpool2(x)
x = x.view(x.size(0), x.size(1))
elif self.pool == 'max':
x = self.model.maxpool2(x)
x = x.view(x.size(0), x.size(1))
#x = self.classifier(x)
return x
class ft_net_spade(nn.Module):
def __init__(self, class_num, droprate=0.5, stride=2, init_model=None, pool='avg', norm='bn', adain='a'):
super(ft_net_spade, self).__init__()
if norm == 'bn':
model_ft = models.resnet50(pretrained=True)
elif norm == 'ibn':
model_ft = torch.hub.load('XingangPan/IBN-Net', 'resnet50_ibn_a', pretrained=True)
elif norm == 'ada-ibn':
model_ft = resnet50_adaibn_a(pretrained=True, adain=adain)
# self.pt_model = Pt_ResNet50()
elif norm == 'spade':
model_ft = resnet50_adaibn_a(pretrained=True, adain=adain)
self.norm = norm
# 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)
self.pool = pool
if pool =='avg+max':
model_ft.avgpool2 = nn.AdaptiveAvgPool2d((1,1))
model_ft.maxpool2 = nn.AdaptiveMaxPool2d((1,1))
self.model = model_ft
#self.classifier = ClassBlock(4096, class_num, droprate)
elif pool=='avg':
model_ft.avgpool2 = nn.AdaptiveAvgPool2d((1,1))
self.model = model_ft
#self.classifier = ClassBlock(2048, class_num, droprate)
elif pool=='max':
model_ft.maxpool2 = nn.AdaptiveMaxPool2d((1,1))
self.model = model_ft
if init_model!=None:
self.model = init_model.model
self.pool = init_model.pool
#self.classifier.add_block = init_model.classifier.add_block
def forward(self, x, mod_f):
x = self.model.conv1(x)
x = self.model.bn1(x)
x = self.model.relu(x)
x = self.model.maxpool(x)
if self.norm == 'spade':
# print('---------------- spade norm-----------------------')
x = extract_spade_feature(self.model.layer1, x, mod_f[0])
else:
x = self.model.layer1(x)
if self.norm == 'spade':
x = extract_spade_feature(self.model.layer2, x, mod_f[1])
else:
x = self.model.layer2(x)
x = self.model.layer3(x)
x = self.model.layer4(x)
# print(x.size())
if self.pool == 'avg+max':
x1 = self.model.avgpool2(x)
x2 = self.model.maxpool2(x)
x = torch.cat((x1,x2), dim = 1)
x = x.view(x.size(0), x.size(1))
elif self.pool == 'avg':
x = self.model.avgpool2(x)
x = x.view(x.size(0), x.size(1))
elif self.pool == 'max':
x = self.model.maxpool2(x)
x = x.view(x.size(0), x.size(1))
#x = self.classifier(x)
return x
# LPN + Spade
class ft_net_LPN_Spade(nn.Module):
def __init__(self, class_num, droprate=0.5, stride=2, init_model=None, pool='avg', block=6):
super(ft_net_LPN_Spade, self).__init__()
model_ft = resnet50_adaibn_a(pretrained=True, adain='a')
# 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)
self.pool = pool
self.model = model_ft
self.block = block
if init_model!=None:
self.model = init_model.model
self.pool = init_model.pool
#self.classifier.add_block = init_model.classifier.add_block
def forward(self, x, mod_f):
x = self.model.conv1(x)
x = self.model.bn1(x)
x = self.model.relu(x)
x = self.model.maxpool(x)
x = extract_spade_feature(self.model.layer1, x, mod_f[0])
# x = self.model.layer1(x)
x = self.model.layer2(x)
x = self.model.layer3(x)
x = self.model.layer4(x)
# print(x.shape)
if self.pool == 'avg+max':
x1 = self.get_part_pool(x, pool='avg')
x2 = self.get_part_pool(x, pool='max')
x = torch.cat((x1,x2), dim = 1)
x = x.view(x.size(0), x.size(1), -1)
elif self.pool == 'avg':
x = self.get_part_pool(x)
x = x.view(x.size(0), x.size(1), -1)
elif self.pool == 'max':
x = self.get_part_pool(x, pool='max')
x = x.view(x.size(0), x.size(1), -1)
#x = self.classifier(x)
return x
def get_part_pool(self, x, pool='avg', no_overlap=True):
result = []
if pool == 'avg':
pooling = torch.nn.AdaptiveAvgPool2d((1,1))
elif pool == 'max':
pooling = torch.nn.AdaptiveMaxPool2d((1,1))
H, W = x.size(2), x.size(3)
c_h, c_w = int(H/2), int(W/2)
per_h, per_w = H/(2*self.block),W/(2*self.block)
if per_h < 1 and per_w < 1:
new_H, new_W = H+(self.block-c_h)*2, W+(self.block-c_w)*2
x = nn.functional.interpolate(x, size=[new_H,new_W], mode='bilinear', align_corners=True)
H, W = x.size(2), x.size(3)
c_h, c_w = int(H/2), int(W/2)
per_h, per_w = H/(2*self.block),W/(2*self.block)
per_h, per_w = math.floor(per_h), math.floor(per_w)
for i in range(self.block):
i = i + 1
if i < self.block:
x_curr = x[:,:,(c_h-i*per_h):(c_h+i*per_h),(c_w-i*per_w):(c_w+i*per_w)]
if no_overlap and i > 1:
x_pre = x[:,:,(c_h-(i-1)*per_h):(c_h+(i-1)*per_h),(c_w-(i-1)*per_w):(c_w+(i-1)*per_w)]
x_pad = F.pad(x_pre,(per_h,per_h,per_w,per_w),"constant",0)
x_curr = x_curr - x_pad
avgpool = pooling(x_curr)
result.append(avgpool)
else:
if no_overlap and i > 1:
x_pre = x[:,:,(c_h-(i-1)*per_h):(c_h+(i-1)*per_h),(c_w-(i-1)*per_w):(c_w+(i-1)*per_w)]
pad_h = c_h-(i-1)*per_h
pad_w = c_w-(i-1)*per_w
# x_pad = F.pad(x_pre,(pad_h,pad_h,pad_w,pad_w),"constant",0)
if x_pre.size(2)+2*pad_h == H:
x_pad = F.pad(x_pre,(pad_h,pad_h,pad_w,pad_w),"constant",0)
else:
ep = H - (x_pre.size(2)+2*pad_h)
x_pad = F.pad(x_pre,(pad_h+ep,pad_h,pad_w+ep,pad_w),"constant",0)
x = x - x_pad
avgpool = pooling(x)
result.append(avgpool)
return torch.cat(result, dim=2)
# Define the ResNet50-based part Model
class ft_net_LPN(nn.Module):
def __init__(self, class_num, droprate=0.5, stride=2, init_model=None, pool='avg', block=6):
super(ft_net_LPN, self).__init__()
model_ft = models.resnet50(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)
self.pool = pool
self.model = model_ft
self.block = block
if init_model!=None:
self.model = init_model.model
self.pool = init_model.pool
#self.classifier.add_block = init_model.classifier.add_block
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)
# print(x.shape)
if self.pool == 'avg+max':
x1 = self.get_part_pool(x, pool='avg')
x2 = self.get_part_pool(x, pool='max')
x = torch.cat((x1,x2), dim = 1)
x = x.view(x.size(0), x.size(1), -1)
elif self.pool == 'avg':
x = self.get_part_pool(x)
x = x.view(x.size(0), x.size(1), -1)
elif self.pool == 'max':
x = self.get_part_pool(x, pool='max')
x = x.view(x.size(0), x.size(1), -1)
#x = self.classifier(x)
return x
def get_part_pool(self, x, pool='avg', no_overlap=True):
result = []
if pool == 'avg':
pooling = torch.nn.AdaptiveAvgPool2d((1,1))
elif pool == 'max':
pooling = torch.nn.AdaptiveMaxPool2d((1,1))
H, W = x.size(2), x.size(3)
c_h, c_w = int(H/2), int(W/2)
per_h, per_w = H/(2*self.block),W/(2*self.block)
if per_h < 1 and per_w < 1:
new_H, new_W = H+(self.block-c_h)*2, W+(self.block-c_w)*2
x = nn.functional.interpolate(x, size=[new_H,new_W], mode='bilinear', align_corners=True)
H, W = x.size(2), x.size(3)
c_h, c_w = int(H/2), int(W/2)
per_h, per_w = H/(2*self.block),W/(2*self.block)
per_h, per_w = math.floor(per_h), math.floor(per_w)
for i in range(self.block):
i = i + 1
if i < self.block:
x_curr = x[:,:,(c_h-i*per_h):(c_h+i*per_h),(c_w-i*per_w):(c_w+i*per_w)]
if no_overlap and i > 1:
x_pre = x[:,:,(c_h-(i-1)*per_h):(c_h+(i-1)*per_h),(c_w-(i-1)*per_w):(c_w+(i-1)*per_w)]
x_pad = F.pad(x_pre,(per_h,per_h,per_w,per_w),"constant",0)
x_curr = x_curr - x_pad
avgpool = pooling(x_curr)
result.append(avgpool)
else:
if no_overlap and i > 1:
x_pre = x[:,:,(c_h-(i-1)*per_h):(c_h+(i-1)*per_h),(c_w-(i-1)*per_w):(c_w+(i-1)*per_w)]
pad_h = c_h-(i-1)*per_h
pad_w = c_w-(i-1)*per_w
# x_pad = F.pad(x_pre,(pad_h,pad_h,pad_w,pad_w),"constant",0)
if x_pre.size(2)+2*pad_h == H:
x_pad = F.pad(x_pre,(pad_h,pad_h,pad_w,pad_w),"constant",0)
else:
ep = H - (x_pre.size(2)+2*pad_h)
x_pad = F.pad(x_pre,(pad_h+ep,pad_h,pad_w+ep,pad_w),"constant",0)
x = x - x_pad
avgpool = pooling(x)
result.append(avgpool)
return torch.cat(result, dim=2)
class ft_net_VGG16(nn.Module):
def __init__(self, class_num, droprate=0.5, stride=2, init_model=None, pool='avg'):
super(ft_net_VGG16, self).__init__()
model_ft = models.vgg16_bn(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)
self.pool = pool
if pool =='avg+max':
model_ft.avgpool2 = nn.AdaptiveAvgPool2d((1,1))
model_ft.maxpool2 = nn.AdaptiveMaxPool2d((1,1))
self.model = model_ft
#self.classifier = ClassBlock(4096, class_num, droprate)
elif pool=='avg':
model_ft.avgpool2 = nn.AdaptiveAvgPool2d((1,1))
self.model = model_ft
#self.classifier = ClassBlock(2048, class_num, droprate)
elif pool=='max':
model_ft.maxpool2 = nn.AdaptiveMaxPool2d((1,1))
self.model = model_ft
if init_model!=None:
self.model = init_model.model
self.pool = init_model.pool
#self.classifier.add_block = init_model.classifier.add_block
def forward(self, x):
x = self.model.features(x)
if self.pool == 'avg+max':
x1 = self.model.avgpool2(x)
x2 = self.model.maxpool2(x)
x = torch.cat((x1,x2), dim = 1)
x = x.view(x.size(0), x.size(1))
elif self.pool == 'avg':
x = self.model.avgpool2(x)
x = x.view(x.size(0), x.size(1))
elif self.pool == 'max':
x = self.model.maxpool2(x)
x = x.view(x.size(0), x.size(1))
#x = self.classifier(x)
return x
class ft_net_dense(nn.Module):
def __init__(self, class_num, droprate=0.5, 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()
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))
return x
class ft_net_ResNet101(nn.Module):
def __init__(self, class_num, droprate=0.5, stride=2, init_model=None, pool='avg', norm='bn', adain='a'):
super(ft_net_ResNet101, self).__init__()
model_ft = models.resnet101(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)
self.pool = pool
if pool =='avg+max':
model_ft.avgpool2 = nn.AdaptiveAvgPool2d((1,1))
model_ft.maxpool2 = nn.AdaptiveMaxPool2d((1,1))
self.model = model_ft
#self.classifier = ClassBlock(4096, class_num, droprate)
elif pool=='avg':
model_ft.avgpool2 = nn.AdaptiveAvgPool2d((1,1))
self.model = model_ft
#self.classifier = ClassBlock(2048, class_num, droprate)
elif pool=='max':
model_ft.maxpool2 = nn.AdaptiveMaxPool2d((1,1))
self.model = model_ft
if init_model!=None:
self.model = init_model.model
self.pool = init_model.pool
#self.classifier.add_block = init_model.classifier.add_block
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)
# print(x.size())
if self.pool == 'avg+max':
x1 = self.model.avgpool2(x)
x2 = self.model.maxpool2(x)
x = torch.cat((x1,x2), dim = 1)
x = x.view(x.size(0), x.size(1))
elif self.pool == 'avg':
x = self.model.avgpool2(x)
x = x.view(x.size(0), x.size(1))
elif self.pool == 'max':
x = self.model.maxpool2(x)
x = x.view(x.size(0), x.size(1))
#x = self.classifier(x)
return x
# For cvusa/cvact
class two_view_net(nn.Module):
def __init__(self, class_num, droprate, stride = 1, pool = 'avg', share_weight = False, VGG16=False, LPN=False, block=2, norm='bn', adain='a', btnk=[1,0,1]):
super(two_view_net, self).__init__()
self.LPN = LPN
self.block = block
self.norm = norm
self.adain = adain
self.sqr = True # if the satellite image is square ring partition and the ground image is row partition, self.sqr is True. Otherwise it is False.
if VGG16:
if LPN:
# satelite
self.model_1 = ft_net_VGG16_LPN(class_num, stride=stride, pool=pool, block = block)
if self.sqr:
self.model_1 = ft_net_VGG16_LPN_R(class_num, stride=stride, pool=pool, block=block)
else:
self.model_1 = ft_net_VGG16(class_num, stride=stride, pool = pool)
# self.vgg1 = models.vgg16_bn(pretrained=True)
# self.model_1 = SAFA()
# self.model_1 = SAFA_FC(64, 32, 8)
else:
#resnet50 LPN cvusa/cvact
if LPN:
self.model_1 = ft_net_cvusa_LPN(class_num, stride=stride, pool = pool, block=block)
if self.sqr:
self.model_1 = ft_net_cvusa_LPN_R(class_num, stride=stride, pool=pool, block=block)
self.block = self.model_1.block
else:
if norm == 'spade':
self.model_1 = ft_net_spade(class_num, stride=stride, pool=pool, norm=norm, adain=adain)
else:
self.model_1 = ft_net(class_num, stride = stride, pool = pool, norm=norm, adain=adain)
if share_weight:
print('------------------share weight-----------------------')
self.model_2 = self.model_1
else:
if VGG16:
if LPN:
#street
self.model_2 = ft_net_VGG16_LPN(class_num, stride=stride, pool=pool, block = block, row = self.sqr)
else:
self.model_2 = ft_net_VGG16(class_num, stride = stride, pool = pool)
# self.vgg2 = models.vgg16_bn(pretrained=True)
# self.model_2 = SAFA()
# self.model_2 = SAFA_FC(64, 32, 8)
else:
if LPN:
self.model_2 = ft_net_cvusa_LPN(class_num, stride = stride, pool = pool, block=block, row = self.sqr)
else:
self.model_2 = ft_net(class_num, stride = stride, pool = pool, norm=norm, adain=adain)
if LPN:
if VGG16:
if pool == 'avg+max':
for i in range(self.block):
name = 'classifier'+str(i)
setattr(self, name, ClassBlock(1024, class_num, droprate))
else:
for i in range(self.block):
name = 'classifier'+str(i)
setattr(self, name, ClassBlock(512, class_num, droprate))
else:
if pool == 'avg+max':
for i in range(self.block):
name = 'classifier'+str(i)
setattr(self, name, ClassBlock(4096, class_num, droprate))
else:
for i in range(self.block):
name = 'classifier'+str(i)
setattr(self, name, ClassBlock(2048, class_num, droprate))
else:
self.classifier = ClassBlock(2048, class_num, droprate)
if pool =='avg+max':
self.classifier = ClassBlock(4096, class_num, droprate)
if VGG16:
self.classifier = ClassBlock(512, class_num, droprate)
# self.classifier = ClassBlock(4096, class_num, droprate, num_bottleneck=512) #safa 情况下
if pool =='avg+max':
self.classifier = ClassBlock(1024, class_num, droprate)
if norm == 'ada-ibn' or norm == 'spade':
self.pt_model = Pt_ResNet50(norm=norm, init_mode=['normal'], btnk=btnk)
self._w1, self._b1, self._w2, self._b2, self._w3, self._b3 = pretrained_in_weight(True)
def forward(self, x1, x2):
if self.LPN:
if x1 is None:
y1 = None
else:
x1 = self.model_1(x1)
y1 = self.part_classifier(x1)
if x2 is None:
y2 = None
else:
x2 = self.model_2(x2)
y2 = self.part_classifier(x2)
else:
if x1 is None:
y1 = None
else:
if self.norm == 'ada-ibn':
sw1, sb1, sw2, sb2, sw3, sb3, sout_w = self.pt_model(x1)
if self.adain == 'a':
assign_adain_params(sw1[0] + self._w1[0], sb1[0] + self._b1[0], self.model_1.model.layer1[0], 32) # set layer1's ada-ibn
# assign_adain_params(sw1[1] + self._w1[1], sb1[1] + self._b1[1], self.model_1.model.layer1[1], 32)
assign_adain_params(sw1[2] + self._w1[2], sb1[2] + self._b1[2], self.model_1.model.layer1[2], 32)
# assign_adain_params(sw2[0] + self._w2[0], sb2[0] + self._b2[0], self.model_1.model.layer2[0], 64) # set layer2's ada-ibn
# assign_adain_params(sw2[1] + self._w2[1], sb2[1] + self._b2[1], self.model_1.model.layer2[3], 64)
# assign_adain_params(sw3[0] + self._w3[0], sb3[0] + self._b3[0], self.model_1.model.layer3[0], 128) # set layer3's ada-ibn
# assign_adain_params(sw3[1] + self._w3[1], sb3[1] + self._b3[1], self.model_1.model.layer3[3], 128)
# assign_adain_params(sw3[2] + self._w3[2], sb3[2] + self._b3[2], self.model_1.model.layer3[5], 128)
# x1 = self.vgg1.features(x1)
x1 = self.model_1(x1)
elif self.norm == 'spade':
smod_f, sout_w = self.pt_model(x1)
x1 = self.model_1(x1, smod_f)
else:
x1 = self.model_1(x1)
y1 = self.classifier(x1)
if x2 is None:
y2 = None
else:
if self.norm == 'ada-ibn':
gw1, gb1, gw2, gb2, gw3, gb3, gout_w = self.pt_model(x2)
if self.adain == 'a':
assign_adain_params(gw1[0] + self._w1[0], gb1[0] + self._b1[0], self.model_2.model.layer1[0], 32) # set layer1's ada-ibn
# assign_adain_params(gw1[1] + self._w1[1], gb1[1] + self._b1[1], self.model_2.model.layer1[1], 32)
assign_adain_params(gw1[2] + self._w1[2], gb1[2] + self._b1[2], self.model_2.model.layer1[2], 32)
# assign_adain_params(gw2[0] + self._w2[0], gb2[0] + self._b2[0], self.model_2.model.layer2[0], 64) # set layer2's ada-ibn
# assign_adain_params(gw2[1] + self._w2[1], gb2[1] + self._b2[1], self.model_2.model.layer2[3], 64)
# assign_adain_params(gw3[0] + self._w3[0], gb3[0] + self._b3[0], self.model_2.model.layer3[0], 128) # set layer3's ada-ibn
# assign_adain_params(dw3[1] + self._w3[1], db3[1] + self._b3[1], self.model_3.model.layer3[3], 128)
# assign_adain_params(gw3[2] + self._w3[2], gb3[2] + self._b3[2], self.model_2.model.layer3[5], 128)
# x2 = self.vgg2.features(x2)
x2 = self.model_2(x2)
elif self.norm == 'spade':
gmod_f, gout_w = self.pt_model(x2)
x2 = self.model_2(x2, gmod_f)
else:
x2 = self.model_2(x2)
y2 = self.classifier(x2)
if self.norm == 'ada-ibn' or self.norm == 'spade':
if not self.training:
return y1, y2
else:
return y1, y2, sout_w, gout_w
return y1, y2