forked from tamasino52/UNETR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unetr.py
294 lines (237 loc) · 9.92 KB
/
unetr.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
import copy
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
class SingleDeconv3DBlock(nn.Module):
def __init__(self, in_planes, out_planes):
super().__init__()
self.block = nn.ConvTranspose3d(in_planes, out_planes, kernel_size=2, stride=2, padding=0, output_padding=0)
def forward(self, x):
return self.block(x)
class SingleConv3DBlock(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size):
super().__init__()
self.block = nn.Conv3d(in_planes, out_planes, kernel_size=kernel_size, stride=1,
padding=((kernel_size - 1) // 2))
def forward(self, x):
return self.block(x)
class Conv3DBlock(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size=3):
super().__init__()
self.block = nn.Sequential(
SingleConv3DBlock(in_planes, out_planes, kernel_size),
nn.BatchNorm3d(out_planes),
nn.ReLU(True)
)
def forward(self, x):
return self.block(x)
class Deconv3DBlock(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size=3):
super().__init__()
self.block = nn.Sequential(
SingleDeconv3DBlock(in_planes, out_planes),
SingleConv3DBlock(out_planes, out_planes, kernel_size),
nn.BatchNorm3d(out_planes),
nn.ReLU(True)
)
def forward(self, x):
return self.block(x)
class SelfAttention(nn.Module):
def __init__(self, num_heads, embed_dim, dropout):
super().__init__()
self.num_attention_heads = num_heads
self.attention_head_size = int(embed_dim / num_heads)
self.all_head_size = self.num_attention_heads * self.attention_head_size
self.query = nn.Linear(embed_dim, self.all_head_size)
self.key = nn.Linear(embed_dim, self.all_head_size)
self.value = nn.Linear(embed_dim, self.all_head_size)
self.out = nn.Linear(embed_dim, embed_dim)
self.attn_dropout = nn.Dropout(dropout)
self.proj_dropout = nn.Dropout(dropout)
self.softmax = nn.Softmax(dim=-1)
self.vis = False
def transpose_for_scores(self, x):
new_x_shape = x.size()[:-1] + (self.num_attention_heads, self.attention_head_size)
x = x.view(*new_x_shape)
return x.permute(0, 2, 1, 3)
def forward(self, hidden_states):
mixed_query_layer = self.query(hidden_states)
mixed_key_layer = self.key(hidden_states)
mixed_value_layer = self.value(hidden_states)
query_layer = self.transpose_for_scores(mixed_query_layer)
key_layer = self.transpose_for_scores(mixed_key_layer)
value_layer = self.transpose_for_scores(mixed_value_layer)
attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2))
attention_scores = attention_scores / math.sqrt(self.attention_head_size)
attention_probs = self.softmax(attention_scores)
weights = attention_probs if self.vis else None
attention_probs = self.attn_dropout(attention_probs)
context_layer = torch.matmul(attention_probs, value_layer)
context_layer = context_layer.permute(0, 2, 1, 3).contiguous()
new_context_layer_shape = context_layer.size()[:-2] + (self.all_head_size,)
context_layer = context_layer.view(*new_context_layer_shape)
attention_output = self.out(context_layer)
attention_output = self.proj_dropout(attention_output)
return attention_output, weights
class Mlp(nn.Module):
def __init__(self, in_features, act_layer=nn.GELU, drop=0.):
super().__init__()
self.fc1 = nn.Linear(in_features, in_features)
self.act = act_layer()
self.drop = nn.Dropout(drop)
def forward(self, x):
x = self.fc1()
x = self.act(x)
x = self.drop(x)
return x
class PositionwiseFeedForward(nn.Module):
def __init__(self, d_model=786, d_ff=2048, dropout=0.1):
super().__init__()
# Torch linears have a `b` by default.
self.w_1 = nn.Linear(d_model, d_ff)
self.w_2 = nn.Linear(d_ff, d_model)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
return self.w_2(self.dropout(F.relu(self.w_1(x))))
class Embeddings(nn.Module):
def __init__(self, input_dim, embed_dim, cube_size, patch_size, dropout):
super().__init__()
self.n_patches = int((cube_size[0] * cube_size[1] * cube_size[2]) / (patch_size * patch_size * patch_size))
self.patch_size = patch_size
self.embed_dim = embed_dim
self.patch_embeddings = nn.Conv3d(in_channels=input_dim, out_channels=embed_dim,
kernel_size=patch_size, stride=patch_size)
self.position_embeddings = nn.Parameter(torch.zeros(1, self.n_patches, embed_dim))
self.dropout = nn.Dropout(dropout)
def forward(self, x):
x = self.patch_embeddings(x)
x = x.flatten(2)
x = x.transpose(-1, -2)
embeddings = x + self.position_embeddings
embeddings = self.dropout(embeddings)
return embeddings
class TransformerBlock(nn.Module):
def __init__(self, embed_dim, num_heads, dropout, cube_size, patch_size):
super().__init__()
self.attention_norm = nn.LayerNorm(embed_dim, eps=1e-6)
self.mlp_norm = nn.LayerNorm(embed_dim, eps=1e-6)
self.mlp_dim = int((cube_size[0] * cube_size[1] * cube_size[2]) / (patch_size * patch_size * patch_size))
self.mlp = PositionwiseFeedForward(embed_dim, 2048)
self.attn = SelfAttention(num_heads, embed_dim, dropout)
def forward(self, x):
h = x
x = self.attention_norm(x)
x, weights = self.attn(x)
x = x + h
h = x
x = self.mlp_norm(x)
x = self.mlp(x)
x = x + h
return x, weights
class Transformer(nn.Module):
def __init__(self, input_dim, embed_dim, cube_size, patch_size, num_heads, num_layers, dropout, extract_layers):
super().__init__()
self.embeddings = Embeddings(input_dim, embed_dim, cube_size, patch_size, dropout)
self.layer = nn.ModuleList()
self.encoder_norm = nn.LayerNorm(embed_dim, eps=1e-6)
self.extract_layers = extract_layers
for _ in range(num_layers):
layer = TransformerBlock(embed_dim, num_heads, dropout, cube_size, patch_size)
self.layer.append(copy.deepcopy(layer))
def forward(self, x):
extract_layers = []
hidden_states = self.embeddings(x)
for depth, layer_block in enumerate(self.layer):
hidden_states, _ = layer_block(hidden_states)
if depth + 1 in self.extract_layers:
extract_layers.append(hidden_states)
return extract_layers
class UNETR(nn.Module):
def __init__(self, img_shape=(128, 128, 128), input_dim=4, output_dim=3, embed_dim=768, patch_size=16, num_heads=12, dropout=0.1):
super().__init__()
self.input_dim = input_dim
self.output_dim = output_dim
self.embed_dim = embed_dim
self.img_shape = img_shape
self.patch_size = patch_size
self.num_heads = num_heads
self.dropout = dropout
self.num_layers = 12
self.ext_layers = [3, 6, 9, 12]
self.patch_dim = [int(x / patch_size) for x in img_shape]
# Transformer Encoder
self.transformer = \
Transformer(
input_dim,
embed_dim,
img_shape,
patch_size,
num_heads,
self.num_layers,
dropout,
self.ext_layers
)
# U-Net Decoder
self.decoder0 = \
nn.Sequential(
Conv3DBlock(input_dim, 32, 3),
Conv3DBlock(32, 64, 3)
)
self.decoder3 = \
nn.Sequential(
Deconv3DBlock(embed_dim, 512),
Deconv3DBlock(512, 256),
Deconv3DBlock(256, 128)
)
self.decoder6 = \
nn.Sequential(
Deconv3DBlock(embed_dim, 512),
Deconv3DBlock(512, 256),
)
self.decoder9 = \
Deconv3DBlock(embed_dim, 512)
self.decoder12_upsampler = \
SingleDeconv3DBlock(embed_dim, 512)
self.decoder9_upsampler = \
nn.Sequential(
Conv3DBlock(1024, 512),
Conv3DBlock(512, 512),
Conv3DBlock(512, 512),
SingleDeconv3DBlock(512, 256)
)
self.decoder6_upsampler = \
nn.Sequential(
Conv3DBlock(512, 256),
Conv3DBlock(256, 256),
SingleDeconv3DBlock(256, 128)
)
self.decoder3_upsampler = \
nn.Sequential(
Conv3DBlock(256, 128),
Conv3DBlock(128, 128),
SingleDeconv3DBlock(128, 64)
)
self.decoder0_header = \
nn.Sequential(
Conv3DBlock(128, 64),
Conv3DBlock(64, 64),
SingleConv3DBlock(64, output_dim, 1)
)
def forward(self, x):
z = self.transformer(x)
z0, z3, z6, z9, z12 = x, *z
z3 = z3.transpose(-1, -2).view(-1, self.embed_dim, *self.patch_dim)
z6 = z6.transpose(-1, -2).view(-1, self.embed_dim, *self.patch_dim)
z9 = z9.transpose(-1, -2).view(-1, self.embed_dim, *self.patch_dim)
z12 = z12.transpose(-1, -2).view(-1, self.embed_dim, *self.patch_dim)
z12 = self.decoder12_upsampler(z12)
z9 = self.decoder9(z9)
z9 = self.decoder9_upsampler(torch.cat([z9, z12], dim=1))
z6 = self.decoder6(z6)
z6 = self.decoder6_upsampler(torch.cat([z6, z9], dim=1))
z3 = self.decoder3(z3)
z3 = self.decoder3_upsampler(torch.cat([z3, z6], dim=1))
z0 = self.decoder0(z0)
output = self.decoder0_header(torch.cat([z0, z3], dim=1))
return output