-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
748 lines (636 loc) · 30.3 KB
/
models.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
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
import math
import time
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
class SparseSpeedupBench(object):
"""Class to benchmark speedups for convolutional layers.
Basic usage:
1. Assing a single SparseSpeedupBench instance to class (and sub-classes with conv layers).
2. Instead of forwarding input through normal convolutional layers, we pass them through the bench:
self.bench = SparseSpeedupBench()
self.conv_layer1 = nn.Conv2(3, 96, 3)
if self.bench is not None:
outputs = self.bench.forward(self.conv_layer1, inputs, layer_id='conv_layer1')
else:
outputs = self.conv_layer1(inputs)
3. Speedups of the convolutional layer will be aggregated and print every 1000 mini-batches.
"""
def __init__(self):
self.layer_timings = {}
self.layer_timings_channel_sparse = {}
self.layer_timings_sparse = {}
self.iter_idx = 0
self.layer_0_idx = None
self.total_timings = []
self.total_timings_channel_sparse = []
self.total_timings_sparse = []
def get_density(self, x):
return (x.data!=0.0).sum().item()/x.numel()
def print_weights(self, w, layer):
# w dims: out, in, k1, k2
#outers = []
#for outer in range(w.shape[0]):
# inners = []
# for inner in range(w.shape[1]):
# n = np.prod(w.shape[2:])
# density = (w[outer, inner, :, :] != 0.0).sum().item() / n
# #print(density, w[outer, inner])
# inners.append(density)
# outers.append([np.mean(inners), np.std(inner)])
#print(outers)
#print(w.shape, (w!=0.0).sum().item()/w.numel())
pass
def forward(self, layer, x, layer_id):
if self.layer_0_idx is None: self.layer_0_idx = layer_id
if layer_id == self.layer_0_idx: self.iter_idx += 1
self.print_weights(layer.weight.data, layer)
# calc input sparsity
sparse_channels_in = ((x.data != 0.0).sum([2, 3]) == 0.0).sum().item()
num_channels_in = x.shape[1]
batch_size = x.shape[0]
channel_sparsity_input = sparse_channels_in/float(num_channels_in*batch_size)
input_sparsity = self.get_density(x)
# bench dense layer
start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
start.record()
x = layer(x)
end.record()
start.synchronize()
end.synchronize()
time_taken_s = start.elapsed_time(end)/1000.0
# calc weight sparsity
num_channels = layer.weight.shape[1]
sparse_channels = ((layer.weight.data != 0.0).sum([0, 2, 3]) == 0.0).sum().item()
channel_sparsity_weight = sparse_channels/float(num_channels)
weight_sparsity = self.get_density(layer.weight)
# store sparse and dense timings
if layer_id not in self.layer_timings:
self.layer_timings[layer_id] = []
self.layer_timings_channel_sparse[layer_id] = []
self.layer_timings_sparse[layer_id] = []
self.layer_timings[layer_id].append(time_taken_s)
self.layer_timings_channel_sparse[layer_id].append(time_taken_s*(1.0-channel_sparsity_weight)*(1.0-channel_sparsity_input))
self.layer_timings_sparse[layer_id].append(time_taken_s*input_sparsity*weight_sparsity)
if self.iter_idx % 1000 == 0:
self.print_layer_timings()
self.iter_idx += 1
return x
def print_layer_timings(self):
total_time_dense = 0.0
total_time_sparse = 0.0
total_time_channel_sparse = 0.0
print('\n')
for layer_id in self.layer_timings:
t_dense = np.mean(self.layer_timings[layer_id])
t_channel_sparse = np.mean(self.layer_timings_channel_sparse[layer_id])
t_sparse = np.mean(self.layer_timings_sparse[layer_id])
total_time_dense += t_dense
total_time_sparse += t_sparse
total_time_channel_sparse += t_channel_sparse
print('Layer {0}: Dense {1:.6f} Channel Sparse {2:.6f} vs Full Sparse {3:.6f}'.format(layer_id, t_dense, t_channel_sparse, t_sparse))
self.total_timings.append(total_time_dense)
self.total_timings_sparse.append(total_time_sparse)
self.total_timings_channel_sparse.append(total_time_channel_sparse)
print('Speedups for this segment:')
print('Dense took {0:.4f}s. Channel Sparse took {1:.4f}s. Speedup of {2:.4f}x'.format(total_time_dense, total_time_channel_sparse, total_time_dense/total_time_channel_sparse))
print('Dense took {0:.4f}s. Sparse took {1:.4f}s. Speedup of {2:.4f}x'.format(total_time_dense, total_time_sparse, total_time_dense/total_time_sparse))
print('\n')
total_dense = np.sum(self.total_timings)
total_sparse = np.sum(self.total_timings_sparse)
total_channel_sparse = np.sum(self.total_timings_channel_sparse)
print('Speedups for entire training:')
print('Dense took {0:.4f}s. Channel Sparse took {1:.4f}s. Speedup of {2:.4f}x'.format(total_dense, total_channel_sparse, total_dense/total_channel_sparse))
print('Dense took {0:.4f}s. Sparse took {1:.4f}s. Speedup of {2:.4f}x'.format(total_dense, total_sparse, total_dense/total_sparse))
print('\n')
# clear timings
for layer_id in list(self.layer_timings.keys()):
self.layer_timings.pop(layer_id)
self.layer_timings_channel_sparse.pop(layer_id)
self.layer_timings_sparse.pop(layer_id)
class AlexNet(nn.Module):
"""AlexNet with batch normalization and without pooling.
This is an adapted version of AlexNet as taken from
SNIP: Single-shot Network Pruning based on Connection Sensitivity,
https://arxiv.org/abs/1810.02340
There are two different version of AlexNet:
AlexNet-s (small): Has hidden layers with size 1024
AlexNet-b (big): Has hidden layers with size 2048
Based on https://github.com/mi-lad/snip/blob/master/train.py
by Milad Alizadeh.
"""
def __init__(self, config='s', num_classes=1000, save_features=False, bench_model=False):
super(AlexNet, self).__init__()
self.save_features = save_features
self.feats = []
self.densities = []
self.bench = None if not bench_model else SparseSpeedupBench()
factor = 1 if config=='s' else 2
self.features = nn.Sequential(
nn.Conv2d(3, 96, kernel_size=11, stride=2, padding=2, bias=True),
nn.BatchNorm2d(96),
nn.ReLU(inplace=True),
nn.Conv2d(96, 256, kernel_size=5, stride=2, padding=2, bias=True),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Conv2d(256, 384, kernel_size=3, stride=2, padding=1, bias=True),
nn.BatchNorm2d(384),
nn.ReLU(inplace=True),
nn.Conv2d(384, 384, kernel_size=3, stride=2, padding=1, bias=True),
nn.BatchNorm2d(384),
nn.ReLU(inplace=True),
nn.Conv2d(384, 256, kernel_size=3, stride=2, padding=1, bias=True),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
)
self.classifier = nn.Sequential(
nn.Linear(256, 1024*factor),
nn.BatchNorm1d(1024*factor),
nn.ReLU(inplace=True),
nn.Linear(1024*factor, 1024*factor),
nn.BatchNorm1d(1024*factor),
nn.ReLU(inplace=True),
nn.Linear(1024*factor, num_classes),
)
def forward(self, x):
for layer_id, layer in enumerate(self.features):
if self.bench is not None and isinstance(layer, nn.Conv2d):
x = self.bench.forward(layer, x, layer_id)
else:
x = layer(x)
if self.save_features:
if isinstance(layer, nn.ReLU):
self.feats.append(x.clone().detach())
if isinstance(layer, nn.Conv2d):
self.densities.append((layer.weight.data != 0.0).sum().item()/layer.weight.numel())
x = x.view(x.size(0), -1)
x = self.classifier(x)
return F.log_softmax(x, dim=1)
class LeNet_300_100(nn.Module):
"""Simple NN with hidden layers [300, 100]
Based on https://github.com/mi-lad/snip/blob/master/train.py
by Milad Alizadeh.
"""
def __init__(self, save_features=None, bench_model=False):
super(LeNet_300_100, self).__init__()
self.fc1 = nn.Linear(28*28, 300, bias=False)
self.fc2 = nn.Linear(300, 100, bias=False)
self.fc3 = nn.Linear(100, 10, bias=False)
self.mask = None
def forward(self, x):
x0 = x.view(-1, 28*28)
x1 = F.relu(self.fc1(x0))
x2 = F.relu(self.fc2(x1))
x3 = self.fc3(x2)
return x3
class MLP_CIFAR10(nn.Module):
def __init__(self, save_features=None, bench_model=False):
super(MLP_CIFAR10, self).__init__()
self.fc1 = nn.Linear(3*32*32, 1024)
self.fc2 = nn.Linear(1024, 512)
self.fc3 = nn.Linear(512, 10)
def forward(self, x):
x0 = F.relu(self.fc1(x.view(-1, 3*32*32)))
x1 = F.relu(self.fc2(x0))
return F.log_softmax(self.fc3(x1), dim=1)
class LeNet5(nn.Module):
def __init__(self, n_classes):
super(LeNet5, self).__init__()
#self.conv1 = nn.Conv2d(3, 6, 5, bias=False) #CIFAR10
self.conv1 = nn.Conv2d(1, 6, 5, bias=False)
self.relu = nn.ReLU()
self.pool = nn.MaxPool2d(2)
self.conv2 = nn.Conv2d(6, 16, 5, bias=False)
#self.fc1 = nn.Linear(16*5*5, 120, bias=False) #CIFAR10
self.fc1 = nn.Linear(16 * 4 * 4, 120, bias=False)
self.fc2 = nn.Linear(120, 84, bias=False)
self.fc3 = nn.Linear(84, n_classes, bias=False)
def forward(self, x):
x = self.pool(self.relu(self.conv1(x)))
x = self.pool(self.relu(self.conv2(x)))
#x = x.view(-1, 16 * 5 * 5) #CIFAR10
x = x.view(-1, 16 * 4 * 4)
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = self.fc3(x)
return x
class LeNet_5_Caffe(nn.Module):
"""LeNet-5 without padding in the first layer.
This is based on Caffe's implementation of Lenet-5 and is slightly different
from the vanilla LeNet-5. Note that the first layer does NOT have padding
and therefore intermediate shapes do not match the official LeNet-5.
Based on https://github.com/mi-lad/snip/blob/master/train.py
by Milad Alizadeh.
"""
def __init__(self, save_features=None, bench_model=False):
super().__init__()
self.conv1 = nn.Conv2d(1, 20, 5, padding=0, bias=True)
self.conv2 = nn.Conv2d(20, 50, 5, bias=True)
self.fc3 = nn.Linear(50 * 4 * 4, 500)
self.fc4 = nn.Linear(500, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.fc3(x.view(-1, 50 * 4 * 4)))
x = F.log_softmax(self.fc4(x), dim=1)
return x
############################################################################################################
################################################ VGG #######################################################
############################################################################################################
VGG_CONFIGS = {
# M for MaxPool, Number for channels
'like': [
64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M',
512, 512, 512, 'M'
],
'D': [
64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M',
512, 512, 512, 'M'
],
'C': [
64, 64, 'M', 128, 128, 'M', 256, 256, (1, 256), 'M', 512, 512, (1, 512), 'M',
512, 512, (1, 512), 'M' # tuples indicate (kernel size, output channels)
]
}
class VGG16(nn.Module):
"""
This is a base class to generate three VGG variants used in SNIP paper:
1. VGG-C (16 layers)
2. VGG-D (16 layers)
3. VGG-like
Some of the differences:
* Reduced size of FC layers to 512
* Adjusted flattening to match CIFAR-10 shapes
* Replaced dropout layers with BatchNorm
Based on https://github.com/mi-lad/snip/blob/master/train.py
by Milad Alizadeh.
"""
def __init__(self, config, num_classes=100, save_features=False, bench_model=False):
super().__init__()
self.features = self.make_layers(VGG_CONFIGS[config], batch_norm=True)
self.feats = []
self.densities = []
self.save_features = save_features
self.bench = None if not bench_model else SparseSpeedupBench()
if config == 'C' or config == 'D':
self.classifier = nn.Sequential(
nn.Linear((512 if config == 'D' else 2048), 512), # 512 * 7 * 7 in the original VGG
nn.ReLU(True),
nn.BatchNorm1d(512, affine=False, track_running_stats=False), # instead of dropout
nn.Linear(512, 512),
nn.ReLU(True),
nn.BatchNorm1d(512, affine=False, track_running_stats=False), # instead of dropout
nn.Linear(512, num_classes),
)
else:
self.classifier = nn.Sequential(
nn.Linear(512, 512), # 512 * 7 * 7 in the original VGG
nn.ReLU(True),
nn.BatchNorm1d(512, affine=False, track_running_stats=False), # instead of dropout
nn.Linear(512, num_classes),
)
@staticmethod
def make_layers(config, batch_norm=False):
layers = []
in_channels = 3
for v in config:
if v == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
kernel_size = 3
if isinstance(v, tuple):
kernel_size, v = v
conv2d = nn.Conv2d(in_channels, v, kernel_size=kernel_size, padding=1)
if batch_norm:
layers += [
conv2d,
nn.BatchNorm2d(v, affine=False, track_running_stats=False),
nn.ReLU(inplace=True)
]
else:
layers += [conv2d, nn.ReLU(inplace=True)]
in_channels = v
return nn.Sequential(*layers)
def forward(self, x):
for layer_id, layer in enumerate(self.features):
if self.bench is not None and isinstance(layer, nn.Conv2d):
x = self.bench.forward(layer, x, layer_id)
else:
x = layer(x)
if self.save_features:
if isinstance(layer, nn.ReLU):
self.feats.append(x.clone().detach())
self.densities.append((x.data != 0.0).sum().item()/x.numel())
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
############################################################################################################
################################################ MobileNet #################################################
############################################################################################################
def MobileNetFunc(num_classes):
mobilenet_v2 = models.mobilenet_v2(pretrained=False)
# Modify the final fully connected layer for your specific classification task
mobilenet_v2.classifier[1] = nn.Linear(mobilenet_v2.last_channel, num_classes)
return mobilenet_v2
class InvertedResidual(nn.Module):
def __init__(self, in_channels, out_channels, stride, expand_ratio):
super(InvertedResidual, self).__init__()
self.use_residual = (stride == 1 and in_channels == out_channels)
hidden_dim = in_channels * expand_ratio
self.conv = nn.Sequential(
nn.Conv2d(in_channels, hidden_dim, 1, 1, 0, bias=False),
nn.BatchNorm2d(hidden_dim, affine=False, track_running_stats=False),
nn.ReLU6(inplace=True),
nn.Conv2d(hidden_dim, hidden_dim, 3, stride, 1, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim, affine=False, track_running_stats=False),
nn.ReLU6(inplace=True),
nn.Conv2d(hidden_dim, out_channels, 1, 1, 0, bias=False),
nn.BatchNorm2d(out_channels, affine=False, track_running_stats=False),
)
def forward(self, x):
if self.use_residual:
return x + self.conv(x)
else:
return self.conv(x)
# Define the MobileNetV2 architecture
class MobileNetV2(nn.Module):
def __init__(self, num_classes=100, width_mult=1.0):
super(MobileNetV2, self).__init__()
# Define the architecture parameters for MobileNetV2
inverted_residual_setting = [
[1, 16, 1, 1],
[6, 24, 2, 2],
[6, 32, 3, 2],
[6, 64, 4, 2],
[6, 96, 3, 1],
[6, 160, 3, 2],
[6, 320, 1, 1],
]
# Initial convolution layer
input_channels = int(32 * width_mult)
self.features = [nn.Sequential(
nn.Conv2d(3, input_channels, 3, 2, 1, bias=False),
nn.BatchNorm2d(input_channels, affine=False, track_running_stats=False),
nn.ReLU6(inplace=True)
)]
# Build the MobileNetV2 backbone
for t, c, n, s in inverted_residual_setting:
output_channels = int(c * width_mult)
for i in range(n):
stride = s if i == 0 else 1
self.features.append(InvertedResidual(input_channels, output_channels, stride, t))
input_channels = output_channels
# Final convolution layers
self.features.append(nn.Sequential(
nn.Conv2d(input_channels, 1280, 1, 1, 0, bias=False),
nn.BatchNorm2d(1280, affine=False, track_running_stats=False),
nn.ReLU6(inplace=True)
))
self.features = nn.Sequential(*self.features)
# Classifier (fully connected layer)
self.classifier = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Flatten(),
nn.Linear(1280, num_classes)
)
def forward(self, x):
x = self.features(x)
x = self.classifier(x)
return x
############################################################################################################
################################################ WideResNet ################################################
############################################################################################################
class WideResNet(nn.Module):
"""Wide Residual Network with varying depth and width.
For more info, see the paper: Wide Residual Networks by Sergey Zagoruyko, Nikos Komodakis
https://arxiv.org/abs/1605.07146
"""
def __init__(self, depth, widen_factor, num_classes=10, dropRate=0.3, save_features=False, bench_model=False):
super(WideResNet, self).__init__()
nChannels = [16, 16*widen_factor, 32*widen_factor, 64*widen_factor]
assert((depth - 4) % 6 == 0)
n = (depth - 4) / 6
block = BasicBlock
# 1st conv before any network block
self.conv1 = nn.Conv2d(3, nChannels[0], kernel_size=3, stride=1,
padding=1, bias=False)
self.bench = None if not bench_model else SparseSpeedupBench()
# 1st block
self.block1 = NetworkBlock(n, nChannels[0], nChannels[1], block, 1, dropRate, save_features=save_features, bench=self.bench)
# 2nd block
self.block2 = NetworkBlock(n, nChannels[1], nChannels[2], block, 2, dropRate, save_features=save_features, bench=self.bench)
# 3rd block
self.block3 = NetworkBlock(n, nChannels[2], nChannels[3], block, 2, dropRate, save_features=save_features, bench=self.bench)
# global average pooling and classifier
self.bn1 = nn.BatchNorm2d(nChannels[3])
self.relu = nn.ReLU(inplace=True)
self.fc = nn.Linear(nChannels[3], num_classes)
self.nChannels = nChannels[3]
self.feats = []
self.densities = []
self.save_features = save_features
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
m.bias.data.zero_()
def forward(self, x):
if self.bench is not None:
out = self.bench.forward(self.conv1, x, 'conv1')
else:
out = self.conv1(x)
out = self.block1(out)
out = self.block2(out)
out = self.block3(out)
if self.save_features:
# this is a mess, but I do not have time to refactor it now
self.feats += self.block1.feats
self.densities += self.block1.densities
del self.block1.feats[:]
del self.block1.densities[:]
self.feats += self.block2.feats
self.densities += self.block2.densities
del self.block2.feats[:]
del self.block2.densities[:]
self.feats += self.block3.feats
self.densities += self.block3.densities
del self.block3.feats[:]
del self.block3.densities[:]
out = self.relu(self.bn1(out))
out = F.avg_pool2d(out, 8)
out = out.view(-1, self.nChannels)
out = self.fc(out)
return F.log_softmax(out, dim=1)
class BasicBlock(nn.Module):
"""Wide Residual Network basic block
For more info, see the paper: Wide Residual Networks by Sergey Zagoruyko, Nikos Komodakis
https://arxiv.org/abs/1605.07146
"""
def __init__(self, in_planes, out_planes, stride, dropRate=0.0, save_features=False, bench=None):
super(BasicBlock, self).__init__()
self.bn1 = nn.BatchNorm2d(in_planes)
self.relu1 = nn.ReLU(inplace=True)
self.conv1 = nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(out_planes)
self.relu2 = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(out_planes, out_planes, kernel_size=3, stride=1,
padding=1, bias=False)
self.droprate = dropRate
self.equalInOut = (in_planes == out_planes)
self.convShortcut = (not self.equalInOut) and nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride,
padding=0, bias=False) or None
self.feats = []
self.densities = []
self.save_features = save_features
self.bench = bench
self.in_planes = in_planes
def forward(self, x):
conv_layers = []
if not self.equalInOut:
x = self.relu1(self.bn1(x))
if self.save_features:
self.feats.append(x.clone().detach())
self.densities.append((x.data != 0.0).sum().item()/x.numel())
else:
out = self.relu1(self.bn1(x))
if self.save_features:
self.feats.append(out.clone().detach())
self.densities.append((out.data != 0.0).sum().item()/out.numel())
if self.bench:
out0 = self.bench.forward(self.conv1, (out if self.equalInOut else x), str(self.in_planes) + '.conv1')
else:
out0 = self.conv1(out if self.equalInOut else x)
out = self.relu2(self.bn2(out0))
if self.save_features:
self.feats.append(out.clone().detach())
self.densities.append((out.data != 0.0).sum().item()/out.numel())
if self.droprate > 0:
out = F.dropout(out, p=self.droprate, training=self.training)
if self.bench:
out = self.bench.forward(self.conv2, out, str(self.in_planes) + '.conv2')
else:
out = self.conv2(out)
return torch.add(x if self.equalInOut else self.convShortcut(x), out)
class NetworkBlock(nn.Module):
"""Wide Residual Network network block which holds basic blocks.
For more info, see the paper: Wide Residual Networks by Sergey Zagoruyko, Nikos Komodakis
https://arxiv.org/abs/1605.07146
"""
def __init__(self, nb_layers, in_planes, out_planes, block, stride, dropRate=0.0, save_features=False, bench=None):
super(NetworkBlock, self).__init__()
self.feats = []
self.densities = []
self.save_features = save_features
self.bench = bench
self.layer = self._make_layer(block, in_planes, out_planes, nb_layers, stride, dropRate)
def _make_layer(self, block, in_planes, out_planes, nb_layers, stride, dropRate):
layers = []
for i in range(int(nb_layers)):
layers.append(block(i == 0 and in_planes or out_planes, out_planes, i == 0 and stride or 1, dropRate, save_features=self.save_features, bench=self.bench))
return nn.Sequential(*layers)
def forward(self, x):
for layer in self.layer:
x = layer(x)
if self.save_features:
self.feats += layer.feats
self.densities += layer.densities
del layer.feats[:]
del layer.densities[:]
return x
############################################################################################################
################################################ ResNet ####################################################
############################################################################################################
class BasicBlock(nn.Module):
expansion = 1
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)
self.bn1 = nn.BatchNorm2d(planes, affine=False, track_running_stats=False)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes, affine=False, track_running_stats=False)
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, affine=False, track_running_stats=False)
)
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 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, affine=False, track_running_stats=False)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes, affine=False, track_running_stats=False)
self.conv3 = nn.Conv2d(planes, self.expansion*planes, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(self.expansion*planes, affine=False, track_running_stats=False)
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, affine=False, track_running_stats=False)
)
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 ResNet(nn.Module):
def __init__(self, block, num_blocks, num_classes):
super(ResNet, self).__init__()
_outputs = [64, 128, 256, 512]
#_outputs = [21, 42, 85, 170]
self.in_planes = _outputs[0]
self.conv1 = nn.Conv2d(3, _outputs[0], kernel_size=7, stride=2, padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(_outputs[0], affine=False, track_running_stats=False)
self.layer1 = self._make_layer(block, _outputs[0], num_blocks[0], stride=1)
self.layer2 = self._make_layer(block, _outputs[1], num_blocks[1], stride=2)
self.layer3 = self._make_layer(block, _outputs[2], num_blocks[2], stride=2)
self.layer4 = self._make_layer(block, _outputs[3], num_blocks[3], stride=2)
self.avgpool = nn.AdaptiveAvgPool2d(1)
self.classifier = nn.Linear(_outputs[3]*block.expansion, num_classes, bias=False)
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 = self.avgpool(out)
out = out.view(out.size(0), -1)
out = self.classifier(out)
return out
def ResNet18(c=10):
return ResNet(BasicBlock, [2,2,2,2], c)
def ResNet34(c=10):
return ResNet(BasicBlock, [3,4,6,3],c)
def ResNet50(c=10):
return ResNet(Bottleneck, [3,4,6,3],c)
def ResNet101(c=10):
return ResNet(Bottleneck, [3,4,23,3],c)
def ResNet152(c=10):
return ResNet(Bottleneck, [3,8,36,3],c)