-
Notifications
You must be signed in to change notification settings - Fork 0
/
unet_models.py
272 lines (207 loc) · 10.7 KB
/
unet_models.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
from tensorflow import keras
def stride_conv(X, channel, pool_size=2, activation='relu', name='X'):
'''
stride convolutional layer --> batch normalization --> Activation
*Proposed to replace max- and average-pooling layers
Input
----------
X: input tensor
channel: number of convolution filters
pool_size: size of stride
activation: 'relu' for ReLU or 'leaky' for Leaky ReLU
name: name of created keras layers
Output
----------
X: output tensor
'''
# linear convolution with strides
X = keras.layers.Conv2D(channel, pool_size, strides=(pool_size, pool_size), padding='valid',
use_bias=False, kernel_initializer='he_normal', name=name+'_stride_conv')(X)
# batch normalization
X = keras.layers.BatchNormalization(axis=3, name=name+'_stride_conv_bn')(X)
# activation
if activation == 'relu':
X = keras.layers.ReLU(name=name+'_stride_conv_relu')(X)
elif activation == 'leaky':
X = keras.layers.LeakyReLU(alpha=0.3, name=name+'_stride_conv_leaky')(X)
elif activation == 'prelu':
X = keras.layers.PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=[1, 2], name=name+'_stride_conv_prelu')(X)
return X
def CONV_stack(X, channel, kernel_size=3, stack_num=2, activation='relu', name='conv_stac'):
'''
(Convolutional layer --> batch normalization --> Activation)*stack_num
Input
----------
X: input tensor
channel: number of convolution filters
kernel_size: size of 2-d convolution kernels
stack_num: number of stacked convolutional layers
activation: 'relu' for ReLU or 'leaky' for Leaky ReLU
name: name of created keras layers
Output
----------
X: output tensor
'''
# stacking Convolutional layers
for i in range(stack_num):
# linear convolution
X = keras.layers.Conv2D(channel, kernel_size, padding='same', use_bias=False,
kernel_initializer='he_normal', name=name+'_stack{}_conv'.format(i))(X)
# batch normalization
X = keras.layers.BatchNormalization(axis=3, name=name+'_stack{}_bn'.format(i))(X)
# activation
if activation == 'relu':
X = keras.layers.ReLU(name=name+'_stack{}_relu'.format(i))(X)
elif activation == 'leaky':
X = keras.layers.LeakyReLU(alpha=0.3, name=name+'_stack{}_leaky'.format(i))(X)
elif activation == 'prelu':
X = keras.layers.PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=[1, 2], name=name+'_stack{}_prelu'.format(i))(X)
return X
def UNET_left(X, channel, kernel_size=3, pool_size=2, pool=True, activation='relu', name='left0'):
'''
Encoder block of UNet
Input
----------
X: input tensor
channel: number of convolution filters
kernel_size: size of 2-d convolution kernels
pool_size: size of stride
pool: True for maxpooling, False for strided convolutional layers
activation: 'relu' for ReLU or 'leaky' for Leaky ReLU
name: name of created keras layers
Output
----------
X: output tensor
'''
# maxpooling layer vs strided convolutional layers
if pool:
X = keras.layers.MaxPooling2D(pool_size=(pool_size, pool_size), name='{}_pool'.format(name))(X)
else:
X = stride_conv(X, channel, pool_size, activation=activation, name=name)
# stack linear convolutional layers
X = CONV_stack(X, channel, kernel_size, stack_num=1, activation=activation, name=name)
return X
def UNET_right(X, X_left, channel, kernel_size=3, pool_size=2, activation='relu', name='right0'):
'''
Decoder block of UNet
Input
----------
X: input tensor
channel: number of convolution filters
kernel_size: size of 2-d convolution kernels
pool_size: size of transpose stride, expected to be the same as "UNET_left"
activation: 'relu' for ReLU or 'leaky' for Leaky ReLU
name: name of created keras layers
Output
----------
X: output tensor
'''
# Transpose convolutional layer --> stacked linear convolutional layers
X = keras.layers.Conv2DTranspose(channel, kernel_size, strides=(pool_size, pool_size),
padding='same', name=name+'_trans_conv')(X)
X = CONV_stack(X, channel, kernel_size, stack_num=1, activation=activation, name=name+'_conv_after_trans')
# Tensor concatenation
H = keras.layers.concatenate([X_left, X], axis=3)
# stacked linear convolutional layers after concatenation
H = CONV_stack(H, channel, kernel_size, stack_num=1, activation=activation, name=name+'_conv_after_concat')
return H
def NEST_UNET_right(X_conv, X_list, channel, kernel_size=3, pool_size=2, activation='relu', name='right0'):
'''
Decoder block of Nest-UNet
Input
----------
X: input tensor
X_list: a list of other corresponded input tensors (see Sha 2020b, Figure 2)
channel: number of convolution filters
kernel_size: size of 2-d convolution kernels
pool_size: size of transpose stride, expected to be the same as "UNET_left"
activation: 'relu' for ReLU or 'leaky' for Leaky ReLU
name: name of created keras layers
Output
----------
X: output tensor
'''
# Transpose convolutional layer --> concatenation
X_unpool = keras.layers.Conv2DTranspose(channel, kernel_size, strides=(pool_size, pool_size),
padding='same', name=name+'_trans_conv')(X_conv)
# <--- *stacked convolutional can be applied here
X_unpool = keras.layers.concatenate([X_unpool]+X_list, axis=3, name=name+'_nest')
# Stacked convolutions after concatenation
X_conv = CONV_stack(X_unpool, channel, kernel_size, stack_num=2, activation=activation, name=name+'_conv_after_concat')
return X_conv
# -----------------------------------
# Architectures of UNET/UNET_AE/NEST_UNET
# -----------------------------------
def UNET(layer_N, input_size, input_stack_num=2, pool=True, activation='relu'):
'''
UNet with three down- and upsampling levels.
Input
----------
layer_N: Number of convolution filters on each down- and upsampling levels
Should be an iterable of four int numbers, e.g., [32, 64, 128, 256]
input_size: a tuple that feed input keras.layers.Input, e.g. (None, None, 3)
input_stack_num: number of stacked convolutional layers before entering the first downsampling block
pool: True for maxpooling, False for strided convolutional layers
activation: 'relu' for ReLU or 'leaky' for Leaky ReLU
Output
----------
model: the Keras functional API model, i.e., keras.models.Model(inputs=[IN], outputs=[OUT])
'''
# input layer
IN = keras.layers.Input(input_size, name='unet_in')
temp = keras.layers.UpSampling2D(size=2, interpolation='bilinear')(IN)
temp = keras.layers.UpSampling2D(size=2, interpolation='bilinear')(temp)
X_en1 = CONV_stack(temp, layer_N[0], kernel_size=3, stack_num=input_stack_num, activation=activation, name='unet_left0')
# downsampling levels
X_en2 = UNET_left(X_en1, layer_N[1], pool=pool, activation=activation, name='unet_left1')
X_en3 = UNET_left(X_en2, layer_N[2], pool=pool, activation=activation, name='unet_left2')
X4 = UNET_left(X_en3, layer_N[3], pool=pool, activation=activation, name='unet_bottom')
# upsampling levels
X_de3 = UNET_right(X4, X_en3, layer_N[2], activation=activation, name='unet_right2')
X_de2 = UNET_right(X_de3, X_en2, layer_N[1], activation=activation, name='unet_right1')
X_de1 = UNET_right(X_de2, X_en1, layer_N[0], activation=activation, name='unet_right0')
# output
OUT = CONV_stack(X_de1, 2, kernel_size=3, stack_num=1, activation=activation, name='unet_out')
OUT = keras.layers.Conv2D(1, 1, activation=keras.activations.linear, padding='same', name='unet_exit')(OUT)
# model
model = keras.models.Model(inputs=[IN], outputs=[OUT], name='UNet')
return model
def NEST_UNET(layer_N, input_size, input_stack_num=2, pool=True, activation='relu'):
'''
Nest-UNet (or UNet++) with three down- and upsampling levels.
Input
----------
layer_N: Number of convolution filters on each down- and upsampling levels
Should be an iterable of four int numbers, e.g., [32, 64, 128, 256]
input_size: a tuple that feed input keras.layers.Input, e.g. (None, None, 3)
input_stack_num: number of stacked convolutional layers before entering the first downsampling block
activation: 'relu' for ReLU or 'leaky' for Leaky ReLU
pool: True for maxpooling, False for strided convolutional layers
Output
----------
model: the Keras functional API model, i.e., keras.models.Model(inputs=[IN], outputs=[OUT])
'''
# input layer
IN = keras.layers.Input(input_size, name='unet_in')
temp = keras.layers.UpSampling2D(size=2, interpolation='bilinear')(IN)
temp = keras.layers.UpSampling2D(size=2, interpolation='bilinear')(temp)
X11_conv = CONV_stack(temp, layer_N[0], kernel_size=3, stack_num=input_stack_num, activation=activation, name='unet_left0')
# downsampling levels (same as in the UNET)
X21_conv = UNET_left(X11_conv, layer_N[1], pool=pool, activation=activation, name='unet_left1')
X31_conv = UNET_left(X21_conv, layer_N[2], pool=pool, activation=activation, name='unet_left2')
X41_conv = UNET_left(X31_conv, layer_N[3], pool=pool, activation=activation, name='unet_bottom')
# up-sampling part 1
X12_conv = NEST_UNET_right(X21_conv, [X11_conv], layer_N[0], activation=activation, name='nest_unet_12')
X22_conv = NEST_UNET_right(X31_conv, [X21_conv], layer_N[1], activation=activation, name='nest_unet_22')
X32_conv = NEST_UNET_right(X41_conv, [X31_conv], layer_N[2], activation=activation, name='nest_unet_32')
# up-sampling part 2
X13_conv = NEST_UNET_right(X22_conv, [X11_conv, X12_conv], layer_N[0], activation=activation, name='nest_unet_right13')
X23_conv = NEST_UNET_right(X32_conv, [X21_conv, X22_conv], layer_N[1], activation=activation, name='nest_unet_right23')
# up-sampling part 3
X14_conv = NEST_UNET_right(X23_conv, [X11_conv, X12_conv, X13_conv], layer_N[0], activation=activation, name='nest_unet_right14')
# output
OUT = CONV_stack(X14_conv, 2, kernel_size=3, stack_num=1, activation=activation, name='nest_unet_out')
OUT = keras.layers.Conv2D(1, 1, activation=keras.activations.linear)(OUT)
# model
model = keras.models.Model(inputs=[IN], outputs=[OUT], name='Nest_UNet')
return model