-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwide_resnet.py
282 lines (243 loc) · 10.5 KB
/
wide_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
# network definition
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from collections import OrderedDict
# wildcard import for legacy reasons
if __name__ == '__main__':
from blocks import *
else:
from .blocks import *
def parse_options(convtype, blocktype):
# legacy cmdline argument parsing
if isinstance(convtype,str):
conv = conv_function(convtype)
else:
raise NotImplementedError("Tuple convolution specification no longer supported.")
if blocktype =='Basic':
block = BasicBlock
elif blocktype =='Bottle':
block = BottleBlock
elif blocktype =='Old':
block = OldBlock
else:
block = None
return conv, block
def group_lowrank(named_parameters, weight_decay, compression_ratio):
lowrank_params, other_params = [], []
for n,p in named_parameters:
if 'A' in n or 'D' in n:
lowrank_params.append(p)
elif 'shuffle' in n:
lowrank_params.append(p)
elif 'hashed' in n:
lowrank_params.append(p)
elif 'weight_core' in n or 'weight_u' in n:
lowrank_params.append(p)
elif 'lowrank' in n:
lowrank_params.append(p)
else:
other_params.append(p)
return [{'params': lowrank_params,
'weight_decay': compression_ratio*weight_decay},
{'params': other_params}]
def compression(model_class, kwargs):
# assume there is a kwarg "conv", which is the convolution we've chosen
compressed_params = sum([p.numel() for p in
model_class(**kwargs).parameters()])
if 'genotype' in list(kwargs.keys()):
# standard conv with DARTS is DepthwiseSep
kwargs['ConvClass'] = DepthwiseSep
else:
# everything else it's Conv
kwargs['ConvClass'] = Conv
uncompressed_params = sum([p.numel() for p in
model_class(**kwargs).parameters()])
ratio = float(compressed_params)/float(uncompressed_params)
print("Compression: %i to %i, ratio %.2f"%(uncompressed_params,
compressed_params, ratio))
return ratio
class WideResNet(nn.Module):
def __init__(self, depth, widen_factor, ConvClass, block, num_classes=10, dropRate=0.0, s = 1):
super(WideResNet, self).__init__()
self.kwargs = dict(depth=depth, widen_factor=widen_factor, ConvClass=ConvClass,
block=block, num_classes=num_classes, dropRate=dropRate, s=s)
nChannels = [16, 16 * widen_factor, 32 * widen_factor, 64 * widen_factor]
nChannels = [int(a) for a in nChannels]
assert ((depth - 4) % 6 == 0) # why?
n = (depth - 4) // 6
assert n % s == 0, 'n mod s must be zero'
# 1st conv before any network block
self.conv1 = nn.Conv2d(3, nChannels[0], kernel_size=3, stride=1,
padding=1, bias=False)
# 1st block
self.block1 = torch.nn.ModuleList()
for i in range(s):
self.block1.append(NetworkBlock(int(n//s), nChannels[0] if i == 0 else nChannels[1],
nChannels[1], block, 1, dropRate, ConvClass))
# 2nd block
self.block2 = torch.nn.ModuleList()
for i in range(s):
self.block2.append(NetworkBlock(int(n//s), nChannels[1] if i == 0 else nChannels[2],
nChannels[2], block, 2 if i == 0 else 1, dropRate, ConvClass))
# 3rd block
self.block3 = torch.nn.ModuleList()
for i in range(s):
self.block3.append(NetworkBlock(int(n//s), nChannels[2] if i == 0 else nChannels[3],
nChannels[3], block, 2 if i == 0 else 1, dropRate, ConvClass))
# 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]
# normal is better than uniform initialisation
# this should really be in `self.reset_parameters`
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
try:
m.weight.data.normal_(0, math.sqrt(2. / n))
except AttributeError:
pass
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
m.bias.data.zero_()
def compression_ratio(self):
return compression(self.__class__, self.kwargs)
def grouped_parameters(self, weight_decay):
# iterate over parameters and separate those in ACDC layers
return group_lowrank(self.named_parameters(), weight_decay,
self.compression_ratio())
def forward(self, x):
activation_maps = []
out = self.conv1(x)
#activations.append(out)
attention = lambda x: F.normalize(x.pow(2).mean(1).view(x.size(0), -1))
for sub_block in self.block1:
out = sub_block(out)
activation_maps.append(attention(out))
for sub_block in self.block2:
out = sub_block(out)
activation_maps.append(attention(out))
for sub_block in self.block3:
out = sub_block(out)
activation_maps.append(attention(out))
out = self.relu(self.bn1(out))
out = F.avg_pool2d(out, 8)
out = out.view(-1, self.nChannels)
return self.fc(out), activation_maps
class ResNet(nn.Module):
def __init__(self, ConvClass, layers, block=Bottleneck, widen=1,
num_classes=1000, expansion=4):
self.kwargs = dict(layers=layers, expansion=expansion,
ConvClass=ConvClass, widen=widen, num_classes=num_classes,
block=block)
self.expansion = expansion
super(ResNet, self).__init__()
self.Conv = ConvClass
self.inplanes = 64
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64*widen, layers[0])
self.layer2 = self._make_layer(block, 128*widen, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256*widen, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512*widen, layers[3], stride=2)
self.avgpool = nn.AvgPool2d((7, 7), 1, 0)
self.fc = nn.Linear(512*widen * self.expansion, num_classes)
#self.fc = self.Conv(512*widen * self.expansion, num_classes, kernel_size=1, bias=True)
for m in self.modules():
if isinstance(m, nn.Conv2d):
if hasattr(m, 'weight'):
w = m.weight
nn.init.kaiming_normal_(w, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * self.expansion:
downsample = nn.Sequential(OrderedDict([
('conv', self.Conv(self.inplanes, planes * self.expansion,
kernel_size=1, stride=stride, padding=0, bias=False)),
('bn', nn.BatchNorm2d(planes * self.expansion))
]))
layers = []
layers.append(block(self.inplanes, planes, self.Conv, stride, downsample, self.expansion))
self.inplanes = planes * self.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes, self.Conv, expansion=self.expansion))
return nn.Sequential(*layers)
def compression_ratio(self):
return compression(self.__class__, self.kwargs)
def grouped_parameters(self, weight_decay):
# iterate over parameters and separate those in other layer types
return group_lowrank(self.named_parameters(), weight_decay,
self.compression_ratio())
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
attention_maps = []
attention = lambda x: F.normalize(x.pow(2).mean(1).view(x.size(0), -1))
if self.train:
x = self.layer1(x)
#x = checkpoint(self.layer1, x)
#x = checkpoint_sequential(self.layer1, 1, x)
else:
x = self.layer1(x)
attention_maps.append(attention(x))
if self.train:
x = self.layer2(x)
#x = checkpoint(self.layer2, x)
#x = checkpoint_sequential(self.layer2, 1, x)
else:
x = self.layer2(x)
attention_maps.append(attention(x))
if self.train:
x = self.layer3(x)
#x = checkpoint(self.layer3, x)
#x = checkpoint_sequential(self.layer3, 1, x)
else:
x = self.layer3(x)
attention_maps.append(attention(x))
if self.train:
x = self.layer4(x)
#x = checkpoint(self.layer4, x)
#x = checkpoint_sequential(self.layer4, 1, x)
else:
x = self.layer4(x)
attention_maps.append(attention(x))
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
#x = x.view(x.size(0), -1)
return x, attention_maps
def WRN_50_2(Conv, Block=None):
assert Block is None
return ResNet(Conv, [3, 4, 6, 3], widen=2, expansion=2)
def test():
net = WideResNet(28, 10, conv_function("Shuffle_7"), BasicBlock)
params = net.grouped_parameters(5e-4)
params = [d['params'] for d in params]
print("Low-rank: ", sum([p.numel() for p in params[0]]))
print("Full-rank: ", sum([p.numel() for p in params[1]]))
print("FC: ", sum([p.numel() for p in net.fc.parameters()]))
net = WRN_50_2(conv_function("Shuffle_7"))
params = net.grouped_parameters(5e-4)
params = [d['params'] for d in params]
print("Low-rank: ", sum([p.numel() for p in params[0]]))
print("Full-rank: ", sum([p.numel() for p in params[1]]))
print("FC: ", sum([p.numel() for p in net.fc.parameters()]))
x = torch.randn(1,3,224,224).float()
y, _ = net(Variable(x))
print(y.size())
if __name__ == '__main__':
test()