-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.py
executable file
·299 lines (263 loc) · 11.2 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
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
'''ResNet in PyTorch.
For Pre-activation ResNet, see 'preact_resnet.py'.
Reference:
[1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
Deep Residual Learning for Image Recognition. arXiv:1512.03385
'''
import torch
import torch.nn as nn
import torch.nn.functional as F
class BasicBlock(nn.Module):
expansion = 1
# basic block 34-layer config. two layers per block with 3*3 kernel size
def __init__(self, in_planes, planes, stride=1):
super(BasicBlock, self).__init__()
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)#stride = 2
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)# stride = 1
self.bn2 = nn.BatchNorm2d(planes)
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != self.expansion*planes:
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False), #stride = 2, 64-->128
nn.BatchNorm2d(self.expansion*planes) # 1*1 convolution for match dimension
)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out += self.shortcut(x)
out = F.relu(out)
return out
class Block(nn.Module):
def __init__(self, channels):
super().__init__()
self.conv1 = nn.Conv2d(channels, channels, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(channels)
self.prelu1 = nn.PReLU(channels)
self.conv2 = nn.Conv2d(channels, channels, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(channels)
self.prelu2 = nn.PReLU(channels)
def forward(self, x):
short_cut = x
x = self.conv1(x)
x = self.bn1(x)
x = self.prelu1(x)
x = self.conv2(x)
x = self.bn2(x)
x = self.prelu2(x)
return x + short_cut
class resnet20_hashing(nn.Module):
"""
ONLY ONE FULLY CONNECTED LAYER FOLLOWED BY THE BOTTLENECK
clf: if classification loss is returned, for ddqh_loss
"""
def __init__(self, num_layers=64, hashing_bits=48, tanh=False, clf=None, size=7):
super().__init__()
assert num_layers in [20, 64], 'spherenet num_layers should be 20 or 64'
if num_layers == 20:
layers = [1, 2, 4, 1]
elif num_layers == 64:
layers = [3, 8, 16, 3]
else:
raise ValueError('sphere' + str(num_layers) + "is not supported!")
filter_list = [3, 64, 128, 256, 512]
if size == 7:
stride_list = [2, 2, 2, 2]
else:
stride_list = [1, 2, 2, 2]
self.bn0 = nn.BatchNorm2d(filter_list[1])
block = Block
self.clf = clf
self.hashing_bits = hashing_bits
self.layer1 = self._make_layer(block, filter_list[0], filter_list[1], layers[0], stride=stride_list[0])
self.layer2 = self._make_layer(block, filter_list[1], filter_list[2], layers[1], stride=stride_list[1])
self.layer3 = self._make_layer(block, filter_list[2], filter_list[3], layers[2], stride=stride_list[2])
self.layer4 = self._make_layer(block, filter_list[3], filter_list[4], layers[3], stride=stride_list[3])
self.fc = nn.Linear(512*size*size, 512)
self.bn = nn.BatchNorm1d(512)
self.logits = nn.Linear(512, self.hashing_bits)
self.bn_last = nn.BatchNorm1d(self.hashing_bits)
self.drop = nn.Dropout()
self.tanh = tanh
if self.tanh:
self.tanh_act = nn.Tanh()
if self.clf is not None:
self.classifier = nn.Sequential(
# nn.Tanh(),
nn.Linear(self.hashing_bits, self.clf), # (n, class_num)
nn.LogSoftmax(dim=1) # log(softmax(x)) function
)
def _make_layer(self, block, inplanes, planes, num_units, stride):
layers = []
layers.append(nn.Conv2d(inplanes, planes, 3, stride, 1))
layers.append(nn.BatchNorm2d(planes))
layers.append(nn.PReLU(planes))
for i in range(num_units):
layers.append(block(planes))
return nn.Sequential(*layers)
def forward(self, x):
x = self.layer1(x)
x = self.bn0(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = x.view(x.size(0), -1)
x = self.drop(x)
x = self.fc(x)
x = self.bn(x)
# x = self.drop(x)
x = self.logits(x)
out = self.bn_last(x)
if self.tanh:
out = self.tanh_act(out)
if self.clf:
clf_x = self.classifier(out)
return out, clf_x
return out
class resnet20_pq(nn.Module):
def __init__(self, num_layers=20, feature_dim=512, channel_max=512, size=7): # size = 4 for 32*32 size dataset
super().__init__()
assert num_layers in [20, 64], 'spherenet num_layers should be 20 or 64'
if num_layers == 20:
layers = [1, 2, 4, 1]
elif num_layers == 64:
layers = [3, 8, 16, 3]
else:
raise ValueError('sphere' + str(num_layers) + "is not supported!")
if channel_max == 512:
filter_list = [3, 64, 128, 256, 512]
if size == 7:
stride_list = [2, 2, 2, 2]
else:
stride_list = [1, 2, 2, 2]
else:
filter_list = [3, 16, 32, 64, 128]
stride_list = [1, 2, 2, 2]
block = Block
self.feature_dim = feature_dim
self.layer1 = self._make_layer(block, filter_list[0], filter_list[1], layers[0], stride=stride_list[0])
self.layer2 = self._make_layer(block, filter_list[1], filter_list[2], layers[1], stride=stride_list[1])
self.layer3 = self._make_layer(block, filter_list[2], filter_list[3], layers[2], stride=stride_list[2])
self.layer4 = self._make_layer(block, filter_list[3], filter_list[4], layers[3], stride=stride_list[3])
self.bn = nn.BatchNorm1d(channel_max*size*size)
self.fc = nn.Linear(channel_max*size*size, self.feature_dim)
self.last_bn = nn.BatchNorm1d(self.feature_dim)
self.drop = nn.Dropout()
def _make_layer(self, block, inplanes, planes, num_units, stride):
layers = []
layers.append(nn.Conv2d(inplanes, planes, 3, stride, 1))
layers.append(nn.BatchNorm2d(planes))
layers.append(nn.PReLU(planes))
for i in range(num_units):
layers.append(block(planes))
return nn.Sequential(*layers)
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = x.view(x.size(0), -1)
x = self.bn(x)
x = self.drop(x)
x = self.fc(x)
out = self.last_bn(x)
return out
class SphereNet20_pq(nn.Module):
"""
MODIFIED VERSION OF ABOVE TO HANDLE LARGE INPUT SIZE DATASET
ONLY ONE FULLY CONNECTED LAYER FOLLOWED BY THE BOTTLENECK
"""
def __init__(self, num_layers=64, feature_dim=512):
super().__init__()
assert num_layers in [20, 64], 'spherenet num_layers should be 20 or 64'
if num_layers == 20:
layers = [1, 2, 4, 1]
elif num_layers == 64:
layers = [3, 8, 16, 3]
else:
raise ValueError('sphere' + str(num_layers) + "is not supported!")
filter_list = [3, 64, 128, 256, 512]
block = Block
self.feature_dim = feature_dim
self.layer1 = self._make_layer(block, filter_list[0], filter_list[1], layers[0], stride=2)
self.layer2 = self._make_layer(block, filter_list[1], filter_list[2], layers[1], stride=2)
self.layer3 = self._make_layer(block, filter_list[2], filter_list[3], layers[2], stride=2)
self.layer4 = self._make_layer(block, filter_list[3], filter_list[4], layers[3], stride=2)
self.bn = nn.BatchNorm1d(512*7*7)
self.fc = nn.Linear(512*7*7, self.feature_dim)
self.last_bn = nn.BatchNorm1d(self.feature_dim)
self.drop = nn.Dropout()
def _make_layer(self, block, inplanes, planes, num_units, stride):
layers = []
layers.append(nn.Conv2d(inplanes, planes, 3, stride, 1))
layers.append(nn.BatchNorm2d(planes))
layers.append(nn.PReLU(planes))
for i in range(num_units):
layers.append(block(planes))
return nn.Sequential(*layers)
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = x.view(x.size(0), -1)
x = self.bn(x)
x = self.drop(x)
x = self.fc(x)
# x = self.drop(x)
out = self.last_bn(x)
return out
class ResNet_q(nn.Module):
"""
Input size: 32 * 32
"""
def __init__(self, block, num_blocks, num_seg, split=False, feature_dim=512):
super(ResNet_q, self).__init__()
self.feature_dim = feature_dim
self.in_planes = 64 # always starts from 64 here
self.split = split
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1) # 64 are num of planes
self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2) # now stride !=1 and 64! 128
self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)
self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2) # size of 4*4
self.fc5 = nn.Linear(512 * 4 * 4, self.feature_dim)
self.bn2 = nn.BatchNorm1d(self.feature_dim)
# self.drop = nn.Dropout(p=0.3)
if self.split:
self.num_seg = num_seg
self.len_seg = int(self.feature_dim / self.num_seg)
self.ListBn = nn.ModuleList([nn.BatchNorm1d(self.len_seg) for i in range(self.num_seg)])
def _make_layer(self, block, planes, num_blocks, stride):
strides = [stride] + [1]*(num_blocks-1) # [2, 1, 1, 1]
layers = []
for stride in strides:
layers.append(block(self.in_planes, planes, stride))
self.in_planes = planes * block.expansion
return nn.Sequential(*layers) # return self.layer1, self.layer2...
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
# out = F.avg_pool2d(out, 4)# 4*4 the full size of feature map, equal to global average pooling
"""
comment the below lines if using gloabal avg pooling"""
out = out.view(out.size(0), -1)
out = self.fc5(out)
if self.split:
out = [self.ListBn[i](out[:, self.len_seg*i: self.len_seg*(i+1)]) for i in range(self.num_seg)]
out = torch.cat(out, dim=1)
else:
out = self.bn2(out)
return out
def CosQuantNet34(num_seg, split, feature_dim):
return ResNet_q(BasicBlock, [3, 4, 6, 3], num_seg=num_seg, split=split, feature_dim=feature_dim)
if __name__ == '__main__':
net = resnet20_pq()
img = torch.randn(3, 3, 112, 112)
y = net(img)
# print(net)
print(y.size())