-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmgn.py
130 lines (102 loc) · 4.55 KB
/
mgn.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
import copy
import torch
import torch.nn as nn
from opt import opt
from torchvision.models.resnet import resnet50, resnet101, Bottleneck
class MGN(nn.Module):
def __init__(self):
super(MGN, self).__init__()
num_classes = 751
feats = 256
if opt.backbone == 'resnet50':
resnet = resnet50(pretrained=True)
elif opt.backbone == 'resnet101':
resnet = resnet101(pretrained=True)
self.backbone = nn.Sequential(
resnet.conv1,
resnet.bn1,
resnet.relu,
resnet.maxpool,
resnet.layer1,
resnet.layer2,
resnet.layer3[0],
)
res_conv4 = nn.Sequential(*resnet.layer3[1:])
res_g_conv5 = resnet.layer4
res_p_conv5 = nn.Sequential(
Bottleneck(1024, 512, downsample=nn.Sequential(nn.Conv2d(1024, 2048, 1, bias=False), nn.BatchNorm2d(2048))),
Bottleneck(2048, 512),
Bottleneck(2048, 512))
res_p_conv5.load_state_dict(resnet.layer4.state_dict())
self.p1 = nn.Sequential(copy.deepcopy(res_conv4), copy.deepcopy(res_g_conv5))
self.p2 = nn.Sequential(copy.deepcopy(res_conv4), copy.deepcopy(res_p_conv5))
self.p3 = nn.Sequential(copy.deepcopy(res_conv4), copy.deepcopy(res_p_conv5))
self.maxpool_zg_p1 = nn.MaxPool2d(kernel_size=(12, 4))
self.maxpool_zg_p2 = nn.MaxPool2d(kernel_size=(24, 8))
self.maxpool_zg_p3 = nn.MaxPool2d(kernel_size=(24, 8))
self.maxpool_zp2 = nn.MaxPool2d(kernel_size=(12, 8))
self.maxpool_zp3 = nn.MaxPool2d(kernel_size=(8, 8))
self.reduction = nn.Sequential(nn.Conv2d(2048, feats, 1, bias=False), nn.BatchNorm2d(feats), nn.ReLU())
self._init_reduction(self.reduction)
self.fc_id_2048_0 = nn.Linear(feats, num_classes)
self.fc_id_2048_1 = nn.Linear(feats, num_classes)
self.fc_id_2048_2 = nn.Linear(feats, num_classes)
self.fc_id_256_1_0 = nn.Linear(feats, num_classes)
self.fc_id_256_1_1 = nn.Linear(feats, num_classes)
self.fc_id_256_2_0 = nn.Linear(feats, num_classes)
self.fc_id_256_2_1 = nn.Linear(feats, num_classes)
self.fc_id_256_2_2 = nn.Linear(feats, num_classes)
self._init_fc(self.fc_id_2048_0)
self._init_fc(self.fc_id_2048_1)
self._init_fc(self.fc_id_2048_2)
self._init_fc(self.fc_id_256_1_0)
self._init_fc(self.fc_id_256_1_1)
self._init_fc(self.fc_id_256_2_0)
self._init_fc(self.fc_id_256_2_1)
self._init_fc(self.fc_id_256_2_2)
@staticmethod
def _init_reduction(reduction):
# conv
nn.init.kaiming_normal_(reduction[0].weight, mode='fan_in')
# nn.init.constant_(reduction[0].bias, 0.)
# bn
nn.init.normal_(reduction[1].weight, mean=1., std=0.02)
nn.init.constant_(reduction[1].bias, 0.)
@staticmethod
def _init_fc(fc):
nn.init.kaiming_normal_(fc.weight, mode='fan_out')
# nn.init.normal_(fc.weight, std=0.001)
nn.init.constant_(fc.bias, 0.)
def forward(self, x):
x = self.backbone(x)
p1 = self.p1(x)
p2 = self.p2(x)
p3 = self.p3(x)
zg_p1 = self.maxpool_zg_p1(p1)
zg_p2 = self.maxpool_zg_p2(p2)
zg_p3 = self.maxpool_zg_p3(p3)
zp2 = self.maxpool_zp2(p2)
z0_p2 = zp2[:, :, 0:1, :]
z1_p2 = zp2[:, :, 1:2, :]
zp3 = self.maxpool_zp3(p3)
z0_p3 = zp3[:, :, 0:1, :]
z1_p3 = zp3[:, :, 1:2, :]
z2_p3 = zp3[:, :, 2:3, :]
fg_p1 = self.reduction(zg_p1).squeeze(dim=3).squeeze(dim=2)
fg_p2 = self.reduction(zg_p2).squeeze(dim=3).squeeze(dim=2)
fg_p3 = self.reduction(zg_p3).squeeze(dim=3).squeeze(dim=2)
f0_p2 = self.reduction(z0_p2).squeeze(dim=3).squeeze(dim=2)
f1_p2 = self.reduction(z1_p2).squeeze(dim=3).squeeze(dim=2)
f0_p3 = self.reduction(z0_p3).squeeze(dim=3).squeeze(dim=2)
f1_p3 = self.reduction(z1_p3).squeeze(dim=3).squeeze(dim=2)
f2_p3 = self.reduction(z2_p3).squeeze(dim=3).squeeze(dim=2)
l_p1 = self.fc_id_2048_0(fg_p1)
l_p2 = self.fc_id_2048_1(fg_p2)
l_p3 = self.fc_id_2048_2(fg_p3)
l0_p2 = self.fc_id_256_1_0(f0_p2)
l1_p2 = self.fc_id_256_1_1(f1_p2)
l0_p3 = self.fc_id_256_2_0(f0_p3)
l1_p3 = self.fc_id_256_2_1(f1_p3)
l2_p3 = self.fc_id_256_2_2(f2_p3)
predict = torch.cat([fg_p1, fg_p2, fg_p3, f0_p2, f1_p2, f0_p3, f1_p3, f2_p3], dim=1)
return predict, fg_p1, fg_p2, fg_p3, l_p1, l_p2, l_p3, l0_p2, l1_p2, l0_p3, l1_p3, l2_p3