-
Notifications
You must be signed in to change notification settings - Fork 0
/
attention_models_3d.py
444 lines (364 loc) · 14.1 KB
/
attention_models_3d.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.fft import fftn, ifftn
# import torch_dct as dct
class ConvBlock(nn.Module):
def __init__(self,
in_channels,
out_channels,
kernel_size=3,
padding=1,
dilation=1):
super().__init__()
block = []
block.append(
nn.Conv3d(in_channels,
out_channels,
kernel_size=kernel_size,
padding=padding,
bias=True,
dilation=dilation))
block.append(nn.PReLU())
self.block = nn.Sequential(*block)
def forward(self, x):
out = self.block(x)
return out
class SAB_astrous(nn.Module):
def __init__(self, in_channels, reduction=4):
super().__init__()
block = []
block.append(
ConvBlock(in_channels=in_channels, out_channels=in_channels))
out_stage1 = in_channels // reduction
block.append(
ConvBlock(in_channels=in_channels,
out_channels=out_stage1,
kernel_size=1,
padding=0))
block.append(
ConvBlock(in_channels=out_stage1,
out_channels=out_stage1,
kernel_size=3,
padding=2,
dilation=2))
out_stage2 = out_stage1 // reduction
block.append(
ConvBlock(in_channels=out_stage1,
out_channels=out_stage2,
kernel_size=1,
padding=0))
block.append(
ConvBlock(in_channels=out_stage2,
out_channels=out_stage2,
kernel_size=3,
padding=4,
dilation=4))
block.append(
nn.Conv3d(out_stage2, 1, kernel_size=1, padding=0, bias=True))
block.append(nn.Sigmoid())
self.block = nn.Sequential(*block)
def forward(self, x):
out = self.block(x)
return out
class ChannelAttention(nn.Module):
"""
Channel attention part
"""
def __init__(self, channels, reduction=16):
super().__init__()
block = []
block.append(nn.AdaptiveAvgPool3d((1, 1, 1)))
block.append(
ConvBlock(in_channels=channels,
out_channels=channels // reduction,
kernel_size=1,
padding=0))
block.append(
nn.Conv3d(channels // reduction,
channels,
kernel_size=1,
padding=0,
bias=True))
block.append(nn.Sigmoid())
self.block = nn.Sequential(*block)
def forward(self, x):
out = self.block(x)
return out
class SCAB(nn.Module):
"""
Dual attention block
"""
def __init__(self, org_channels, out_channels):
super().__init__()
pre_x = []
pre_x.append(
ConvBlock(in_channels=org_channels, out_channels=out_channels))
pre_x.append(
nn.Conv3d(out_channels, out_channels, kernel_size=3, padding=1))
self.pre_x = nn.Sequential(*pre_x)
self.CAB = ChannelAttention(channels=out_channels)
self.SAB = SAB_astrous(in_channels=out_channels)
self.last = nn.Conv3d(in_channels=2 * out_channels,
out_channels=out_channels,
kernel_size=3,
padding=1,
bias=True)
def forward(self, x):
pre_x = self.pre_x(x)
channel = self.CAB(pre_x)
spatial = self.SAB(pre_x)
out_s = pre_x * spatial.expand_as(pre_x)
out_c = pre_x * channel.expand_as(pre_x)
out_combine = torch.cat([out_s, out_c], dim=1)
out = self.last(out_combine)
out = x + out
return out
class ResidualBlock(nn.Module):
def __init__(self, in_size, out_size, shortcut=None):
super().__init__()
self.basic = nn.Sequential(
nn.Conv3d(in_size, out_size, kernel_size=3, padding=1, bias=True),
nn.PReLU())
self.shortcut = shortcut
def forward(self, x):
out = self.basic(x)
residual = x if self.shortcut is None else self.shortcut(x)
out += residual
return out
class UNetConvBlock(nn.Module):
def __init__(self, in_size, out_size, num):
super().__init__()
block = []
block.append(
nn.Conv3d(in_size, out_size, kernel_size=3, padding=1, bias=True))
block.append(nn.PReLU())
for _ in range(max(num - 1, 1)):
block.append(ResidualBlock(out_size, out_size))
self.block = nn.Sequential(*block)
def forward(self, x):
out = self.block(x)
return out
class UNetUpBlock(nn.Module):
def __init__(self, in_size, out_size, num):
super().__init__()
self.up = nn.ConvTranspose3d(in_size,
out_size,
kernel_size=3,
stride=2,
padding=1,
output_padding=1,
bias=True)
self.conv_block = UNetConvBlock(in_size, out_size, num)
def forward(self, x, bridge):
up = self.up(x)
out = torch.cat([up, bridge], 1)
out = self.conv_block(out)
return out
class UNet(nn.Module):
def __init__(self,
in_channels=5,
out_channels=4,
depth=4,
feature_dims=64):
super().__init__()
self.depth = depth
prev_channels = in_channels
self.down_path = nn.ModuleList()
for i in range(depth):
self.down_path.append(
UNetConvBlock(prev_channels, (2**i) * feature_dims, depth - i))
if i != depth - 1:
self.down_path.append(
SCAB((2**i) * feature_dims, (2**i) * feature_dims))
prev_channels = (2**i) * feature_dims
self.up_path = nn.ModuleList()
for i in reversed(range(depth - 1)):
self.up_path.append(
UNetUpBlock(prev_channels, (2**i) * feature_dims, depth - i))
prev_channels = (2**i) * feature_dims
self.last = nn.Conv3d(prev_channels,
out_channels,
kernel_size=3,
stride=1,
padding=1,
bias=True)
def forward(self, x):
blocks = []
for i, down in enumerate(self.down_path):
x = down(x)
if (i != len(self.down_path) - 1) and (i % 2 == 1):
blocks.append(x)
x = F.avg_pool3d(x, 2)
for i, up in enumerate(self.up_path):
x = up(x, blocks[-i - 1])
return self.last(x)
class SigmaNet(nn.Module):
def __init__(self, in_channels, out_channels, depth=3, num_filter=64):
super().__init__()
block = []
block.append(
nn.Conv3d(in_channels,
num_filter,
kernel_size=3,
padding=1,
bias=True))
block.append(nn.PReLU())
for _ in range(depth):
block.append(
nn.Conv3d(num_filter,
num_filter,
kernel_size=3,
padding=1,
bias=True))
block.append(nn.PReLU())
block.append(
nn.Conv3d(num_filter,
out_channels,
kernel_size=3,
padding=1,
bias=True))
self.block = nn.Sequential(*block)
def forward(self, x):
out = self.block(x)
return out
class FAN3D(nn.Module):
"""
Frequency Attention Network in 3D
Implementation of UNET using attention blocks with 3D input
and working in the frequency domain via the Fourier transform.
Works for PET, PEt+CT or PET+MR or PET+CT+MR input.
Activation functions throughout are PReLU and no BN, no dropout.
This is a Black & White implementation of https://github.com/momo1689/FAN
"""
def __init__(self,
in_channels,
depth_S=5,
depth_U=4,
feature_dims=64,
**kwargs):
super().__init__()
self.sigma_net = SigmaNet(in_channels=1,
out_channels=1,
depth=depth_S,
num_filter=feature_dims)
self.UNet = UNet(in_channels=in_channels + 3,
out_channels=2,
depth=depth_U,
feature_dims=feature_dims)
def forward(self, img):
# get noise map from SigmaNet. Only pass PET image
if self.in_channels == 1:
noise_map = self.sigma_net(img)
else:
noise_map = self.sigma_net(img[:, :1, ...])
# Fourier decomposition on gray scale image. Split REAL and IMAG part
fourier_decomp = fftn(img)
f_real = fourier_decomp.real
f_imag = fourier_decomp.imag
# concat (Fourier decomposition + image + noise_map) as input to UNET
# (bs, 4, dim1, dim2)
net_input = torch.cat((f_real, f_imag, img, noise_map), dim=1)
# run through the UNet
net_out = self.UNet(net_input)
# extract REAL and IMAG part
out_fftr = net_out[:, 0].unsqueeze(dim=1)
out_ffti = net_out[:, 1].unsqueeze(dim=1)
# compute the inverse FFT to generate output image
out_img = torch.abs(ifftn(out_fftr + 1j * out_ffti))
# return self.out(out_final)
if torch.any(torch.isnan(out_img)):
raise ValueError(
"Network output contains nan values. Something's wrong.")
return out_img
class AttentionUNet(nn.Module):
"""
Attention UNet in 3D
Implementation of UNET using attention blocks with 3D input
can be PET only, or PET+CT or PET+MR
Activation functions throughout are PReLU and no BN, no dropout.
This is a re-work of https://github.com/momo1689/FAN
"""
def __init__(self,
in_channels,
depth_S=5,
depth_U=4,
feature_dims=64,
**kwargs):
super().__init__()
self.in_channels = in_channels
self.sigma_net = SigmaNet(in_channels=1,
out_channels=1,
depth=depth_S,
num_filter=feature_dims)
self.UNet = UNet(in_channels=in_channels + 1,
out_channels=1,
depth=depth_U,
feature_dims=feature_dims)
def forward(self, img):
# get noise map from SigmaNet. Only pass PET image
if self.in_channels == 1:
noise_map = self.sigma_net(img)
else:
noise_map = self.sigma_net(img[:, :1, ...])
# concat (Fourier decomposition + image + noise_map) as input to UNET
# (bs, 4, dim1, dim2)
net_input = torch.cat((img, noise_map), dim=1)
# run through the UNet
out_img = self.UNet(net_input)
if torch.any(torch.isnan(out_img)):
raise ValueError(
"Network output contains nan values. Something's wrong.")
return out_img
class FAN3D(nn.Module):
"""
Frequency attention Network in 3D with Fourier Transform.
Same as FAN2D (but for 3D!) in attention_models_2d.py
Implementation of UNET using attention blocks with 3D input
can be PET only, or PET+CT or PET+MR
Activation functions throughout are PReLU and no BN, no dropout.
This is a re-work of https://github.com/momo1689/FAN
where I changed wavelet transform to Fourier transform.
WARNING: I have had mixed results with this network.
Mostly I get exploding gradients because of no BN.
So need careful choice of learning rate scheduler.
"""
def __init__(self,
in_channels,
depth_S=5,
depth_U=4,
feature_dims=64,
**kwargs):
super().__init__()
self.sigma_net = SigmaNet(in_channels=1,
out_channels=1,
depth=depth_S,
num_filter=feature_dims)
self.UNet = UNet(in_channels=in_channels + 3,
out_channels=2,
depth=depth_U,
feature_dims=feature_dims)
def forward(self, img):
# get noise map from SigmaNet. Only pass PET image
if self.in_channels == 1:
noise_map = self.sigma_net(img)
else:
noise_map = self.sigma_net(img[:, :1, ...])
# Fourier decomposition on gray scale image. Split REAL and IMAG part
fourier_decomp = fftn(img)
f_real = fourier_decomp.real
f_imag = fourier_decomp.imag
# concat (Fourier decomposition + image + noise_map) as input to UNET
# (bs, 4, dim1, dim2)
net_input = torch.cat((f_real, f_imag, img, noise_map), dim=1)
# run through the UNet
net_out = self.UNet(net_input)
# extract REAL and IMAG part
out_fftr = net_out[:, 0].unsqueeze(dim=1)
out_ffti = net_out[:, 1].unsqueeze(dim=1)
# compute the inverse FFT to generate output image
out_img = torch.abs(ifftn(out_fftr + 1j * out_ffti))
if torch.any(torch.isnan(out_img)):
raise ValueError(
"Network output contains nan values. Something's wrong.")
return out_img