-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathattentionnet.py
380 lines (291 loc) · 12.6 KB
/
attentionnet.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
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from collections import OrderedDict
from typing import Type, Any, Callable, Union, List, Optional
__all__ = [
'Attention', 'attention56', 'attention92'
]
''' This code is based on https://github.com/weiaicunzai/pytorch-cifar100.git, with modification on base network'''
''' Residual Bottleneck from Torch Vision'''
def conv3x3(in_planes: int, out_planes: int, stride: int = 1, groups: int = 1, dilation: int = 1) -> nn.Conv2d:
"""3x3 convolution with padding"""
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=dilation, groups=groups, bias=False, dilation=dilation)
def conv1x1(in_planes: int, out_planes: int, stride: int = 1) -> nn.Conv2d:
"""1x1 convolution"""
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
class ResidualBlock(nn.Module):
expansion = 4
def __init__(
self,
inplanes: int,
planes: int,
stride: int = 1,
downsample: Optional[nn.Module] = None,
groups: int = 1,
base_width: int = 64,
dilation: int = 1,
norm_layer: Optional[Callable[..., nn.Module]] = None
) -> None:
super(ResidualBlock, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
#width = int(planes * (base_width / 64.)) * groups
planes = int(planes/4)
# Both self.conv2 and self.downsample layers downsample the input when stride != 1
self.conv1 = conv1x1(inplanes, planes)
self.bn1 = norm_layer(planes)
self.conv2 = conv3x3(planes, planes, stride, groups, dilation)
self.bn2 = norm_layer(planes)
self.conv3 = conv1x1(planes, planes * self.expansion)
self.bn3 = norm_layer(planes * self.expansion)
self.relu = nn.ReLU(inplace=True)
#self.downsample = downsample
self.stride = stride
# Added: auto downsample
self.downsample = nn.Sequential(
conv1x1(inplanes, planes * self.expansion, stride),
norm_layer(planes * self.expansion),
)
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class AttentionModule1(nn.Module):
def __init__(self, in_channels, out_channels, p=1, t=2, r=1):
super().__init__()
#"""The hyperparameter p denotes the number of preprocessing Residual
#Units before splitting into trunk branch and mask branch. t denotes
#the number of Residual Units in trunk branch. r denotes the number of
#Residual Units between adjacent pooling layer in the mask branch."""
assert in_channels == out_channels
self.pre = self._make_residual(in_channels, out_channels, p)
self.trunk = self._make_residual(in_channels, out_channels, t)
self.soft_resdown1 = self._make_residual(in_channels, out_channels, r)
self.soft_resdown2 = self._make_residual(in_channels, out_channels, r)
self.soft_resdown3 = self._make_residual(in_channels, out_channels, r)
self.soft_resdown4 = self._make_residual(in_channels, out_channels, r)
self.soft_resup1 = self._make_residual(in_channels, out_channels, r)
self.soft_resup2 = self._make_residual(in_channels, out_channels, r)
self.soft_resup3 = self._make_residual(in_channels, out_channels, r)
self.soft_resup4 = self._make_residual(in_channels, out_channels, r)
self.shortcut_short = ResidualBlock(in_channels, out_channels, 1)
self.shortcut_long = ResidualBlock(in_channels, out_channels, 1)
self.sigmoid = nn.Sequential(
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=1),
nn.Sigmoid()
)
self.last = self._make_residual(in_channels, out_channels, p)
def forward(self, x):
###We make the size of the smallest output map in each mask branch 7*7 to be consistent
#with the smallest trunk output map size.
###Thus 3,2,1 max-pooling layers are used in mask branch with input size 56 * 56, 28 * 28, 14 * 14 respectively.
x = self.pre(x)
input_size = (x.size(2), x.size(3))
x_t = self.trunk(x)
#first downsample out 28
x_s = F.max_pool2d(x, kernel_size=3, stride=2, padding=1)
x_s = self.soft_resdown1(x_s)
#28 shortcut
shape1 = (x_s.size(2), x_s.size(3))
shortcut_long = self.shortcut_long(x_s)
#seccond downsample out 14
x_s = F.max_pool2d(x, kernel_size=3, stride=2, padding=1)
x_s = self.soft_resdown2(x_s)
#14 shortcut
shape2 = (x_s.size(2), x_s.size(3))
shortcut_short = self.soft_resdown3(x_s)
#third downsample out 7
x_s = F.max_pool2d(x, kernel_size=3, stride=2, padding=1)
x_s = self.soft_resdown3(x_s)
#mid
x_s = self.soft_resdown4(x_s)
x_s = self.soft_resup1(x_s)
#first upsample out 14
x_s = self.soft_resup2(x_s)
x_s = F.interpolate(x_s, size=shape2)
x_s += shortcut_short
#second upsample out 28
x_s = self.soft_resup3(x_s)
x_s = F.interpolate(x_s, size=shape1)
x_s += shortcut_long
#thrid upsample out 54
x_s = self.soft_resup4(x_s)
x_s = F.interpolate(x_s, size=input_size)
x_s = self.sigmoid(x_s)
x = (1 + x_s) * x_t
x = self.last(x)
return x
def _make_residual(self, in_channels, out_channels, p):
layers = []
for _ in range(p):
layers.append(ResidualBlock(in_channels, out_channels, 1))
return nn.Sequential(*layers)
class AttentionModule2(nn.Module):
def __init__(self, in_channels, out_channels, p=1, t=2, r=1):
super().__init__()
#"""The hyperparameter p denotes the number of preprocessing Residual
#Units before splitting into trunk branch and mask branch. t denotes
#the number of Residual Units in trunk branch. r denotes the number of
#Residual Units between adjacent pooling layer in the mask branch."""
assert in_channels == out_channels
self.pre = self._make_residual(in_channels, out_channels, p)
self.trunk = self._make_residual(in_channels, out_channels, t)
self.soft_resdown1 = self._make_residual(in_channels, out_channels, r)
self.soft_resdown2 = self._make_residual(in_channels, out_channels, r)
self.soft_resdown3 = self._make_residual(in_channels, out_channels, r)
self.soft_resup1 = self._make_residual(in_channels, out_channels, r)
self.soft_resup2 = self._make_residual(in_channels, out_channels, r)
self.soft_resup3 = self._make_residual(in_channels, out_channels, r)
self.shortcut = ResidualBlock(in_channels, out_channels, 1)
self.sigmoid = nn.Sequential(
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=1),
nn.Sigmoid()
)
self.last = self._make_residual(in_channels, out_channels, p)
def forward(self, x):
x = self.pre(x)
input_size = (x.size(2), x.size(3))
x_t = self.trunk(x)
#first downsample out 14
x_s = F.max_pool2d(x, kernel_size=3, stride=2, padding=1)
x_s = self.soft_resdown1(x_s)
#14 shortcut
shape1 = (x_s.size(2), x_s.size(3))
shortcut = self.shortcut(x_s)
#seccond downsample out 7
x_s = F.max_pool2d(x, kernel_size=3, stride=2, padding=1)
x_s = self.soft_resdown2(x_s)
#mid
x_s = self.soft_resdown3(x_s)
x_s = self.soft_resup1(x_s)
#first upsample out 14
x_s = self.soft_resup2(x_s)
x_s = F.interpolate(x_s, size=shape1)
x_s += shortcut
#second upsample out 28
x_s = self.soft_resup3(x_s)
x_s = F.interpolate(x_s, size=input_size)
x_s = self.sigmoid(x_s)
x = (1 + x_s) * x_t
x = self.last(x)
return x
def _make_residual(self, in_channels, out_channels, p):
layers = []
for _ in range(p):
layers.append(ResidualBlock(in_channels, out_channels, 1))
return nn.Sequential(*layers)
class AttentionModule3(nn.Module):
def __init__(self, in_channels, out_channels, p=1, t=2, r=1):
super().__init__()
assert in_channels == out_channels
self.pre = self._make_residual(in_channels, out_channels, p)
self.trunk = self._make_residual(in_channels, out_channels, t)
self.soft_resdown1 = self._make_residual(in_channels, out_channels, r)
self.soft_resdown2 = self._make_residual(in_channels, out_channels, r)
self.soft_resup1 = self._make_residual(in_channels, out_channels, r)
self.soft_resup2 = self._make_residual(in_channels, out_channels, r)
self.shortcut = ResidualBlock(in_channels, out_channels, 1)
self.sigmoid = nn.Sequential(
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=1),
nn.Sigmoid()
)
self.last = self._make_residual(in_channels, out_channels, p)
def forward(self, x):
x = self.pre(x)
input_size = (x.size(2), x.size(3))
x_t = self.trunk(x)
#first downsample out 14
x_s = F.max_pool2d(x, kernel_size=3, stride=2, padding=1)
x_s = self.soft_resdown1(x_s)
#mid
x_s = self.soft_resdown2(x_s)
x_s = self.soft_resup1(x_s)
#first upsample out 14
x_s = self.soft_resup2(x_s)
x_s = F.interpolate(x_s, size=input_size)
x_s = self.sigmoid(x_s)
x = (1 + x_s) * x_t
x = self.last(x)
return x
def _make_residual(self, in_channels, out_channels, p):
layers = []
for _ in range(p):
layers.append(ResidualBlock(in_channels, out_channels, 1))
return nn.Sequential(*layers)
class Attention(nn.Module):
"""residual attention netowrk
Args:
block_num: attention module number for each stage
"""
def __init__(self, block_num, num_classes=100,pretrained=None):
super().__init__()
self.pre_conv = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
)
self.stage1 = self._make_stage(64, 256, block_num[0], AttentionModule1)
self.stage2 = self._make_stage(256, 512, block_num[1], AttentionModule2)
self.stage3 = self._make_stage(512, 1024, block_num[2], AttentionModule3)
self.stage4 = nn.Sequential(
ResidualBlock(1024, 2048, 2),
ResidualBlock(2048, 2048, 1),
ResidualBlock(2048, 2048, 1)
)
self.avg = nn.AdaptiveAvgPool2d(1)
self.classifier = nn.Linear(2048, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def forward(self, x):
x = self.pre_conv(x)
x = self.stage1(x)
x = self.stage2(x)
x = self.stage3(x)
x = self.stage4(x)
x = self.avg(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
def _make_stage(self, in_channels, out_channels, num, block):
layers = []
layers.append(ResidualBlock(in_channels, out_channels, 2))
for _ in range(num):
layers.append(block(out_channels, out_channels))
return nn.Sequential(*layers)
def attention56(**kwargs):
return Attention([1, 1, 1], **kwargs)
def attention92(**kwargs):
return Attention([1, 2, 3], **kwargs)