-
Notifications
You must be signed in to change notification settings - Fork 433
/
TF_NAS.py
492 lines (432 loc) · 13.3 KB
/
TF_NAS.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
"""
@author: Yibo Hu, Jun Wang
@date: 20201019
@contact: [email protected]
"""
import sys
import torch
import torch.nn as nn
import torch.nn.functional as F
from collections import OrderedDict
def channel_shuffle(x, groups):
assert groups > 1
batchsize, num_channels, height, width = x.size()
assert (num_channels % groups == 0)
channels_per_group = num_channels // groups
# reshape
x = x.view(batchsize, groups, channels_per_group, height, width)
# transpose
x = torch.transpose(x, 1, 2).contiguous()
# flatten
x = x.view(batchsize, -1, height, width)
return x
def get_same_padding(kernel_size):
if isinstance(kernel_size, tuple):
assert len(kernel_size) == 2, 'invalid kernel size: {}'.format(kernel_size)
p1 = get_same_padding(kernel_size[0])
p2 = get_same_padding(kernel_size[1])
return p1, p2
assert isinstance(kernel_size, int), 'kernel size should be either `int` or `tuple`'
assert kernel_size % 2 > 0, 'kernel size should be odd number'
return kernel_size // 2
class Swish(nn.Module):
def __init__(self, inplace=False):
super(Swish, self).__init__()
self.inplace = inplace
def forward(self, x):
if self.inplace:
return x.mul_(x.sigmoid())
else:
return x * x.sigmoid()
class HardSwish(nn.Module):
def __init__(self, inplace=False):
super(HardSwish, self).__init__()
self.inplace = inplace
def forward(self, x):
if self.inplace:
return x.mul_(F.relu6(x + 3., inplace=True) / 6.)
else:
return x * F.relu6(x + 3.) /6.
class BasicLayer(nn.Module):
def __init__(
self,
in_channels,
out_channels,
use_bn=True,
affine = True,
act_func='relu6',
ops_order='weight_bn_act'):
super(BasicLayer, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.use_bn = use_bn
self.affine = affine
self.act_func = act_func
self.ops_order = ops_order
""" add modules """
# batch norm
if self.use_bn:
if self.bn_before_weight:
self.bn = nn.BatchNorm2d(in_channels, affine=affine, track_running_stats=affine)
else:
self.bn = nn.BatchNorm2d(out_channels, affine=affine, track_running_stats=affine)
else:
self.bn = None
# activation
if act_func == 'relu':
if self.ops_list[0] == 'act':
self.act = nn.ReLU(inplace=False)
else:
self.act = nn.ReLU(inplace=True)
elif act_func == 'relu6':
if self.ops_list[0] == 'act':
self.act = nn.ReLU6(inplace=False)
else:
self.act = nn.ReLU6(inplace=True)
elif act_func == 'swish':
if self.ops_list[0] == 'act':
self.act = Swish(inplace=False)
else:
self.act = Swish(inplace=True)
elif act_func == 'h-swish':
if self.ops_list[0] == 'act':
self.act = HardSwish(inplace=False)
else:
self.act = HardSwish(inplace=True)
else:
self.act = None
@property
def ops_list(self):
return self.ops_order.split('_')
@property
def bn_before_weight(self):
for op in self.ops_list:
if op == 'bn':
return True
elif op == 'weight':
return False
raise ValueError('Invalid ops_order: %s' % self.ops_order)
def weight_call(self, x):
raise NotImplementedError
def forward(self, x):
for op in self.ops_list:
if op == 'weight':
x = self.weight_call(x)
elif op == 'bn':
if self.bn is not None:
x = self.bn(x)
elif op == 'act':
if self.act is not None:
x = self.act(x)
else:
raise ValueError('Unrecognized op: %s' % op)
return x
class ConvLayer(BasicLayer):
def __init__(
self,
in_channels,
out_channels,
kernel_size=3,
stride=1,
groups=1,
has_shuffle=False,
bias=False,
use_bn=True,
affine=True,
act_func='relu6',
ops_order='weight_bn_act'):
super(ConvLayer, self).__init__(
in_channels,
out_channels,
use_bn,
affine,
act_func,
ops_order)
self.kernel_size = kernel_size
self.stride = stride
self.groups = groups
self.has_shuffle = has_shuffle
self.bias = bias
padding = get_same_padding(self.kernel_size)
self.conv = nn.Conv2d(
in_channels,
out_channels,
kernel_size=self.kernel_size,
stride=self.stride,
padding=padding,
groups=self.groups,
bias=self.bias)
def weight_call(self, x):
x = self.conv(x)
if self.has_shuffle and self.groups > 1:
x = channel_shuffle(x, self.groups)
return x
class LinearLayer(nn.Module):
def __init__(
self,
in_features,
out_features,
bias=True,
use_bn=False,
affine=False,
act_func=None,
ops_order='weight_bn_act'):
super(LinearLayer, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.bias = bias
self.use_bn = use_bn
self.affine = affine
self.act_func = act_func
self.ops_order = ops_order
""" add modules """
# batch norm
if self.use_bn:
if self.bn_before_weight:
self.bn = nn.BatchNorm1d(in_features, affine=affine, track_running_stats=affine)
else:
self.bn = nn.BatchNorm1d(out_features, affine=affine, track_running_stats=affine)
else:
self.bn = None
# activation
if act_func == 'relu':
if self.ops_list[0] == 'act':
self.act = nn.ReLU(inplace=False)
else:
self.act = nn.ReLU(inplace=True)
elif act_func == 'relu6':
if self.ops_list[0] == 'act':
self.act = nn.ReLU6(inplace=False)
else:
self.act = nn.ReLU6(inplace=True)
elif act_func == 'tanh':
self.act = nn.Tanh()
elif act_func == 'sigmoid':
self.act = nn.Sigmoid()
else:
self.act = None
# linear
self.linear = nn.Linear(self.in_features, self.out_features, self.bias)
@property
def ops_list(self):
return self.ops_order.split('_')
@property
def bn_before_weight(self):
for op in self.ops_list:
if op == 'bn':
return True
elif op == 'weight':
return False
raise ValueError('Invalid ops_order: %s' % self.ops_order)
def forward(self, x):
for op in self.ops_list:
if op == 'weight':
x = self.linear(x)
elif op == 'bn':
if self.bn is not None:
x = self.bn(x)
elif op == 'act':
if self.act is not None:
x = self.act(x)
else:
raise ValueError('Unrecognized op: %s' % op)
return x
class MBInvertedResBlock(nn.Module):
def __init__(
self,
in_channels,
mid_channels,
se_channels,
out_channels,
kernel_size=3,
stride=1,
groups=1,
has_shuffle=False,
bias=False,
use_bn=True,
affine=True,
act_func='relu6'):
super(MBInvertedResBlock, self).__init__()
self.in_channels = in_channels
self.mid_channels = mid_channels
self.se_channels = se_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.stride = stride
self.groups = groups
self.has_shuffle = has_shuffle
self.bias = bias
self.use_bn = use_bn
self.affine = affine
self.act_func = act_func
# inverted bottleneck
if mid_channels > in_channels:
inverted_bottleneck = OrderedDict([
('conv', nn.Conv2d(in_channels, mid_channels, 1, 1, 0, groups=groups, bias=bias)),
])
if use_bn:
inverted_bottleneck['bn'] = nn.BatchNorm2d(mid_channels, affine=affine, track_running_stats=affine)
if act_func == 'relu':
inverted_bottleneck['act'] = nn.ReLU(inplace=True)
elif act_func == 'relu6':
inverted_bottleneck['act'] = nn.ReLU6(inplace=True)
elif act_func == 'swish':
inverted_bottleneck['act'] = Swish(inplace=True)
elif act_func == 'h-swish':
inverted_bottleneck['act'] = HardSwish(inplace=True)
self.inverted_bottleneck = nn.Sequential(inverted_bottleneck)
else:
self.inverted_bottleneck = None
self.mid_channels = in_channels
mid_channels = in_channels
# depthwise convolution
padding = get_same_padding(self.kernel_size)
depth_conv = OrderedDict([
('conv',
nn.Conv2d(
mid_channels,
mid_channels,
kernel_size,
stride,
padding,
groups=mid_channels,
bias=bias)),
])
if use_bn:
depth_conv['bn'] = nn.BatchNorm2d(mid_channels, affine=affine, track_running_stats=affine)
if act_func == 'relu':
depth_conv['act'] = nn.ReLU(inplace=True)
elif act_func == 'relu6':
depth_conv['act'] = nn.ReLU6(inplace=True)
elif act_func == 'swish':
depth_conv['act'] = Swish(inplace=True)
elif act_func == 'h-swish':
depth_conv['act'] = HardSwish(inplace=True)
self.depth_conv = nn.Sequential(depth_conv)
# se model
if se_channels > 0:
squeeze_excite = OrderedDict([
('conv_reduce', nn.Conv2d(mid_channels, se_channels, 1, 1, 0, groups=groups, bias=True)),
])
if act_func == 'relu':
squeeze_excite['act'] = nn.ReLU(inplace=True)
elif act_func == 'relu6':
squeeze_excite['act'] = nn.ReLU6(inplace=True)
elif act_func == 'swish':
squeeze_excite['act'] = Swish(inplace=True)
elif act_func == 'h-swish':
squeeze_excite['act'] = HardSwish(inplace=True)
squeeze_excite['conv_expand'] = nn.Conv2d(se_channels, mid_channels, 1, 1, 0, groups=groups, bias=True)
self.squeeze_excite = nn.Sequential(squeeze_excite)
else:
self.squeeze_excite = None
self.se_channels = 0
# pointwise linear
point_linear = OrderedDict([
('conv', nn.Conv2d(mid_channels, out_channels, 1, 1, 0, groups=groups, bias=bias)),
])
if use_bn:
point_linear['bn'] = nn.BatchNorm2d(out_channels, affine=affine, track_running_stats=affine)
self.point_linear = nn.Sequential(point_linear)
# residual flag
self.has_residual = (in_channels == out_channels) and (stride == 1)
def forward(self, x):
res = x
if self.inverted_bottleneck is not None:
x = self.inverted_bottleneck(x)
if self.has_shuffle and self.groups > 1:
x = channel_shuffle(x, self.groups)
x = self.depth_conv(x)
if self.squeeze_excite is not None:
x_se = F.adaptive_avg_pool2d(x, 1)
x = x * torch.sigmoid(self.squeeze_excite(x_se))
x = self.point_linear(x)
if self.has_shuffle and self.groups > 1:
x = channel_shuffle(x, self.groups)
if self.has_residual:
x += res
return x
class Flatten(nn.Module):
def forward(self, x):
return x.view(x.size(0), -1)
class TF_NAS_A(nn.Module):
def __init__(self, out_h, out_w, feat_dim, drop_ratio=0.0):
super(TF_NAS_A, self).__init__()
self.drop_ratio = drop_ratio
self.first_stem = ConvLayer(3, 32, kernel_size=3, stride=1, act_func='relu')
self.second_stem = MBInvertedResBlock(32, 32, 8, 16, kernel_size=3, stride=1, act_func='relu')
self.stage1 = nn.Sequential(
MBInvertedResBlock(16, 83, 32, 24, kernel_size=3, stride=2, act_func='relu'),
MBInvertedResBlock(24, 128, 0, 24, kernel_size=5, stride=1, act_func='relu'),
)
self.stage2 = nn.Sequential(
MBInvertedResBlock(24, 138, 48, 40, kernel_size=3, stride=2, act_func='swish'),
MBInvertedResBlock(40, 297, 0, 40, kernel_size=3, stride=1, act_func='swish'),
MBInvertedResBlock(40, 170, 80, 40, kernel_size=5, stride=1, act_func='swish'),
)
self.stage3 = nn.Sequential(
MBInvertedResBlock(40, 248, 80, 80, kernel_size=5, stride=2, act_func='swish'),
MBInvertedResBlock(80, 500, 0, 80, kernel_size=3, stride=1, act_func='swish'),
MBInvertedResBlock(80, 424, 0, 80, kernel_size=3, stride=1, act_func='swish'),
MBInvertedResBlock(80, 477, 0, 80, kernel_size=3, stride=1, act_func='swish'),
)
self.stage4 = nn.Sequential(
MBInvertedResBlock(80, 504, 160, 112, kernel_size=3, stride=1, act_func='swish'),
MBInvertedResBlock(112, 796, 0, 112, kernel_size=3, stride=1, act_func='swish'),
MBInvertedResBlock(112, 723, 224, 112, kernel_size=3, stride=1, act_func='swish'),
MBInvertedResBlock(112, 555, 224, 112, kernel_size=3, stride=1, act_func='swish'),
)
self.stage5 = nn.Sequential(
MBInvertedResBlock(112, 813, 0, 192, kernel_size=3, stride=2, act_func='swish'),
MBInvertedResBlock(192, 1370, 0, 192, kernel_size=3, stride=1, act_func='swish'),
MBInvertedResBlock(192, 1138, 384, 192, kernel_size=3, stride=1, act_func='swish'),
MBInvertedResBlock(192, 1359, 384, 192, kernel_size=3, stride=1, act_func='swish'),
)
self.stage6 = nn.Sequential(
MBInvertedResBlock(192, 1203, 384, 320, kernel_size=5, stride=1, act_func='swish'),
)
self.feature_mix_layer = ConvLayer(320, 1280, kernel_size=1, stride=1, act_func='none')
self.output_layer = nn.Sequential(
nn.Dropout(self.drop_ratio),
Flatten(),
nn.Linear(1280 * out_h * out_w, feat_dim),
nn.BatchNorm1d(feat_dim))
self._initialization()
def forward(self, x):
x = self.first_stem(x)
x = self.second_stem(x)
for block in self.stage1:
x = block(x)
for block in self.stage2:
x = block(x)
for block in self.stage3:
x = block(x)
for block in self.stage4:
x = block(x)
for block in self.stage5:
x = block(x)
for block in self.stage6:
x = block(x)
x = self.feature_mix_layer(x)
x = self.output_layer(x)
return x
def _initialization(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
if m.weight is not None:
nn.init.constant_(m.weight, 1)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
if __name__ == '__main__':
x = torch.rand((2,3,112,112))
net = TF_NAS_A(7, 7, 512, drop_ratio=0.0)
x = x.cuda()
net = net.cuda()
out = net(x)
print(out.size())