-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers.py
399 lines (274 loc) · 10.3 KB
/
layers.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
from __init__ import *
from utils import round_width
def Depthwise_Conv2d(
channels: int,
kernel_size: int,
stride: int,
padding: int = None,
dilation: int = 1,
bias: bool = False):
"""
__version__ = 1.0
__date__ = 2022.03.07
__paper__ = https://arxiv.org/abs/1704.04861
The structure is decribed in <Figure 2.(b)> of the paper.
'padding' is set to retain 2D-size of a feature, if it is not given.
"""
if not padding and padding != 0:
padding = dilation * (kernel_size - 1) // 2
dw_conv = nn.Conv2d(channels, channels, kernel_size, stride, padding, dilation,
groups=channels, bias=bias)
return dw_conv
def Pointwise_Conv2d(
in_channels: int,
out_channels: int,
bias: bool = False):
"""
__version__ = 1.0
__date__ = 2022.03.07
__paper__ = https://arxiv.org/abs/1704.04861
The structure is decribed in <Figure 2.(c)> of the paper.
"""
pw_conv = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, bias=bias)
return pw_conv
def Seperable_Conv2d(
in_channels: int,
out_channels: int,
kernel_size: int = 3,
stride: int = 1,
padding: int = None,
dilation: int = 1,
bias: bool = False):
"""
__version__ = 1.0
__date__ = 2022.03.07
"""
conv = nn.Sequential(Depthwise_Conv2d(in_channels, kernel_size, stride, padding, dilation, False),
Pointwise_Conv2d(in_channels, out_channels, bias))
return conv
class Mish(nn.Module):
__doc__ = r"""
__version__ = 1.0
__date__ = 2022.03.07
__paper__ = https://arxiv.org/abs/1908.08681
"""
def __init__(self,
beta: int = 1,
threshold: int = 20):
super(Mish, self).__init__()
self.beta = beta
self.threshold = threshold
def forward(self, x):
return x * F.softplus(x, self.beta, self.threshold).tanh()
class H_Sigmoid(nn.Module):
__doc__ = r"""
__version__ = 1.0
__date__ = 2022.03.07
__paper__ = https://arxiv.org/abs/1905.02244
"""
def __init__(self, inplace:bool = False):
super(H_Sigmoid, self).__init__()
self.inplace = inplace
def forward(self, x):
return F.relu6(x + 3, inplace=self.inplace) / 6
class H_Swish(nn.Module):
__doc__ = r"""
__version__ = 1.0
__date__ = 2022.03.07
__paper__ = https://arxiv.org/abs/1905.02244
"""
def __init__(self, inplace:bool = False):
super(H_Swish, self).__init__()
self.inplace = inplace
def forward(self, x):
return x * F.relu6(x + 3, inplace=self.inplace) / 6
class L2_Norm(nn.Module):
__doc__ = r"""
__version__ = 1.0
__date__ = 2022.03.07
__paper__ = https://arxiv.org/abs/1205.2653
"""
def __init__(self,
channels: int,
eps: float = 1e-10):
super(L2_Norm,self).__init__()
self.weight = nn.Parameter(torch.Tensor(channels))
self.channels = channels
self.eps = eps
def forward(self, x):
norm = x.pow(2).sum(1, True).sqrt() + self.eps
x = torch.div(x, norm)
x = self.weight.unsqueeze(0).unsqueeze(2).unsqueeze(3).expand_as(x) * x
return x
class Static_ConvLayer(nn.Module):
__doc__ = r"""
__version__ = 1.0
__date__ = 2022.03.07
The module selectively comprises convolution, batch normalizaion, activation in general order.
'padding', 'dilation', 'groups' of nn.Conv2d are given constantly, according to 'Static'.
'Act' receives an instance, not a class.
"""
def __init__(self,
in_channels: int,
out_channels: int,
kernel_size: int = 3,
stride: int = 1,
bias: bool = False,
batch_norm: bool = True,
Act: None or nn.Module = nn.ReLU(inplace=False),
**kwargs):
batch_eps = kwargs.get('eps', 1e-05)
batch_momentum = kwargs.get('momentum', 0.1)
padding = (kernel_size - 1) // 2
super(Static_ConvLayer, self).__init__()
layer = [nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, bias=bias)]
if batch_norm:
layer.append(nn.BatchNorm2d(out_channels, eps=batch_eps, momentum=batch_momentum))
if Act:
layer.append(Act)
self.layer = nn.Sequential(*layer)
def forward(self, x):
return self.layer(x)
class Dynamic_ConvLayer(nn.Module):
__doc__ = r"""
__version__ = 1.0
__date__ = 2022.03.07
'reverse' orders components in various ways.
'padding' is set to retain 2D-size of a feature, if it is not given.
"""
def __init__(self,
in_channels: int,
out_channels: int,
kernel_size: int = 3,
stride: int = 1,
padding: int = None,
dilation: int = 1,
groups: int = 1,
bias: bool = False,
batch_norm: bool = True,
Act: None or nn.Module = nn.ReLU(inplace=False),
reverse: str = None,
**kwargs):
batch_eps = kwargs.get('eps', 1e-05)
batch_momentum = kwargs.get('momentum', 0.1)
if not padding and padding != 0:
padding = dilation * (kernel_size - 1) // 2
super(Dynamic_ConvLayer, self).__init__()
layer = [nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias)]
if not reverse:
if batch_norm:
layer.append(nn.BatchNorm2d(out_channels, eps=batch_eps, momentum=batch_momentum))
if Act:
layer.append(Act)
elif reverse == 'ACB':
if batch_norm:
layer.append(nn.BatchNorm2d(out_channels, eps=batch_eps, momentum=batch_momentum))
if Act:
layer.insert(0, Act)
elif reverse == 'BAC':
if batch_norm:
layer.insert(0, nn.BatchNorm2d(in_channels, eps=batch_eps, momentum=batch_momentum))
if Act:
layer.insert(-1, Act)
elif reverse == 'ABC':
if batch_norm:
layer.insert(0, nn.BatchNorm2d(in_channels, eps=batch_eps, momentum=batch_momentum))
if Act:
layer.insert(0, Act)
else:
raise ValueError('reverse order should be one of ACB, BAC, ABC')
self.layer = nn.Sequential(*layer)
def forward(self, x):
return self.layer(x)
class Squeeze_Excitation(nn.Module):
__doc__ = r"""
__version__ = 1.0
__date__ = 2022.03.07
__paper__ = https://arxiv.org/abs/1709.01507
The structure is decribed in <Figure 2.(right)> of the paper.
'reduction' is a denominator of reduction ratio of squeezing, following the paper.
"""
def __init__(self,
in_channels: int,
channels: int,
reduction: float,
batch_norm: bool = False,
Act: nn.Module = nn.ReLU(),
Sigmoid: nn.Module = nn.Sigmoid(),
**kwargs):
divisor = kwargs.get('divisor', 1)
round_bias = kwargs.get('round_bias', 0.9)
reduct_channels = round_width(channels // reduction, divisor, round_bias)
super(Squeeze_Excitation, self).__init__()
squeeze = [nn.AdaptiveAvgPool2d(1),
nn.Flatten(),
nn.Linear(in_channels, reduct_channels)]
if not batch_norm:
squeeze.append(Act)
else:
squeeze.append(nn.BatchNorm2d(reduct_channels))
squeeze.append(Act)
excitation = [nn.Linear(reduct_channels, channels),
Sigmoid]
self.squeeze = nn.Sequential(*squeeze)
self.excitation = nn.Sequential(*excitation)
def forward(self, input):
batch, channel, _, _ = input.size()
x = self.squeeze(input)
x = self.excitation(x)
x = x.view(batch, channel, 1, 1)
x = x * input
return x
class Squeeze_Excitation_Conv(nn.Module):
__doc__ = r"""
__version__ = 1.0
__date__ = 2022.03.07
From Squeeze_Excitation, nn.Linear is replaced with nn.Conv2d with filter of 1.
"""
def __init__(self,
in_channels: int,
channels: int,
reduction: float,
batch_norm: bool = False,
Act: nn.Module = nn.ReLU(),
Sigmoid: nn.Module = nn.Sigmoid(),
**kwargs):
divisor = kwargs.get('divisor', 1)
round_bias = kwargs.get('round_bias', 0.9)
reduct_channels = round_width(channels // reduction, divisor, round_bias)
super(Squeeze_Excitation_Conv, self).__init__()
squeeze = [nn.AdaptiveAvgPool2d(1),
nn.Conv2d(in_channels, reduct_channels, kernel_size=1)]
if not batch_norm:
squeeze.append(Act)
else:
squeeze.append(nn.BatchNorm2d(reduct_channels))
squeeze.append(Act)
excitation = [nn.Conv2d(reduct_channels, channels, kernel_size=1),
Sigmoid]
self.squeeze = nn.Sequential(*squeeze)
self.excitation = nn.Sequential(*excitation)
def forward(self, input):
x = self.squeeze(input)
x = self.excitation(x)
x = x * input
return x
class SPP(nn.Module):
__doc__ = r"""
__version__ = 1.0
__date__ = 2022.03.07
__paper__ = https://arxiv.org/abs/1406.4729
Each pooling is followed by flattening to vector in the original, but here, it isn't.
"""
def __init__(self,
kernel_sizes: list,
inverse: bool = False):
super(SPP, self).__init__()
self.pools = nn.ModuleList([nn.MaxPool2d(k, 1, padding=k // 2) for k in kernel_sizes])
self.inverse = inverse
def forward(self, x):
x = [x] + [p(x) for p in self.pools]
if self.inverse:
x = x[::-1]
x = torch.cat(x, 1)
return x