-
Notifications
You must be signed in to change notification settings - Fork 131
/
slimmable_ops.py
213 lines (187 loc) · 7.66 KB
/
slimmable_ops.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
import torch.nn as nn
from utils.config import FLAGS
class SwitchableBatchNorm2d(nn.Module):
def __init__(self, num_features_list):
super(SwitchableBatchNorm2d, self).__init__()
self.num_features_list = num_features_list
self.num_features = max(num_features_list)
bns = []
for i in num_features_list:
bns.append(nn.BatchNorm2d(i))
self.bn = nn.ModuleList(bns)
self.width_mult = max(FLAGS.width_mult_list)
self.ignore_model_profiling = True
def forward(self, input):
idx = FLAGS.width_mult_list.index(self.width_mult)
y = self.bn[idx](input)
return y
class SlimmableConv2d(nn.Conv2d):
def __init__(self, in_channels_list, out_channels_list,
kernel_size, stride=1, padding=0, dilation=1,
groups_list=[1], bias=True):
super(SlimmableConv2d, self).__init__(
max(in_channels_list), max(out_channels_list),
kernel_size, stride=stride, padding=padding, dilation=dilation,
groups=max(groups_list), bias=bias)
self.in_channels_list = in_channels_list
self.out_channels_list = out_channels_list
self.groups_list = groups_list
if self.groups_list == [1]:
self.groups_list = [1 for _ in range(len(in_channels_list))]
self.width_mult = max(FLAGS.width_mult_list)
def forward(self, input):
idx = FLAGS.width_mult_list.index(self.width_mult)
self.in_channels = self.in_channels_list[idx]
self.out_channels = self.out_channels_list[idx]
self.groups = self.groups_list[idx]
weight = self.weight[:self.out_channels, :self.in_channels, :, :]
if self.bias is not None:
bias = self.bias[:self.out_channels]
else:
bias = self.bias
y = nn.functional.conv2d(
input, weight, bias, self.stride, self.padding,
self.dilation, self.groups)
return y
class SlimmableLinear(nn.Linear):
def __init__(self, in_features_list, out_features_list, bias=True):
super(SlimmableLinear, self).__init__(
max(in_features_list), max(out_features_list), bias=bias)
self.in_features_list = in_features_list
self.out_features_list = out_features_list
self.width_mult = max(FLAGS.width_mult_list)
def forward(self, input):
idx = FLAGS.width_mult_list.index(self.width_mult)
self.in_features = self.in_features_list[idx]
self.out_features = self.out_features_list[idx]
weight = self.weight[:self.out_features, :self.in_features]
if self.bias is not None:
bias = self.bias[:self.out_features]
else:
bias = self.bias
return nn.functional.linear(input, weight, bias)
def make_divisible(v, divisor=8, min_value=1):
"""
forked from slim:
https://github.com/tensorflow/models/blob/\
0344c5503ee55e24f0de7f37336a6e08f10976fd/\
research/slim/nets/mobilenet/mobilenet.py#L62-L69
"""
if min_value is None:
min_value = divisor
new_v = max(min_value, 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
class USConv2d(nn.Conv2d):
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
padding=0, dilation=1, groups=1, depthwise=False, bias=True,
us=[True, True], ratio=[1, 1]):
super(USConv2d, self).__init__(
in_channels, out_channels,
kernel_size, stride=stride, padding=padding, dilation=dilation,
groups=groups, bias=bias)
self.depthwise = depthwise
self.in_channels_max = in_channels
self.out_channels_max = out_channels
self.width_mult = None
self.us = us
self.ratio = ratio
def forward(self, input):
if self.us[0]:
self.in_channels = make_divisible(
self.in_channels_max
* self.width_mult
/ self.ratio[0]) * self.ratio[0]
if self.us[1]:
self.out_channels = make_divisible(
self.out_channels_max
* self.width_mult
/ self.ratio[1]) * self.ratio[1]
self.groups = self.in_channels if self.depthwise else 1
weight = self.weight[:self.out_channels, :self.in_channels, :, :]
if self.bias is not None:
bias = self.bias[:self.out_channels]
else:
bias = self.bias
y = nn.functional.conv2d(
input, weight, bias, self.stride, self.padding,
self.dilation, self.groups)
if getattr(FLAGS, 'conv_averaged', False):
y = y * (max(self.in_channels_list) / self.in_channels)
return y
class USLinear(nn.Linear):
def __init__(self, in_features, out_features, bias=True, us=[True, True]):
super(USLinear, self).__init__(
in_features, out_features, bias=bias)
self.in_features_max = in_features
self.out_features_max = out_features
self.width_mult = None
self.us = us
def forward(self, input):
if self.us[0]:
self.in_features = make_divisible(
self.in_features_max * self.width_mult)
if self.us[1]:
self.out_features = make_divisible(
self.out_features_max * self.width_mult)
weight = self.weight[:self.out_features, :self.in_features]
if self.bias is not None:
bias = self.bias[:self.out_features]
else:
bias = self.bias
return nn.functional.linear(input, weight, bias)
class USBatchNorm2d(nn.BatchNorm2d):
def __init__(self, num_features, ratio=1):
super(USBatchNorm2d, self).__init__(
num_features, affine=True, track_running_stats=False)
self.num_features_max = num_features
# for tracking performance during training
self.bn = nn.ModuleList([
nn.BatchNorm2d(i, affine=False) for i in [
make_divisible(
self.num_features_max * width_mult / ratio) * ratio
for width_mult in FLAGS.width_mult_list]])
self.ratio = ratio
self.width_mult = None
self.ignore_model_profiling = True
def forward(self, input):
weight = self.weight
bias = self.bias
c = make_divisible(
self.num_features_max * self.width_mult / self.ratio) * self.ratio
if self.width_mult in FLAGS.width_mult_list:
idx = FLAGS.width_mult_list.index(self.width_mult)
y = nn.functional.batch_norm(
input,
self.bn[idx].running_mean[:c],
self.bn[idx].running_var[:c],
weight[:c],
bias[:c],
self.training,
self.momentum,
self.eps)
else:
y = nn.functional.batch_norm(
input,
self.running_mean,
self.running_var,
weight[:c],
bias[:c],
self.training,
self.momentum,
self.eps)
return y
def pop_channels(autoslim_channels):
return [i.pop(0) for i in autoslim_channels]
def bn_calibration_init(m):
""" calculating post-statistics of batch normalization """
if getattr(m, 'track_running_stats', False):
# reset all values for post-statistics
m.reset_running_stats()
# set bn in training mode to update post-statistics
m.training = True
# if use cumulative moving average
if getattr(FLAGS, 'cumulative_bn_stats', False):
m.momentum = None