-
Notifications
You must be signed in to change notification settings - Fork 285
/
nas_modules.py
189 lines (162 loc) · 6.26 KB
/
nas_modules.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
import math
from .layers import *
class MobileInvertedResidualBlock(BasicUnit):
def __init__(self, mobile_inverted_conv, shortcut):
super(MobileInvertedResidualBlock, self).__init__()
self.mobile_inverted_conv = mobile_inverted_conv
self.shortcut = shortcut
def forward(self, x):
if self.mobile_inverted_conv.is_zero_layer():
res = x
elif self.shortcut is None or self.shortcut.is_zero_layer():
res = self.mobile_inverted_conv(x)
else:
conv_x = self.mobile_inverted_conv(x)
skip_x = self.shortcut(x)
res = skip_x + conv_x
return res
@property
def unit_str(self):
return '(%s, %s)' % (self.mobile_inverted_conv.unit_str,
self.shortcut.unit_str if self.shortcut is not None else None)
@property
def config(self):
return {
'name': MobileInvertedResidualBlock.__name__,
'mobile_inverted_conv': self.mobile_inverted_conv.config,
'shortcut': self.shortcut.config if self.shortcut is not None else None,
}
@staticmethod
def build_from_config(config):
mobile_inverted_conv = set_layer_from_config(
config['mobile_inverted_conv'])
shortcut = set_layer_from_config(config['shortcut'])
return MobileInvertedResidualBlock(mobile_inverted_conv, shortcut)
def get_flops(self, x):
flops1, _ = self.mobile_inverted_conv.get_flops(x)
if self.shortcut:
flops2, _ = self.shortcut.get_flops(x)
else:
flops2 = 0
return flops1 + flops2, self.forward(x)
class ProxylessNASNets(BasicUnit):
def __init__(self, first_conv, blocks, feature_mix_layer, classifier):
super(ProxylessNASNets, self).__init__()
self.first_conv = first_conv
self.blocks = nn.ModuleList(blocks)
self.feature_mix_layer = feature_mix_layer
self.global_avg_pooling = nn.AdaptiveAvgPool2d(1)
self.classifier = classifier
def forward(self, x):
x = self.first_conv(x)
for block in self.blocks:
x = block(x)
if self.feature_mix_layer:
x = self.feature_mix_layer(x)
x = self.global_avg_pooling(x)
x = x.view(x.size(0), -1) # flatten
x = self.classifier(x)
return x
@property
def unit_str(self):
_str = ''
for block in self.blocks:
_str += block.unit_str + '\n'
return _str
@property
def config(self):
return {
'name': ProxylessNASNets.__name__,
'bn': self.get_bn_param(),
'first_conv': self.first_conv.config,
'feature_mix_layer': self.feature_mix_layer.config if self.feature_mix_layer is not None else None,
'classifier': self.classifier.config,
'blocks': [
block.config for block in self.blocks],
}
@staticmethod
def build_from_config(config):
first_conv = set_layer_from_config(config['first_conv'])
feature_mix_layer = set_layer_from_config(config['feature_mix_layer'])
classifier = set_layer_from_config(config['classifier'])
blocks = []
for block_config in config['blocks']:
blocks.append(
MobileInvertedResidualBlock.build_from_config(block_config))
return ProxylessNASNets(
first_conv,
blocks,
feature_mix_layer,
classifier)
def get_flops(self, x):
flop, x = self.first_conv.get_flops(x)
for block in self.blocks:
delta_flop, x = block.get_flops(x)
flop += delta_flop
if self.feature_mix_layer:
delta_flop, x = self.feature_mix_layer.get_flops(x)
flop += delta_flop
x = self.global_avg_pooling(x)
x = x.view(x.size(0), -1) # flatten
delta_flop, x = self.classifier.get_flops(x)
flop += delta_flop
return flop, x
def set_bn_param(self, bn_momentum, bn_eps):
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
m.momentum = bn_momentum
m.eps = bn_eps
return
def get_bn_param(self):
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
return {
'momentum': m.momentum,
'eps': m.eps,
}
return None
def init_model(self, model_init, init_div_groups=True):
for m in self.modules():
if isinstance(m, nn.Conv2d):
if model_init == 'he_fout':
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
if init_div_groups:
n /= m.groups
m.weight.data.normal_(0, math.sqrt(2. / n))
elif model_init == 'he_fin':
n = m.kernel_size[0] * m.kernel_size[1] * m.in_channels
if init_div_groups:
n /= m.groups
m.weight.data.normal_(0, math.sqrt(2. / n))
else:
raise NotImplementedError
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm1d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def weight_parameters(self):
return self.parameters()
@staticmethod
def _make_divisible(v, divisor, min_val=None):
"""
This function is taken from the original tf repo.
It ensures that all layers have a channel number that is divisible by 8
It can be seen here:
https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
:param v:
:param divisor:
:param min_val:
:return:
"""
if min_val is None:
min_val = divisor
new_v = max(min_val, int(v + divisor / 2) // divisor * divisor)
# Make sure that round down does not go down by more than 10%.
if new_v < 0.9 * v:
new_v += divisor
return new_v