-
Notifications
You must be signed in to change notification settings - Fork 0
/
resnet.py
462 lines (355 loc) · 16 KB
/
resnet.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from group_normalization import GroupNorm2d
from mabn import MABN2d
def norm2d(planes, group_norm):
if group_norm=='group_norm':
return nn.GroupNorm(2, planes)
if group_norm=='instancenorm':
return nn.GroupNorm(planes, planes)
elif group_norm=='mabn':
return MABN2d(planes, B=20, real_B=100, momentum=0.997, eps=1e-5)
else:
return nn.BatchNorm2d(planes,eps=1e-5, affine=True, track_running_stats=True)
def conv2d(in_channels, out_channels, kernel_size, stride=1,
padding=0, dilation=1, groups=1, bias=True, weight_stand=0):
if weight_stand==1:
return Conv2d_stand(in_channels, out_channels, kernel_size, stride,
padding, dilation, groups, bias)
elif weight_stand==2:
return CenConv2d(in_channels, out_channels, kernel_size, stride,
padding, dilation, groups, bias)
else:
return nn.Conv2d(in_channels, out_channels, kernel_size, stride,
padding, dilation, groups, bias)
class CenConv2d(nn.Conv2d):
"""Conv2d layer with Weight Centralization.
The args is exactly same as torch.nn.Conv2d. It's suggested to set bias=False when
using CenConv2d with MABN.
"""
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
padding=0, dilation=1, groups=1, bias=True):
super(CenConv2d, self).__init__(in_channels, out_channels, kernel_size, stride,
padding, dilation, groups, bias)
def forward(self, x):
weight = self.weight
weight_mean = weight.mean(dim=1, keepdim=True).mean(dim=2, keepdim=True).mean(dim=3, keepdim=True)
weight = weight - weight_mean
return F.conv2d(x, weight, self.bias, self.stride,
self.padding, self.dilation, self.groups)
class Conv2d_stand(nn.Conv2d):
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
padding=0, dilation=1, groups=1, bias=True):
super(Conv2d_stand, self).__init__(in_channels, out_channels, kernel_size, stride,
padding, dilation, groups, bias)
def forward(self, x):
weight = self.weight
weight_mean = weight.mean(dim=1, keepdim=True).mean(dim=2,
keepdim=True).mean(dim=3, keepdim=True)
weight = weight - weight_mean
std = weight.view(weight.size(0), -1).std(dim=1).view(-1, 1, 1, 1) + 1e-5
weight = weight / std.expand_as(weight)
return F.conv2d(x, weight, self.bias, self.stride,
self.padding, self.dilation, self.groups)
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_planes, planes, stride=1, group_norm='group_norm', weight_stand=0):
super(BasicBlock, self).__init__()
self.conv1 = conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False, weight_stand=weight_stand)
self.bn1 = norm2d(planes, group_norm)
self.conv2 = conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False, weight_stand=weight_stand)
self.bn2 = norm2d(planes, group_norm)
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != self.expansion * planes:
self.shortcut = nn.Sequential(
conv2d(in_planes, self.expansion * planes, kernel_size=1, stride=stride, bias=False, weight_stand=weight_stand),
norm2d(self.expansion * planes, group_norm)
)
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 BasicBlock_nobn(nn.Module):
expansion = 1
def __init__(self, in_planes, planes, stride=1, group_norm='group_norm', weight_stand=0):
super(BasicBlock_nobn, self).__init__()
self.conv1 = conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False, weight_stand=weight_stand)
self.conv2 = conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False, weight_stand=weight_stand)
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != self.expansion * planes:
self.shortcut = nn.Sequential(
conv2d(in_planes, self.expansion * planes, kernel_size=1, stride=stride, bias=False, weight_stand=weight_stand),
)
def forward(self, x):
out = F.relu(self.conv1(x))
out = self.conv2(out)
out += self.shortcut(x)
out = F.relu(out)
return out
class BasicBlock_noshortcut(nn.Module):
expansion = 1
def __init__(self, in_planes, planes, stride=1):
super(BasicBlock_noshortcut, self).__init__()
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=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)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out = F.relu(out)
return out
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, in_planes, planes, stride=1):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, self.expansion * planes, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(self.expansion * 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),
nn.BatchNorm2d(self.expansion * planes)
)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = F.relu(self.bn2(self.conv2(out)))
out = self.bn3(self.conv3(out))
out += self.shortcut(x)
out = F.relu(out)
return out
class Bottleneck_noshortcut(nn.Module):
expansion = 4
def __init__(self, in_planes, planes, stride=1):
super(Bottleneck_noshortcut, self).__init__()
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, self.expansion * planes, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(self.expansion * planes)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = F.relu(self.bn2(self.conv2(out)))
out = self.bn3(self.conv3(out))
return out
class ResNet(nn.Module):
def __init__(self, block, num_blocks, num_classes=10):
super(ResNet, self).__init__()
self.in_planes = 64
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)
self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)
self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)
self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)
self.fc = nn.Linear(512 * block.expansion, num_classes)
def _make_layer(self, block, planes, num_blocks, stride):
strides = [stride] + [1] * (num_blocks - 1)
layers = []
for stride in strides:
layers.append(block(self.in_planes, planes, stride))
self.in_planes = planes * block.expansion
return nn.Sequential(*layers)
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)
out = out.view(out.size(0), -1)
out = self.fc(out)
return out
class ResNet_cifar(nn.Module):
def __init__(self, block, num_blocks, num_classes=10, group_norm='group_norm', weight_stand=0):
super(ResNet_cifar, self).__init__()
self.in_planes = 16
self.conv1 = conv2d(3, 16, kernel_size=3, stride=1, padding=1, bias=False, weight_stand=weight_stand)
self.bn1 = norm2d(16, group_norm)
self.layer1 = self._make_layer(block, 16, num_blocks[0], stride=1, group_norm=group_norm, weight_stand=weight_stand)
self.layer2 = self._make_layer(block, 32, num_blocks[1], stride=2, group_norm=group_norm, weight_stand=weight_stand)
self.layer3 = self._make_layer(block, 64, num_blocks[2], stride=2, group_norm=group_norm, weight_stand=weight_stand)
self.fc = nn.Linear(64 * block.expansion, num_classes)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
def _make_layer(self, block, planes, num_blocks, stride, group_norm='group_norm', weight_stand=0):
strides = [stride] + [1] * (num_blocks - 1)
layers = []
for stride in strides:
layers.append(block(self.in_planes, planes, stride, group_norm, weight_stand))
self.in_planes = planes * block.expansion
return nn.Sequential(*layers)
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.avgpool(out)
# out = F.avg_pool2d(out, 8)
out = out.view(out.size(0), -1)
out = self.fc(out)
return out
class ResNet_cifar_nobn(nn.Module):
def __init__(self, block, num_blocks, num_classes=10, group_norm='group_norm', weight_stand=0):
super(ResNet_cifar_nobn, self).__init__()
self.in_planes = 16
self.conv1 = conv2d(3, 16, kernel_size=3, stride=1, padding=1, bias=False, weight_stand=weight_stand)
self.layer1 = self._make_layer(block, 16, num_blocks[0], stride=1, group_norm=group_norm, weight_stand=weight_stand)
self.layer2 = self._make_layer(block, 32, num_blocks[1], stride=2, group_norm=group_norm, weight_stand=weight_stand)
self.layer3 = self._make_layer(block, 64, num_blocks[2], stride=2, group_norm=group_norm, weight_stand=weight_stand)
self.fc = nn.Linear(64 * block.expansion, num_classes)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
def _make_layer(self, block, planes, num_blocks, stride, group_norm='group_norm', weight_stand=0):
strides = [stride] + [1] * (num_blocks - 1)
layers = []
for stride in strides:
layers.append(block(self.in_planes, planes, stride, group_norm, weight_stand))
self.in_planes = planes * block.expansion
return nn.Sequential(*layers)
def forward(self, x):
out = F.relu(self.conv1(x))
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.avgpool(out)
# out = F.avg_pool2d(out, 8)
out = out.view(out.size(0), -1)
out = self.fc(out)
return out
class WResNet_cifar(nn.Module):
def __init__(self, block, num_blocks, k, num_classes=10):
super(WResNet_cifar, self).__init__()
self.in_planes = 16 * k
self.conv1 = nn.Conv2d(3, 16 * k, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(16 * k)
self.layer1 = self._make_layer(block, 16 * k, num_blocks[0], stride=1)
self.layer2 = self._make_layer(block, 32 * k, num_blocks[1], stride=2)
self.layer3 = self._make_layer(block, 64 * k, num_blocks[2], stride=2)
self.fc = nn.Linear(64 * k * block.expansion, num_classes)
def _make_layer(self, block, planes, num_blocks, stride):
strides = [stride] + [1] * (num_blocks - 1)
layers = []
for stride in strides:
layers.append(block(self.in_planes, planes, stride))
self.in_planes = planes * block.expansion
return nn.Sequential(*layers)
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 = F.avg_pool2d(out, 8)
out = out.view(out.size(0), -1)
out = self.fc(out)
return out
# ImageNet models
def ResNet18(num_classes=10, group_norm='group_norm', weight_stand=0):
if weight_stand==0 and group_norm=='mabn':
weight_stand=2
return ResNet(BasicBlock, [2, 2, 2, 2], num_classes=num_classes, group_norm=group_norm, weight_stand=weight_stand)
def ResNet18_noshort():
return ResNet(BasicBlock_noshortcut, [2, 2, 2, 2])
def ResNet34():
return ResNet(BasicBlock, [3, 4, 6, 3])
def ResNet34_noshort():
return ResNet(BasicBlock_noshortcut, [3, 4, 6, 3])
def ResNet50():
return ResNet(Bottleneck, [3, 4, 6, 3])
def ResNet50_noshort():
return ResNet(Bottleneck_noshortcut, [3, 4, 6, 3])
def ResNet101():
return ResNet(Bottleneck, [3, 4, 23, 3])
def ResNet101_noshort():
return ResNet(Bottleneck_noshortcut, [3, 4, 23, 3])
def ResNet152():
return ResNet(Bottleneck, [3, 8, 36, 3])
def ResNet152_noshort():
return ResNet(Bottleneck_noshortcut, [3, 8, 36, 3])
# CIFAR-10 models
def ResNet20():
depth = 20
n = (depth - 2) // 6
return ResNet_cifar(BasicBlock, [n, n, n])
def ResNet20_noshort():
depth = 20
n = (depth - 2) // 6
return ResNet_cifar(BasicBlock_noshortcut, [n, n, n])
def ResNet32_test(num_classes=10, group_norm='group_norm', weight_stand=0):
if weight_stand==0 and group_norm=='mabn':
weight_stand=2
depth = 32
n = (depth - 2) // 6
return ResNet_cifar(BasicBlock, [n, n, n],group_norm=group_norm, weight_stand=weight_stand, num_classes=num_classes)
def ResNet32_nobn(num_classes=10, group_norm='group_norm', weight_stand=0):
if weight_stand==0 and group_norm=='mabn':
weight_stand=2
depth = 32
n = (depth - 2) // 6
return ResNet_cifar_nobn(BasicBlock_nobn, [n, n, n],group_norm=group_norm, weight_stand=weight_stand, num_classes=num_classes)
def ResNet32_noshort():
depth = 32
n = (depth - 2) // 6
return ResNet_cifar(BasicBlock_noshortcut, [n, n, n])
def ResNet44_noshort():
depth = 44
n = (depth - 2) // 6
return ResNet_cifar(BasicBlock_noshortcut, [n, n, n])
def ResNet50_16_noshort():
depth = 50
n = (depth - 2) // 6
return ResNet_cifar(BasicBlock_noshortcut, [n, n, n])
def ResNet56():
depth = 56
n = (depth - 2) // 6
return ResNet_cifar(BasicBlock, [n, n, n])
def ResNet56_noshort():
depth = 56
n = (depth - 2) // 6
return ResNet_cifar(BasicBlock_noshortcut, [n, n, n])
def ResNet110():
depth = 110
n = (depth - 2) // 6
return ResNet_cifar(BasicBlock, [n, n, n])
def ResNet110_noshort():
depth = 110
n = (depth - 2) // 6
return ResNet_cifar(BasicBlock_noshortcut, [n, n, n])
def WRN56_2():
depth = 56
n = (depth - 2) // 6
return WResNet_cifar(BasicBlock, [n, n, n], 2)
def WRN56_4():
depth = 56
n = (depth - 2) // 6
return WResNet_cifar(BasicBlock, [n, n, n], 4)
def WRN56_8():
depth = 56
n = (depth - 2) // 6
return WResNet_cifar(BasicBlock, [n, n, n], 8)
def WRN56_2_noshort():
depth = 56
n = (depth - 2) // 6
return WResNet_cifar(BasicBlock_noshortcut, [n, n, n], 2)
def WRN56_4_noshort():
depth = 56
n = (depth - 2) // 6
return WResNet_cifar(BasicBlock_noshortcut, [n, n, n], 4)
def WRN56_8_noshort():
depth = 56
n = (depth - 2) // 6
return WResNet_cifar(BasicBlock_noshortcut, [n, n, n], 8)
def WRN110_2_noshort():
depth = 110
n = (depth - 2) // 6
return WResNet_cifar(BasicBlock_noshortcut, [n, n, n], 2)
def WRN110_4_noshort():
depth = 110
n = (depth - 2) // 6
return WResNet_cifar(BasicBlock_noshortcut, [n, n, n], 4)