forked from jakugel/oct-stargardtretina-seg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemantic_network_models.py
271 lines (194 loc) · 9.88 KB
/
semantic_network_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
from keras.layers import Conv2D, MaxPooling2D, UpSampling2D, concatenate
from keras.layers import Dropout, BatchNormalization, Input, Activation, Add, GlobalAveragePooling2D, Reshape, Dense, multiply, Permute, maximum
from keras import backend as K
from keras.models import Model
def batch_activate(x):
x = BatchNormalization()(x)
x = Activation('relu')(x)
return x
def convolution_block(x, filters, kernel, strides=(1, 1), padding='same', bat_act=True, conv_block_type=1, dilation_rate=1):
if conv_block_type == 1:
x = Conv2D(filters, kernel, strides=strides, padding=padding, dilation_rate=dilation_rate)(x)
if bat_act is True:
x = batch_activate(x)
elif conv_block_type == 2:
if bat_act is True:
x = Activation('relu')(x)
x = Conv2D(filters, kernel, strides=strides, padding=padding, dilation_rate=dilation_rate)(x)
if bat_act is True:
x = BatchNormalization()(x)
elif conv_block_type == 3:
if bat_act is True:
x = batch_activate(x)
x = Conv2D(filters, kernel, strides=strides, padding=padding, dilation_rate=dilation_rate)(x)
return x
def residual_block(block_input, num_filters, conv_layers=2, block_type=3):
inp = block_input
if block_type == 1:
# conv-bn-relu (add before final relu)
for i in range(conv_layers - 1):
inp = convolution_block(inp, num_filters, (3, 3), conv_block_type=1)
x = convolution_block(inp, num_filters, (3, 3), conv_block_type=1)
x = BatchNormalization()(x)
x = Add()([x, block_input])
x = Activation('relu')(x)
elif block_type == 2:
#conv-bn-relu (add before final BN)
for i in range(conv_layers - 1):
inp = convolution_block(inp, num_filters, (3, 3), conv_block_type=1)
x = convolution_block(inp, num_filters, (3, 3), conv_block_type=1)
x = Add()([x, block_input])
x = BatchNormalization()(x)
x = Activation('relu')(x)
elif block_type == 3:
# conv-bn-relu (add at end)
for i in range(conv_layers):
inp = convolution_block(inp, num_filters, (3, 3), conv_block_type=1)
x = Add()([inp, block_input])
elif block_type == 4:
#relu-conv-bn (add at end)
for i in range(conv_layers):
inp = convolution_block(inp, num_filters, (3, 3), conv_block_type=2)
x = Add()([inp, block_input])
elif block_type == 5:
#bn-relu-conv (add at end)
for i in range(conv_layers):
inp = convolution_block(inp, num_filters, (3, 3), conv_block_type=3)
x = Add()([inp, block_input])
return x
def cse_block(inp, ratio=2):
init = inp
channel_axis = 1 if K.image_data_format() == "channels_first" else -1
filters = init._keras_shape[channel_axis]
se_shape = (1, 1, filters)
se = GlobalAveragePooling2D()(init)
se = Reshape(se_shape)(se)
se = Dense(filters // ratio, activation='relu', use_bias=False)(se)
se = Dense(filters, activation='sigmoid', use_bias=False)(se)
if K.image_data_format() == 'channels_first':
se = Permute((3, 1, 2))(se)
x = multiply([init, se])
return x
def sse_block(inp):
x = Conv2D(1, kernel_size=(1, 1), activation='sigmoid', use_bias=False)(inp)
x = multiply([inp, x])
return x
def scse_block(inp, ratio=2):
x1 = cse_block(inp, ratio)
x2 = sse_block(inp)
x = maximum([x1, x2])
return x
def unet_enc_block(inp, size, enc_kernel=(3, 3), conv_layers=2, pool='max', se=None, cSE_ratio=2):
for i in range(conv_layers):
inp = convolution_block(inp, filters=size, kernel=enc_kernel)
if se == 'scSE':
inp = scse_block(inp, ratio=cSE_ratio)
elif se == 'cSE':
inp = cse_block(inp, ratio=cSE_ratio)
elif se == 'sSE':
inp = sse_block(inp)
c = inp
if pool == 'max':
inp = MaxPooling2D(pool_size=(2, 2))(inp)
else:
pass
return [inp, c]
def upsample_conv(inp, size, kernel):
x = UpSampling2D()(inp)
x = convolution_block(x, filters=size, kernel=kernel)
return x
def unet_dec_block(inp, size, concat_map, enc_kernel=(3, 3), dec_kernel=(2, 2), conv_layers=2, se=None, cSE_ratio=2):
x = upsample_conv(inp, size, dec_kernel)
x = concatenate([x, concat_map])
[x, _] = unet_enc_block(x, size, enc_kernel=enc_kernel, conv_layers=conv_layers, pool=False, se=se, cSE_ratio=cSE_ratio)
return x
def resnet_enc_block(inp, size, enc_kernel=(3, 3), block_layers=2, res_layers=1, pool='max', se=None, cSE_ratio=2):
inp = convolution_block(inp, filters=size, kernel=enc_kernel)
for i in range(res_layers):
inp = residual_block(inp, size, block_layers)
if se == 'scSE':
inp = scse_block(inp, ratio=cSE_ratio)
elif se == 'cSE':
inp = cse_block(inp, ratio=cSE_ratio)
elif se == 'sSE':
inp = sse_block(inp)
c = inp
if pool == 'max':
inp = MaxPooling2D(pool_size=(2, 2))(inp)
else:
pass
return [inp, c]
def resnet_dec_block(inp, size, concat_map, enc_kernel=(3, 3), dec_kernel=(2, 2),
block_layers=2, res_layers=1, se=None, cSE_ratio=2, skip_type='concat'):
x = upsample_conv(inp, size, dec_kernel)
if skip_type == 'concat':
x = concatenate([x, concat_map])
elif skip_type == 'add':
x = Add()([x, concat_map])
[x, _] = resnet_enc_block(x, size, enc_kernel=enc_kernel, block_layers=block_layers, res_layers=res_layers,
pool=False, se=se, cSE_ratio=cSE_ratio)
return x
# TODO extend generic unet to allow for dropout parameter (separate dropout in bottleneck and dropout per pooling layer)
def unet(start_neurons, pool_layers, conv_layers, enc_kernel, dec_kernel, input_channels, output_channels, se=None, cSE_ratio=2, pool='max', width=None, height=None):
if K.image_dim_ordering() == 'tf':
inp = Input(batch_shape=(None, width, height, input_channels))
else:
inp = Input(batch_shape=(None, input_channels, width, height))
x = inp
enc = []
for i in range(pool_layers):
[x, c] = unet_enc_block(x, start_neurons * (2 ** i), enc_kernel=enc_kernel, conv_layers=conv_layers, se=se, cSE_ratio=cSE_ratio, pool=pool)
enc.append(c)
[x, _] = unet_enc_block(x, start_neurons * (2 ** pool_layers), enc_kernel=enc_kernel, conv_layers=conv_layers,
pool=False)
x = Dropout(0.5)(x)
for i in range(pool_layers):
x = unet_dec_block(x, start_neurons * (2 ** (pool_layers - 1 - i)), enc[pool_layers - 1 - i],
enc_kernel=enc_kernel, dec_kernel=dec_kernel, conv_layers=conv_layers, se=se, cSE_ratio=cSE_ratio)
o = Conv2D(filters=output_channels, kernel_size=(1, 1), strides=(1, 1), activation="softmax")(x)
arch_params = "{:d}F, {:d}P, {:d}C".format(start_neurons, pool_layers, conv_layers) + ", " + \
str(enc_kernel) + "-" + str(dec_kernel) + "K" + "_convs" + "_" + pool + " pooling"
if se is None:
model_desc = "U-net (" + arch_params + ") {:d}class".format(output_channels)
else:
if se == 'sSE':
model_desc = "U-net (" + arch_params + ", " + se + ") {:d}class".format(output_channels)
else:
model_desc = "U-net (" + arch_params + ", " + se + "_r=" + str(cSE_ratio) + ") {:d}class".format(output_channels)
model_desc_short = "U-net"
return [Model(inputs=inp, outputs=o), model_desc, model_desc_short]
def resnet(start_neurons, pool_layers, block_layers, res_layers, enc_kernel, dec_kernel, input_channels,
output_channels, se=None, cSE_ratio=2, skip_type='concat', type='standard', pool='max', pyramid_bin_sizes=None, pyramid_reduction_factors=None,
width=None, height=None):
if K.image_dim_ordering() == 'tf':
inp = Input(batch_shape=(None, width, height, input_channels))
else:
inp = Input(batch_shape=(None, input_channels, width, height))
x = inp
enc = []
for i in range(pool_layers):
[x, c] = resnet_enc_block(x, start_neurons * (2 ** i), enc_kernel=enc_kernel, block_layers=block_layers,
res_layers=res_layers, se=se, type=type, cSE_ratio=cSE_ratio, pool=pool)
enc.append(c)
[x, _] = resnet_enc_block(x, start_neurons * (2 ** pool_layers), enc_kernel=enc_kernel, block_layers=block_layers,
res_layers=res_layers, pool=False, type=type)
x = Dropout(0.5)(x)
for i in range(pool_layers):
x = resnet_dec_block(x, start_neurons * (2 ** (pool_layers - 1 - i)), enc[pool_layers - 1 - i],
enc_kernel=enc_kernel, dec_kernel=dec_kernel, block_layers=block_layers,
res_layers=res_layers, se=se, skip_type=skip_type, type=type, cSE_ratio=cSE_ratio)
o = Conv2D(filters=output_channels, kernel_size=(1, 1), strides=(1, 1), activation="softmax")(x)
arch_params = "{:d}F, {:d}P, {:d}C, {:d}R".format(start_neurons, pool_layers, block_layers, res_layers, pool) + ", " + \
str(enc_kernel) + "-" + str(dec_kernel) + "K" + "_" + type + " convs" + "_" + pool + " pooling"
if pool == 'pyramid' or pool == 'pyramid_max' or pool == 'pyramid_avg':
arch_params += "_bins" + str(pyramid_bin_sizes) + "_red_factors" + str(pyramid_reduction_factors)
if se is None:
model_desc = "Residual U-net " + type + " (" + arch_params + ") {:d}class".format(output_channels)
else:
if se == 'sSE':
model_desc = "Residual U-net " + type + " (" + arch_params + ", " + se + ") {:d}class".format(output_channels)
else:
model_desc = "Residual U-net " + type + " (" + arch_params + ", " + se + "_r=" + str(cSE_ratio) + ") {:d}class".format(output_channels)
model_desc_short = "Residual U-net"
return [Model(inputs=inp, outputs=o), model_desc, model_desc_short]
# TODO add RNN functionality to semantic networks (RNN bottleneck)