-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathattentionnet3D.py
353 lines (267 loc) · 11.7 KB
/
attentionnet3D.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
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from collections import OrderedDict
__all__ = [
'Attention3D', 'attention3d56', 'attention3d92'
]
''' Residual Bottleneck from Tencent/MedicalNet'''
class ResidualBlock(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(ResidualBlock, self).__init__()
# Added for consistency
planes = int(planes/4)
self.conv1 = nn.Conv3d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm3d(planes)
self.conv2 = nn.Conv3d(
planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm3d(planes)
self.conv3 = nn.Conv3d(planes, planes * 4, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm3d(planes * 4)
self.relu = nn.ReLU(inplace=True)
#self.downsample = downsample
# Added: auto downsample
self.downsample = nn.Sequential(nn.Conv3d(inplanes, planes *4 , kernel_size=1, stride=stride, bias = False), nn.BatchNorm3d(planes*4))
self.stride = stride
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.BatchNorm3d(out_channels),
nn.ReLU(inplace=True),
nn.Conv3d(out_channels, out_channels, kernel_size=1),
nn.BatchNorm3d(out_channels),
nn.ReLU(inplace=True),
nn.Conv3d(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.size(4))
x_t = self.trunk(x)
#first downsample out 28
x_s = F.max_pool3d(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), x_s.size(4))
shortcut_long = self.shortcut_long(x_s)
#seccond downsample out 14
x_s = F.max_pool3d(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), x_s.size(4))
shortcut_short = self.soft_resdown3(x_s)
#third downsample out 7
x_s = F.max_pool3d(x, kernel_size=3, stride=2, padding=1)
x_s = self.soft_resdown3(x_s)
#mid ??? NOT IN THE PAPER
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.BatchNorm3d(out_channels),
nn.ReLU(inplace=True),
nn.Conv3d(out_channels, out_channels, kernel_size=1),
nn.BatchNorm3d(out_channels),
nn.ReLU(inplace=True),
nn.Conv3d(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.size(4))
x_t = self.trunk(x)
#first downsample out 14
x_s = F.max_pool3d(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), x_s.size(4))
shortcut = self.shortcut(x_s)
#seccond downsample out 7
x_s = F.max_pool3d(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.BatchNorm3d(out_channels),
nn.ReLU(inplace=True),
nn.Conv3d(out_channels, out_channels, kernel_size=1),
nn.BatchNorm3d(out_channels),
nn.ReLU(inplace=True),
nn.Conv3d(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.size(4))
x_t = self.trunk(x)
#first downsample out 14
x_s = F.max_pool3d(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 Attention3D(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.Conv3d(3, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm3d(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, stride=2),
ResidualBlock(2048, 2048, stride=1),
ResidualBlock(2048, 2048, stride=1)
)
self.avg = nn.AdaptiveAvgPool3d(1)
self.classifier = nn.Linear(2048, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv3d):
m.weight = nn.init.kaiming_normal(m.weight, mode='fan_out')
elif isinstance(m, nn.BatchNorm3d):
m.weight.data.fill_(1)
m.bias.data.zero_()
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, stride=2))
for _ in range(num):
layers.append(block(out_channels, out_channels))
return nn.Sequential(*layers)
def attention3d56(**kwargs):
return Attention3D([1, 1, 1], **kwargs)
def attention3d92(**kwargs):
return Attention3D([1, 2, 3], **kwargs)