-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunetr_2d.py
163 lines (126 loc) · 4.41 KB
/
unetr_2d.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
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
from math import log2
import tensorflow as tf
import tensorflow.keras.layers as L
from tensorflow.keras.models import Model
def mlp(x, cf):
x = L.Dense(cf["mlp_dim"], activation="gelu")(x)
x = L.Dropout(cf["dropout_rate"])(x)
x = L.Dense(cf["hidden_dim"])(x)
x = L.Dropout(cf["dropout_rate"])(x)
return x
def transformer_encoder(x, cf):
skip_1 = x
x = L.LayerNormalization()(x)
x = L.MultiHeadAttention(
num_heads=cf["num_heads"], key_dim=cf["hidden_dim"]
)(x, x)
x = L.Add()([x, skip_1])
skip_2 = x
x = L.LayerNormalization()(x)
x = mlp(x, cf)
x = L.Add()([x, skip_2])
return x
def conv_block(x, num_filters, kernel_size=3):
x = L.Conv2D(num_filters, kernel_size=kernel_size, padding="same")(x)
x = L.BatchNormalization()(x)
x = L.ReLU()(x)
return x
def deconv_block(x, num_filters, strides=2):
x = L.Conv2DTranspose(num_filters, kernel_size=2, padding="same", strides=strides)(x)
return x
def build_unetr_2d(cf):
""" Inputs """
input_shape = (cf["num_patches"], cf["patch_size"]*cf["patch_size"]*cf["num_channels"])
inputs = L.Input(input_shape) ## (None, 256, 3072)
""" Patch + Position Embeddings """
patch_embed = L.Dense(cf["hidden_dim"])(inputs) ## (None, 256, 768)
positions = tf.range(start=0, limit=cf["num_patches"], delta=1) ## (256,)
pos_embed = L.Embedding(input_dim=cf["num_patches"], output_dim=cf["hidden_dim"])(positions) ## (256, 768)
x = patch_embed + pos_embed ## (None, 256, 768)
""" Transformer Encoder """
skip_connection_index = [3, 6, 9, 12]
skip_connections = []
for i in range(1, cf["num_layers"]+1, 1):
x = transformer_encoder(x, cf)
if i in skip_connection_index:
skip_connections.append(x)
""" CNN Decoder """
z3, z6, z9, z12 = skip_connections
## Reshaping
z0 = L.Reshape((cf["image_size"], cf["image_size"], cf["num_channels"]))(inputs)
shape = (
cf["image_size"]//cf["patch_size"],
cf["image_size"]//cf["patch_size"],
cf["hidden_dim"]
)
z3 = L.Reshape(shape)(z3)
z6 = L.Reshape(shape)(z6)
z9 = L.Reshape(shape)(z9)
z12 = L.Reshape(shape)(z12)
## Additional layers for managing different patch sizes
total_upscale_factor = int(log2(cf["patch_size"]))
upscale = total_upscale_factor - 4
if upscale >= 2: ## Patch size 16 or greater
z3 = deconv_block(z3, z3.shape[-1], strides=2**upscale)
z6 = deconv_block(z6, z6.shape[-1], strides=2**upscale)
z9 = deconv_block(z9, z9.shape[-1], strides=2**upscale)
z12 = deconv_block(z12, z12.shape[-1], strides=2**upscale)
# print(z3.shape, z6.shape, z9.shape, z12.shape)
if upscale < 0: ## Patch size less than 16
p = 2**abs(upscale)
z3 = L.MaxPool2D((p, p))(z3)
z6 = L.MaxPool2D((p, p))(z6)
z9 = L.MaxPool2D((p, p))(z9)
z12 = L.MaxPool2D((p, p))(z12)
## Decoder 1
x = deconv_block(z12, 128)
s = deconv_block(z9, 128)
s = conv_block(s, 128)
x = L.Concatenate()([x, s])
x = conv_block(x, 128)
x = conv_block(x, 128)
## Decoder 2
x = deconv_block(x, 64)
s = deconv_block(z6, 64)
s = conv_block(s, 64)
s = deconv_block(s, 64)
s = conv_block(s, 64)
x = L.Concatenate()([x, s])
x = conv_block(x, 64)
x = conv_block(x, 64)
## Decoder 3
x = deconv_block(x, 32)
s = deconv_block(z3, 32)
s = conv_block(s, 32)
s = deconv_block(s, 32)
s = conv_block(s, 32)
s = deconv_block(s, 32)
s = conv_block(s, 32)
x = L.Concatenate()([x, s])
x = conv_block(x, 32)
x = conv_block(x, 32)
## Decoder 4
x = deconv_block(x, 16)
s = conv_block(z0, 16)
s = conv_block(s, 16)
x = L.Concatenate()([x, s])
x = conv_block(x, 16)
x = conv_block(x, 16)
""" Output """
outputs = L.Conv2D(1, kernel_size=1, padding="same", activation="sigmoid")(x)
return Model(inputs, outputs, name="UNETR_2D")
if __name__ == "__main__":
config = {}
config["image_size"] = 512
config["num_layers"] = 12
config["hidden_dim"] = 64
config["mlp_dim"] = 128
config["num_heads"] = 6
config["dropout_rate"] = 0.1
config["patch_size"] = 1
config["num_patches"] = (config["image_size"]**2)//(config["patch_size"]**2)
config["num_channels"] = 3
model = build_unetr_2d(config)
model.summary()