forked from CVHub520/Convolution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResNeSt Block.py
274 lines (233 loc) · 6.7 KB
/
ResNeSt Block.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
'''
Components
'''
import torch
import torch.nn as nn
import torch.nn.functional as F
# source: https://github.com/STomoya/ResNeSt/blob/master/resnest/layers.py
'''
basic layers
'''
class GlobalAvgPool2d(nn.Module):
'''
global average pooling 2D class
'''
def __init__(self):
super(GlobalAvgPool2d, self).__init__()
def forward(self, x):
return F.adaptive_avg_pool2d(x, 1).view(x.size(0), -1)
class ConvBlock(nn.Module):
'''
convolution block class
convolution 2D -> batch normalization -> ReLU
'''
def __init__(self,
in_channels,
out_channels,
kernel_size,
stride,
padding
):
super(ConvBlock, self).__init__()
self.block = nn.Sequential(
nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding,
bias=False,
),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
def forward(self, x):
x = self.block(x)
return x
'''
Split Attention
'''
class rSoftMax(nn.Module):
'''
(radix-majorize) softmax class
input is cardinal-major shaped tensor.
transpose to radix-major
'''
def __init__(self,
groups=1,
radix=2
):
super(rSoftMax, self).__init__()
self.groups = groups
self.radix = radix
def forward(self, x):
B = x.size(0)
# transpose to radix-major
x = x.view(B, self.groups, self.radix, -1).transpose(1, 2)
x = F.softmax(x, dim=1)
x = x.view(B, -1, 1, 1)
return x
class SplitAttention(nn.Module):
'''
split attention class
'''
def __init__(self,
in_channels,
channels,
kernel_size,
stride=1,
padding=0,
dilation=1,
groups=1,
bias=True,
radix=2,
reduction_factor=4
):
super(SplitAttention, self).__init__()
self.radix = radix
self.radix_conv = nn.Sequential(
nn.Conv2d(
in_channels=in_channels,
out_channels=channels*radix,
kernel_size=kernel_size,
stride=stride,
padding=padding,
dilation=dilation,
groups=groups*radix,
bias=bias
),
nn.BatchNorm2d(channels*radix),
nn.ReLU(inplace=True)
)
inter_channels = max(32, in_channels*radix//reduction_factor)
self.attention = nn.Sequential(
nn.Conv2d(
in_channels=channels,
out_channels=inter_channels,
kernel_size=1,
groups=groups
),
nn.BatchNorm2d(inter_channels),
nn.ReLU(inplace=True),
nn.Conv2d(
in_channels=inter_channels,
out_channels=channels*radix,
kernel_size=1,
groups=groups
)
)
self.rsoftmax = rSoftMax(
groups=groups,
radix=radix
)
def forward(self, x):
# NOTE: comments are ugly...
'''
input : | in_channels |
'''
'''
radix_conv : | radix 0 | radix 1 | ... | radix r |
| group 0 | group 1 | ... | group k | group 0 | group 1 | ... | group k | ... | group 0 | group 1 | ... | group k |
'''
x = self.radix_conv(x)
'''
split : [ | group 0 | group 1 | ... | group k |, | group 0 | group 1 | ... | group k |, ... ]
sum : | group 0 | group 1 | ...| group k |
'''
B, rC = x.size()[:2]
splits = torch.split(x, rC // self.radix, dim=1)
gap = sum(splits)
'''
!! becomes cardinal-major !!
attention : | group 0 | group 1 | ... | group k |
| radix 0 | radix 1| ... | radix r | radix 0 | radix 1| ... | radix r | ... | radix 0 | radix 1| ... | radix r |
'''
att_map = self.attention(gap)
'''
!! transposed to radix-major in rSoftMax !!
rsoftmax : same as radix_conv
'''
att_map = self.rsoftmax(att_map)
'''
split : same as split
sum : same as sum
'''
att_maps = torch.split(att_map, rC // self.radix, dim=1)
out = sum([att_map*split for att_map, split in zip(att_maps, splits)])
'''
output : | group 0 | group 1 | ...| group k |
concatenated tensors of all groups,
which split attention is applied
'''
return out.contiguous()
'''
Bottleneck Block
'''
class BottleneckBlock(nn.Module):
'''
bottleneck block class
'''
expansion = 4
def __init__(self,
in_channels,
channels,
stride=1,
dilation=1,
downsample=None,
radix=2,
groups=1,
bottleneck_width=64,
is_first=False
):
super(BottleneckBlock, self).__init__()
group_width = int(channels * (bottleneck_width / 64.)) * groups
layers = [
ConvBlock(
in_channels=in_channels,
out_channels=group_width,
kernel_size=1,
stride=1,
padding=0
),
SplitAttention(
in_channels=group_width,
channels=group_width,
kernel_size=3,
stride=stride,
padding=dilation,
dilation=dilation,
groups=groups,
bias=False,
radix=radix
)
]
if stride > 1 or is_first:
layers.append(
nn.AvgPool2d(
kernel_size=3,
stride=stride,
padding=1
)
)
layers += [
nn.Conv2d(
group_width,
channels*4,
kernel_size=1,
bias=False
),
nn.BatchNorm2d(channels*4)
]
self.block = nn.Sequential(*layers)
self.downsample = downsample
def forward(self, x):
residual = x
if self.downsample:
residual = self.downsample(x)
out = self.block(x)
out += residual
return F.relu(out)
if __name__ == "__main__":
m = BottleneckBlock(256, 64)
x = torch.randn(3, 256, 4, 4)
print(m(x).size())