-
Notifications
You must be signed in to change notification settings - Fork 0
/
unetr.py
300 lines (244 loc) · 10.5 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
295
296
297
298
299
300
import torch
import torch.nn as nn
from torch.nn import functional as F
import copy
import math
class Embeddings(nn.Module):
def __init__(self,
input_dim: int = 3,
embed_dim: int = 768,
image_size: tuple = (224, 224),
patch_size: int = 16,
dropout: float = 0.2
):
super().__init__()
self.n_patches = int((image_size[0] * image_size[1]) / (patch_size * patch_size))
self.patch_size = patch_size
self.embed_dim = embed_dim
self.patch_embeddings = nn.Conv2d(in_channels=input_dim,
out_channels=embed_dim,
kernel_size=patch_size,
stride=patch_size)
self.position_embeddings = nn.Parameter(torch.randn(1, self.n_patches, embed_dim))
self.dropout = nn.Dropout(dropout)
def forward(self, x):
image_resolution = x.shape[-1]
assert image_resolution % self.patch_size == 0, f"Input image size must be divisible by patch size, image shape: {image_resolution}, patch size: {self.patch_size}"
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 SelfAttention(nn.Module):
def __init__(self, num_heads: int, embed_dim: int, dropout: float):
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: int, act_layer: nn.Module = nn.GELU, drop: float = 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)
x = self.act(x)
x = self.drop(x)
return x
class PositionwiseFeedForward(nn.Module):
def __init__(self, d_model: int = 786, d_ff: int = 2048, dropout: float = 0.1):
super().__init__()
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 TransformerBlock(nn.Module):
def __init__(self, embed_dim: int, num_heads: int, dropout: float, image_size: tuple, patch_size: int):
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((image_size[0] * image_size[1]) / (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: int, embed_dim: int, image_size: tuple, patch_size: int, num_heads: int, num_layers: int, dropout: float, extract_layers: list):
super().__init__()
self.embeddings = Embeddings(input_dim, embed_dim, image_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, image_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 SingleDeconv2DBlock(nn.Module):
def __init__(self, in_planes: int, out_planes: int):
super().__init__()
self.block = nn.ConvTranspose2d(in_planes, out_planes, kernel_size=2, stride=2, padding=0, output_padding=0)
def forward(self, x):
return self.block(x)
class SingleConv2DBlock(nn.Module):
def __init__(self, in_planes: int, out_planes: int, kernel_size: int):
super().__init__()
self.block = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=1,
padding=((kernel_size - 1) // 2))
def forward(self, x):
return self.block(x)
class Conv2DBlock(nn.Module):
def __init__(self, in_planes: int, out_planes: int, kernel_size: int = 3):
super().__init__()
self.block = nn.Sequential(
SingleConv2DBlock(in_planes, out_planes, kernel_size),
nn.BatchNorm2d(out_planes),
nn.ReLU(True)
)
def forward(self, x):
return self.block(x)
class Deconv2DBlock(nn.Module):
def __init__(self, in_planes: int, out_planes: int, kernel_size: int = 3):
super().__init__()
self.block = nn.Sequential(
SingleDeconv2DBlock(in_planes, out_planes),
SingleConv2DBlock(out_planes, out_planes, kernel_size),
nn.BatchNorm2d(out_planes),
nn.ReLU(True)
)
def forward(self, x):
return self.block(x)
class UNETR(nn.Module):
def __init__(self, img_shape: tuple = (224, 224), input_dim: int = 3, output_dim: int = 1, embed_dim: int = 768, patch_size: int = 16, num_heads: int = 12, dropout: float = 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(y / patch_size) for y in img_shape]
self.transformer = \
Transformer(
input_dim,
embed_dim,
img_shape,
patch_size,
num_heads,
self.num_layers,
dropout,
self.ext_layers
)
self.decoder0 = \
nn.Sequential(
Conv2DBlock(input_dim, 32, 3),
Conv2DBlock(32, 64, 3)
)
self.decoder3 = \
nn.Sequential(
Deconv2DBlock(embed_dim, 512),
Deconv2DBlock(512, 256),
Deconv2DBlock(256, 128)
)
self.decoder6 = \
nn.Sequential(
Deconv2DBlock(embed_dim, 512),
Deconv2DBlock(512, 256),
)
self.decoder9 = \
Deconv2DBlock(embed_dim, 512)
self.decoder12_upsampler = \
SingleDeconv2DBlock(embed_dim, 512)
self.decoder9_upsampler = \
nn.Sequential(
Conv2DBlock(1024, 512),
Conv2DBlock(512, 512),
Conv2DBlock(512, 512),
SingleDeconv2DBlock(512, 256)
)
self.decoder6_upsampler = \
nn.Sequential(
Conv2DBlock(512, 256),
Conv2DBlock(256, 256),
SingleDeconv2DBlock(256, 128)
)
self.decoder3_upsampler = \
nn.Sequential(
Conv2DBlock(256, 128),
Conv2DBlock(128, 128),
SingleDeconv2DBlock(128, 64)
)
self.decoder0_header = \
nn.Sequential(
Conv2DBlock(128, 64),
Conv2DBlock(64, 64),
SingleConv2DBlock(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