-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathbackbone.py
179 lines (160 loc) · 7.35 KB
/
backbone.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
import torch.nn as nn
class ResNet12Block(nn.Module):
"""
ResNet Block
"""
def __init__(self, inplanes, planes):
super(ResNet12Block, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1,
padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes)
self.relu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
self.conv = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn = nn.BatchNorm2d(planes)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
def forward(self, x):
residual = x
residual = self.conv(residual)
residual = self.bn(residual)
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
out += residual
out = self.relu(out)
out = self.maxpool(out)
return out
class ResNet12(nn.Module):
"""
ResNet12 Backbone
"""
def __init__(self, emb_size, block=ResNet12Block, cifar_flag=False):
super(ResNet12, self).__init__()
cfg = [64, 128, 256, 512]
# layers = [1, 1, 1, 1]
iChannels = int(cfg[0])
self.conv1 = nn.Conv2d(3, iChannels, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(iChannels)
self.relu = nn.LeakyReLU()
self.emb_size = emb_size
self.layer1 = self._make_layer(block, cfg[0], cfg[0])
self.layer2 = self._make_layer(block, cfg[0], cfg[1])
self.layer3 = self._make_layer(block, cfg[1], cfg[2])
self.layer4 = self._make_layer(block, cfg[2], cfg[3])
self.avgpool = nn.AvgPool2d(7)
self.maxpool = nn.MaxPool2d(kernel_size=2)
layer_second_in_feat = cfg[2] * 5 * 5 if not cifar_flag else cfg[2] * 2 * 2
self.layer_second = nn.Sequential(nn.Linear(in_features=layer_second_in_feat,
out_features=self.emb_size,
bias=True),
nn.BatchNorm1d(self.emb_size))
self.layer_last = nn.Sequential(nn.Linear(in_features=cfg[3],
out_features=self.emb_size,
bias=True),
nn.BatchNorm1d(self.emb_size))
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='leaky_relu')
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def _make_layer(self, block, inplanes, planes):
layers = []
layers.append(block(inplanes, planes))
return nn.Sequential(*layers)
def forward(self, x):
# 3 -> 64
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
# 64 -> 64
x = self.layer1(x)
# 64 -> 128
x = self.layer2(x)
# 128 -> 256
inter = self.layer3(x)
# 256 -> 512
x = self.layer4(inter)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
# 512 -> 128
x = self.layer_last(x)
inter = self.maxpool(inter)
# 256 * 5 * 5
inter = inter.view(inter.size(0), -1)
# 256 * 5 * 5 -> 128
inter = self.layer_second(inter)
out = []
out.append(x)
out.append(inter)
# no FC here
return out
class ConvNet(nn.Module):
"""
Conv4 Backbone
"""
def __init__(self, emb_size, cifar_flag=False):
super(ConvNet, self).__init__()
# set size
self.hidden = 128
self.last_hidden = self.hidden * 25 if not cifar_flag else self.hidden
self.emb_size = emb_size
# set layers
self.conv_1 = nn.Sequential(nn.Conv2d(in_channels=3,
out_channels=self.hidden,
kernel_size=3,
padding=1,
bias=False),
nn.BatchNorm2d(num_features=self.hidden),
nn.MaxPool2d(kernel_size=2),
nn.LeakyReLU(negative_slope=0.2, inplace=True))
self.conv_2 = nn.Sequential(nn.Conv2d(in_channels=self.hidden,
out_channels=int(self.hidden*1.5),
kernel_size=3,
bias=False),
nn.BatchNorm2d(num_features=int(self.hidden*1.5)),
nn.MaxPool2d(kernel_size=2),
nn.LeakyReLU(negative_slope=0.2, inplace=True))
self.conv_3 = nn.Sequential(nn.Conv2d(in_channels=int(self.hidden*1.5),
out_channels=self.hidden*2,
kernel_size=3,
padding=1,
bias=False),
nn.BatchNorm2d(num_features=self.hidden * 2),
nn.MaxPool2d(kernel_size=2),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Dropout2d(0.4))
self.max = nn.MaxPool2d(kernel_size=2)
self.layer_second = nn.Sequential(nn.Linear(in_features=self.last_hidden * 2,
out_features=self.emb_size, bias=True),
nn.BatchNorm1d(self.emb_size))
self.conv_4 = nn.Sequential(nn.Conv2d(in_channels=self.hidden*2,
out_channels=self.hidden*4,
kernel_size=3,
padding=1,
bias=False),
nn.BatchNorm2d(num_features=self.hidden * 4),
nn.MaxPool2d(kernel_size=2),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Dropout2d(0.5))
self.layer_last = nn.Sequential(nn.Linear(in_features=self.last_hidden * 4,
out_features=self.emb_size, bias=True),
nn.BatchNorm1d(self.emb_size))
def forward(self, input_data):
out_1 = self.conv_1(input_data)
out_2 = self.conv_2(out_1)
out_3 = self.conv_3(out_2)
output_data = self.conv_4(out_3)
output_data0 = self.max(out_3)
out = []
out.append(self.layer_last(output_data.view(output_data.size(0), -1)))
out.append(self.layer_second(output_data0.view(output_data0.size(0), -1)))
return out