-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoperations.py
478 lines (390 loc) · 17 KB
/
operations.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
import torch.nn as nn
import torch
from torch.nn.functional import interpolate
OPS = {
'none': lambda c, stride, affine, dp: ZeroOp(c, c, stride=stride),
'identity': lambda c, stride, affine, dp: IdentityOp(c, c, affine=affine),
'cweight': lambda c, stride, affine, dp: CWeightOp(c, c, affine=affine, dropout_rate=dp),
'dil_conv': lambda c, stride, affine, dp: ConvOps(c, c, affine=affine, dilation=2, dropout_rate=dp),
'dep_conv': lambda c, stride, affine, dp: ConvOps(c, c, affine=affine, use_depthwise=True, dropout_rate=dp),
'shuffle_conv': lambda c, stride, affine, dp: ConvOps(c, c, affine=affine,has_shuffle=True),
'conv': lambda c, stride, affine, dp: ConvOps(c, c, affine=affine),
'avg_pool': lambda c, stride, affine, dp: PoolingOp(c, c, affine=affine, pool_type='avg'),
'max_pool': lambda c, stride, affine, dp: PoolingOp(c, c, affine=affine,pool_type='max'),
'down_cweight': lambda c, stride, affine, dp: CWeightOp(c, c, stride=2, affine=affine, dropout_rate=dp),
'down_dil_conv': lambda c, stride, affine, dp: ConvOps(c, c, stride=2, affine=affine, dilation=2, dropout_rate=dp),
'down_dep_conv': lambda c, stride, affine, dp: ConvOps(c, c, stride=2, affine=affine, use_depthwise=True, dropout_rate=dp),
'down_conv': lambda c, stride, affine, dp: ConvOps(c, c, stride=2, affine=affine, dropout_rate=dp),
'up_cweight': lambda c, stride, affine, dp: CWeightOp(c, c, stride=2, affine=affine,use_transpose=True, dropout_rate=dp),
'up_dep_conv': lambda c, stride, affine, dp: ConvOps(c, c, stride=2, affine=affine,use_depthwise=True, use_transpose=True, dropout_rate=dp),
'up_conv': lambda c, stride, affine, dp: ConvOps(c, c, stride=2, affine=affine, use_transpose=True, dropout_rate=dp),
'up_dil_conv': lambda c, stride, affine, dp: ConvOps(c, c, stride=2, affine=affine, dilation=2,use_transpose=True, dropout_rate=dp),
}
def consistent_dim(states):
# handle the un-consistent dimension
# Todo: zbabby
# concatenate all meta-node to output along channels dimension
h_max, w_max = 0, 0
for ss in states:
if h_max < ss.size()[2]:
h_max = ss.size()[2]
if w_max < ss.size()[3]:
w_max = ss.size()[3]
return [interpolate(ss, (h_max, w_max)) for ss in states]
def get_same_padding(kernel_size):
if isinstance(kernel_size, tuple):
assert len(kernel_size) == 2, 'invalid kernel size: %s' % 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
def shuffle_layer(x, groups):
batchsize, num_channels, height, width = x.data.size()
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
class AbstractOp(nn.Module):
def forward(self, x):
raise NotImplementedError
@property
def unit_str(self):
raise NotImplementedError
@property
def config(self):
raise NotImplementedError
@staticmethod
def build_from_config(config):
raise NotImplementedError
class BaseOp(AbstractOp):
def __init__(self, in_channels, out_channels, norm_type='gn', use_norm=True, affine=True,
act_func='relu', dropout_rate=0, ops_order='weight_norm_act' ):
super(BaseOp, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.use_norm = use_norm
self.act_func = act_func
self.dropout_rate = dropout_rate
self.ops_order = ops_order
self.norm_type = norm_type
# batch norm, group norm, instance norm, layer norm
if self.use_norm:
# Ref: <Group Normalization> https://arxiv.org/abs/1803.08494
# 16 channels for one group is best
if self.norm_before_weight:
group = 1 if in_channels % 16 != 0 else in_channels // 16
if norm_type == 'gn':
self.norm = nn.GroupNorm(group, in_channels, affine=affine)
else:
self.norm = nn.BatchNorm2d(in_channels, affine=affine)
else:
group = 1 if out_channels % 16 != 0 else out_channels // 16
if norm_type == 'gn':
self.norm = nn.GroupNorm(group, out_channels, affine=affine)
else:
self.norm = nn.BatchNorm2d(out_channels, affine=affine)
else:
self.norm = None
# activation
if act_func == 'relu':
if self.ops_list[0] == 'act':
self.activation = nn.ReLU(inplace=False)
else:
self.activation = nn.ReLU(inplace=True)
elif act_func == 'relu6':
if self.ops_list[0] == 'act':
self.activation = nn.ReLU6(inplace=False)
else:
self.activation = nn.ReLU6(inplace=True)
else:
self.activation = None
# dropout
if self.dropout_rate > 0:
self.dropout = nn.Dropout2d(self.dropout_rate, inplace=False)
else:
self.dropout = None
@property
def ops_list(self):
return self.ops_order.split('_')
@property
def norm_before_weight(self):
for op in self.ops_list:
if op == 'norm':
return True
elif op == 'weight':
return False
raise ValueError('Invalid ops_order: %s' % self.ops_order)
@property
def unit_str(self):
raise NotImplementedError
@property
def config(self):
return{
'in_channels': self.in_channels,
'out_channels': self.out_channels,
'use_norm': self.use_norm,
'act_func': self.act_func,
'dropout_rate': self.dropout_rate,
'ops_order': self.ops_order
}
@staticmethod
def build_from_config(config):
raise NotImplementedError
@staticmethod
def is_zero_ops():
return False
def get_flops(self, x):
raise NotImplementedError
def weight_call(self, x):
raise NotImplementedError
def forward(self, x):
for op in self.ops_list:
if op == 'weight':
# dropout before weight operation
if self.dropout is not None:
x = self.dropout(x)
x = self.weight_call(x)
elif op == 'norm':
if self.norm is not None:
x = self.norm(x)
elif op == 'act':
if self.activation is not None:
x = self.activation(x)
else:
raise ValueError('Unrecognized op: %s' % op)
return x
class ConvOps(BaseOp):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1,dilation=1,groups=1,
bias=False, has_shuffle=False, use_transpose=False, output_padding=0, use_depthwise=False,
norm_type='gn', use_norm=True, affine=True, act_func='relu', dropout_rate=0, ops_order='weight_norm_act'):
super(ConvOps, self).__init__(in_channels, out_channels, norm_type, use_norm, affine, act_func, dropout_rate, ops_order)
self.kernel_size = kernel_size
self.stride = stride
self.dilation = dilation
self.groups = groups
self.bias = bias
self.has_shuffle = has_shuffle
self.use_transpose = use_transpose
self.use_depthwise = use_depthwise
self.output_padding = output_padding
padding = get_same_padding(self.kernel_size)
if isinstance(padding, int):
padding *= self.dilation
else:
padding[0] *= self.dilation
padding[1] *= self.dilation
# 'kernel_size', 'stride', 'padding', 'dilation' can either be 'int' or 'tuple' of int
if use_transpose:
if use_depthwise: # 1. transpose depth-wise conv
self.depth_conv = nn.ConvTranspose2d(in_channels, in_channels, kernel_size=self.kernel_size,
stride=self.stride, padding=padding, output_padding=self.output_padding, groups=in_channels, bias=self.bias)
self.point_conv = nn.Conv2d(in_channels, out_channels, kernel_size=1,
groups=self.groups, bias=False)
else: # 2. transpose conv
self.conv = nn.ConvTranspose2d(in_channels, out_channels, kernel_size=self.kernel_size,
stride=self.stride, padding=padding,
output_padding=self.output_padding, dilation=self.dilation, bias=self.bias)
else:
if use_depthwise: # 3. depth-wise conv
self.depth_conv = nn.Conv2d(in_channels, in_channels, kernel_size=self.kernel_size,
stride=self.stride, padding=padding,
dilation=self.dilation, groups=in_channels, bias=False)
self.point_conv = nn.Conv2d(in_channels, out_channels, kernel_size=1,
groups=self.groups, bias=False)
else: # 4. conv
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=self.kernel_size,
stride=self.stride, padding=padding,
dilation=self.dilation, bias=False)
@property
def unit_str(self):
if isinstance(self.kernel_size, int):
kernel_size = (self.kernel_size, self.kernel_size)
else:
kernel_size = self.kernel_size
basic_str = 'Conv'
basic_str = 'Dilation' + basic_str if self.dilation > 1 else basic_str
basic_str = 'Depth' + basic_str if self.use_depthwise else basic_str
basic_str = 'Group' + basic_str if self.groups > 1 else basic_str
basic_str = 'Tran' + basic_str if self.use_transpose else basic_str
basic_str = '%dx%d_' % (kernel_size[0], kernel_size[1]) + basic_str
return basic_str
@property
def config(self):
config = {
'name': ConvOps.__name__,
'kernel_size': self.kernel_size,
'stride': self.stride,
'dilation': self.dilation,
'groups': self.groups,
'bias': self.bias,
'has_shuffle': self.has_shuffle,
'depth_wise': self.use_depthwise,
'transpose': self.use_transpose,
}
config.update(super(ConvOps, self).config)
return config
@staticmethod
def build_from_config(config):
return ConvOps(**config)
def weight_call(self, x):
if self.use_depthwise:
x = self.depth_conv(x)
x = self.point_conv(x)
else:
x = self.conv(x)
if self.has_shuffle and self.groups > 1:
x = shuffle_layer(x, self.groups)
return x
class CWeightOp(BaseOp):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1,dilation=1, groups=None,
bias=False, has_shuffle=False, use_transpose=False,output_padding=0, norm_type='gn',
use_norm=False, affine=True, act_func=None, dropout_rate=0, ops_order='weight'):
super(CWeightOp, self).__init__(in_channels, out_channels, norm_type, use_norm, affine, act_func, dropout_rate, ops_order)
self.kernel_size = kernel_size
self.stride = stride
self.dilation = dilation
self.groups = groups
self.bias = bias
self.has_shuffle = has_shuffle
self.use_transpose = use_transpose
self.output_padding = output_padding
padding = get_same_padding(self.kernel_size)
if isinstance(padding, int):
padding *= self.dilation
else:
padding[0] *= self.dilation
padding[1] *= self.dilation
# `kernel_size`, `stride`, `padding`, `dilation`
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(in_channels, in_channels // 16),
nn.ReLU(inplace=True),
nn.Linear(in_channels // 16, out_channels),
nn.Sigmoid()
)
if stride >= 2:
if use_transpose:
self.conv = nn.ConvTranspose2d(in_channels, out_channels, kernel_size=self.kernel_size,
stride=self.stride, padding=padding, output_padding=self.output_padding,
bias=False)
else:
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size,
stride=stride, padding=padding, bias=False)
group = 1 if out_channels % 16 != 0 else out_channels // 16
self.norm = nn.GroupNorm(group, out_channels, affine=affine)
@property
def unit_str(self):
if isinstance(self.kernel_size, int):
kernel_size = (self.kernel_size, self.kernel_size)
else:
kernel_size = self.kernel_size
basic_str = 'ChannelWeight'
basic_str = 'Tran' + basic_str if self.use_transpose else basic_str
return basic_str
@staticmethod
def build_from_config(config):
return CWeightOp(**config)
def weight_call(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
rst = self.norm(self.conv(x*y)) if self.stride >= 2 else x*y
return rst
class PoolingOp(BaseOp):
def __init__(self, in_channels, out_channels, pool_type, kernel_size=2, stride=2,
norm_type='gn', use_norm=False, affine=True, act_func=None, dropout_rate=0, ops_order='weight'):
super(PoolingOp, self).__init__(in_channels, out_channels, norm_type, use_norm, affine, act_func, dropout_rate, ops_order)
self.pool_type = pool_type
self.kernel_size = kernel_size
self.stride = stride
if self.stride == 1:
padding = get_same_padding(self.kernel_size)
else:
padding = 0
if self.pool_type == 'avg':
self.pool = nn.AvgPool2d(self.kernel_size, stride=self.stride, padding=padding, count_include_pad=False)
elif self.pool_type == 'max':
self.pool = nn.MaxPool2d(self.kernel_size, stride=self.stride, padding=padding)
else:
raise NotImplementedError
@property
def unit_str(self):
if isinstance(self.kernel_size, int):
kernel_size = (self.kernel_size, self.kernel_size)
else:
kernel_size = self.kernel_size
return '%dx%d_%sPool' % (kernel_size[0], kernel_size[1], self.pool_type.upper())
@property
def config(self):
config = {
'name': PoolingOp.__name__,
'pool_type': self.pool_type,
'kernel_size': self.kernel_size,
'stride': self.stride
}
config.update(super(PoolingOp, self).config)
return config
@staticmethod
def build_from_config(config):
return PoolingOp(**config)
def get_flops(self, x):
return 0, self.forward(x)
def weight_call(self, x):
return self.pool(x)
class IdentityOp(BaseOp):
def __init__(self, in_channels, out_channels, norm_type='gn', use_norm=False, affine=True,
act_func=None, dropout_rate=0, ops_order='weight_norm_act'):
super(IdentityOp, self).__init__(in_channels, out_channels, norm_type,use_norm, affine,
act_func, dropout_rate, ops_order)
@property
def unit_str(self):
return 'Identity'
@property
def config(self):
config = {
'name': IdentityOp.__name__,
}
config.update(super(IdentityOp, self).config)
return config
@staticmethod
def build_from_config(config):
return IdentityOp(**config)
def get_flops(self, x):
return 0, self.forward(x)
def weight_call(self, x):
return x
class ZeroOp(BaseOp):
def __init__(self, in_channels, out_channels, stride):
super(ZeroOp, self).__init__(in_channels, out_channels)
self.stride = stride
@property
def unit_str(self):
return 'Zero'
@property
def config(self):
return {
'name': ZeroOp.__name__,
'stride': self.stride,
}
@staticmethod
def build_from_config(config):
return ZeroOp(**config)
def get_flops(self, x):
return 0, self.forward(x)
def forward(self, x):
n, c, h, w = x.size()
h //= self.stride
w //= self.stride
if x.is_cuda:
with torch.cuda.device(x.get_device()):
padding = torch.cuda.FloatTensor(n, c, h, w).fill_(0)
else:
padding = torch.zeros(n, c, h, w)
padding = torch.autograd.Variable(padding, requires_grad=False)
return padding