-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcxn_model.py
335 lines (245 loc) · 11.1 KB
/
cxn_model.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
from keras.models import Model
from keras.layers import *
import keras
from tensorflow.keras.optimizers import *
from keras import backend as K
smooth = 0.0000001
def aspp(x,out_shape):
b0=SeparableConv2D(256,(1,1),padding="same",use_bias=False)(x)
b0=BatchNormalization()(b0)
b0=Activation("relu")(b0)
#b5=DepthwiseConv2D((3,3),dilation_rate=(3,3),padding="same",use_bias=False)(x)
#b5=BatchNormalization()(b5)
#b5=Activation("relu")(b5)
#b5=SeparableConv2D(256,(1,1),padding="same",use_bias=False)(b5)
#b5=BatchNormalization()(b5)
#b5=Activation("relu")(b5)
b1=DepthwiseConv2D((3,3),dilation_rate=(6,6),padding="same",use_bias=False)(x)
b1=BatchNormalization()(b1)
b1=Activation("relu")(b1)
b1=SeparableConv2D(256,(1,1),padding="same",use_bias=False)(b1)
b1=BatchNormalization()(b1)
b1=Activation("relu")(b1)
b2=DepthwiseConv2D((3,3),dilation_rate=(12,12),padding="same",use_bias=False)(x)
b2=BatchNormalization()(b2)
b2=Activation("relu")(b2)
b2=SeparableConv2D(256,(1,1),padding="same",use_bias=False)(b2)
b2=BatchNormalization()(b2)
b2=Activation("relu")(b2)
b3=DepthwiseConv2D((3,3),dilation_rate=(18,18),padding="same",use_bias=False)(x)
b3=BatchNormalization()(b3)
b3=Activation("relu")(b3)
b3=SeparableConv2D(256,(1,1),padding="same",use_bias=False)(b3)
b3=BatchNormalization()(b3)
b3=Activation("relu")(b3)
b4=AveragePooling2D(pool_size=(out_shape,out_shape))(x)
b4=SeparableConv2D(256,(1,1),padding="same",use_bias=False)(b4)
b4=BatchNormalization()(b4)
b4=Activation("relu")(b4)
b4=UpSampling2D((out_shape,out_shape), interpolation='bilinear')(b4)
x=Concatenate()([b4,b0,b1,b2,b3])
return x
def jacc_coef(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return 1 - ((intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) - intersection + smooth))
def bn_relu(input_tensor):
"""It adds a Batch_normalization layer before a Relu
"""
input_tensor = BatchNormalization(axis=3)(input_tensor)
return Activation("relu")(input_tensor)
def contr_arm(input_tensor, filters, kernel_size):
"""It adds a feedforward signal to the output of two following conv layers in contracting path
TO DO: remove keras.layers.add and replace it with add only
"""
x = SeparableConv2D(filters, kernel_size, padding='same')(input_tensor)
x = bn_relu(x)
x = SeparableConv2D(filters, kernel_size, padding='same')(x)
x = bn_relu(x)
filters_b = filters // 2
kernel_size_b = (kernel_size[0]-2, kernel_size[0]-2) # creates a kernl size of (1,1) out of (3,3)
x1 = SeparableConv2D(filters_b, kernel_size_b, padding='same')(input_tensor)
x1 = bn_relu(x1)
x1 = concatenate([input_tensor, x1], axis=3)
x = keras.layers.add([x, x1])
x = Activation("relu")(x)
return x
def imprv_contr_arm(input_tensor, filters, kernel_size ):
"""It adds a feedforward signal to the output of two following conv layers in contracting path
"""
x = SeparableConv2D(filters, kernel_size, padding='same')(input_tensor)
x = bn_relu(x)
x0 = SeparableConv2D(filters, kernel_size, padding='same')(x)
x0 = bn_relu(x0)
x = SeparableConv2D(filters, kernel_size, padding='same')(x0)
x = bn_relu(x)
filters_b = filters // 2
kernel_size_b = (kernel_size[0]-2, kernel_size[0]-2) # creates a kernl size of (1,1) out of (3,3)
x1 = SeparableConv2D(filters_b, kernel_size_b, padding='same')(input_tensor)
x1 = bn_relu(x1)
x1 = concatenate([input_tensor, x1], axis=3)
x2 = SeparableConv2D(filters, kernel_size_b, padding='same')(x0)
x2 = bn_relu(x2)
x = keras.layers.add([x, x1, x2])
x = Activation("relu")(x)
return x
def bridge(input_tensor, filters, kernel_size):
"""It is exactly like the identity_block plus a dropout layer. This block only uses in the valley of the UNet
"""
x = SeparableConv2D(filters, kernel_size, padding='same')(input_tensor)
x = bn_relu(x)
x = SeparableConv2D(filters, kernel_size, padding='same')(x)
x = Dropout(.15)(x)
x = bn_relu(x)
filters_b = filters // 2
kernel_size_b = (kernel_size[0]-2, kernel_size[0]-2) # creates a kernl size of (1,1) out of (3,3)
x1 =SeparableConv2D(filters_b, kernel_size_b, padding='same')(input_tensor)
x1 = bn_relu(x1)
x1 = concatenate([input_tensor, x1], axis=3)
x = keras.layers.add([x, x1])
x = Activation("relu")(x)
return x
def conv_block_exp_path(input_tensor, filters, kernel_size):
"""It Is only the convolution part inside each expanding path's block
"""
x = Conv2D(filters, kernel_size, padding='same')(input_tensor)
x = bn_relu(x)
x = Conv2D(filters, kernel_size, padding='same')(x)
x = bn_relu(x)
return x
def conv_block_exp_path3(input_tensor, filters, kernel_size):
"""It Is only the convolution part inside each expanding path's block
"""
x = Conv2D(filters, kernel_size, padding='same')(input_tensor)
x = bn_relu(x)
x = Conv2D(filters, kernel_size, padding='same')(x)
x = bn_relu(x)
x = Conv2D(filters, kernel_size, padding='same')(x)
x = bn_relu(x)
return x
def add_block_exp_path(input_tensor1, input_tensor2, input_tensor3):
"""It is for adding two feed forwards to the output of the two following conv layers in expanding path
"""
x = keras.layers.add([input_tensor1, input_tensor2, input_tensor3])
x = Activation("relu")(x)
return x
def improve_ff_block4(input_tensor1, input_tensor2 ,input_tensor3, input_tensor4, pure_ff):
"""It improves the skip connection by using previous layers feature maps
TO DO: shrink all of ff blocks in one function/class
"""
for ix in range(1):
if ix == 0:
x1 = input_tensor1
x1 = concatenate([x1, input_tensor1], axis=3)
x1 = MaxPooling2D(pool_size=(2, 2))(x1)
for ix in range(3):
if ix == 0:
x2 = input_tensor2
x2 = concatenate([x2, input_tensor2], axis=3)
x2 = MaxPooling2D(pool_size=(4, 4))(x2)
for ix in range(7):
if ix == 0:
x3 = input_tensor3
x3 = concatenate([x3, input_tensor3], axis=3)
x3 = MaxPooling2D(pool_size=(8, 8))(x3)
for ix in range(15):
if ix == 0:
x4 = input_tensor4
x4 = concatenate([x4, input_tensor4], axis=3)
x4 = MaxPooling2D(pool_size=(16, 16))(x4)
x = keras.layers.add([x1, x2, x3, x4, pure_ff])
x = Activation("relu")(x)
return x
def improve_ff_block3(input_tensor1, input_tensor2, input_tensor3, pure_ff):
"""It improves the skip connection by using previous layers feature maps
"""
for ix in range(1):
if ix == 0:
x1 = input_tensor1
x1 = concatenate([x1, input_tensor1], axis=3)
x1 = MaxPooling2D(pool_size=(2, 2))(x1)
for ix in range(3):
if ix == 0:
x2 = input_tensor2
x2 = concatenate([x2, input_tensor2], axis=3)
x2 = MaxPooling2D(pool_size=(4, 4))(x2)
for ix in range(7):
if ix == 0:
x3 = input_tensor3
x3 = concatenate([x3, input_tensor3], axis=3)
x3 = MaxPooling2D(pool_size=(8, 8))(x3)
x = keras.layers.add([x1, x2, x3, pure_ff])
x = Activation("relu")(x)
return x
def improve_ff_block2(input_tensor1, input_tensor2, pure_ff):
"""It improves the skip connection by using previous layers feature maps
"""
for ix in range(1):
if ix == 0:
x1 = input_tensor1
x1 = concatenate([x1, input_tensor1], axis=3)
x1 = MaxPooling2D(pool_size=(2, 2))(x1)
for ix in range(3):
if ix == 0:
x2 = input_tensor2
x2 = concatenate([x2, input_tensor2], axis=3)
x2 = MaxPooling2D(pool_size=(4, 4))(x2)
x = keras.layers.add([x1, x2, pure_ff])
x = Activation("relu")(x)
return x
def improve_ff_block1(input_tensor1, pure_ff):
"""It improves the skip connection by using previous layers feature maps
"""
for ix in range(1):
if ix == 0:
x1 = input_tensor1
x1 = concatenate([x1, input_tensor1], axis=3)
x1 = MaxPooling2D(pool_size=(2, 2))(x1)
x = keras.layers.add([x1, pure_ff])
x = Activation("relu")(x)
return x
def model_arch(input_rows=256, input_cols=256, num_of_channels=3, num_of_classes=1):
inputs = Input((input_rows, input_cols, num_of_channels))
conv1 = Conv2D(16, (3, 3), activation='relu', padding='same')(inputs)
conv1 = contr_arm(conv1, 32, (3, 3))
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = contr_arm(pool1, 64, (3, 3))
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = contr_arm(pool2, 128, (3, 3))
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = contr_arm(pool3, 256, (3, 3))
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = imprv_contr_arm(pool4, 512, (3, 3))
pool5 = MaxPooling2D(pool_size=(2, 2))(conv5)
conv6 = bridge(pool5, 1024, (3, 3))
conv6 = aspp(conv6,input_rows/32)
convT7 = Conv2DTranspose(512, (2, 2), strides=(2, 2), padding='same')(conv6)
prevup7 = improve_ff_block4(input_tensor1=conv4, input_tensor2=conv3, input_tensor3=conv2, input_tensor4=conv1, pure_ff=conv5)
up7 = concatenate([convT7, prevup7], axis=3)
conv7 = conv_block_exp_path3(input_tensor=up7, filters=512, kernel_size=(3, 3))
conv7 = add_block_exp_path(conv7, conv5, convT7)
convT8 = Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv7)
prevup8 = improve_ff_block3(input_tensor1=conv3, input_tensor2=conv2, input_tensor3=conv1, pure_ff=conv4)
up8 = concatenate([convT8, prevup8], axis=3)
conv8 = conv_block_exp_path(input_tensor=up8, filters=256, kernel_size=(3, 3))
conv8 = add_block_exp_path(input_tensor1=conv8, input_tensor2=conv4, input_tensor3=convT8)
convT9 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv8)
prevup9 = improve_ff_block2(input_tensor1=conv2, input_tensor2=conv1, pure_ff=conv3)
up9 = concatenate([convT9, prevup9], axis=3)
conv9 = conv_block_exp_path(input_tensor=up9, filters=128, kernel_size=(3, 3))
conv9 = add_block_exp_path(input_tensor1=conv9, input_tensor2=conv3, input_tensor3=convT9)
convT10 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv9)
prevup10 = improve_ff_block1(input_tensor1=conv1, pure_ff=conv2)
up10 = concatenate([convT10, prevup10], axis=3)
conv10 = conv_block_exp_path(input_tensor=up10, filters=64, kernel_size=(3, 3))
conv10 = add_block_exp_path(input_tensor1=conv10, input_tensor2=conv2, input_tensor3=convT10)
convT11 = Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(conv10)
up11 = concatenate([convT11, conv1], axis=3)
conv11 = conv_block_exp_path(input_tensor=up11, filters=32, kernel_size=(3, 3))
conv11 = add_block_exp_path(input_tensor1=conv11, input_tensor2=conv1, input_tensor3=convT11)
conv12 = Conv2D(num_of_classes, (1, 1), activation='sigmoid')(conv11)
return Model(inputs=[inputs], outputs=[conv12])
model = model_arch(input_rows=384, input_cols=384, num_of_channels=3, num_of_classes=1)
model.compile(optimizer = Adam(lr = 1e-4), loss = jacc_coef, metrics = [jacc_coef,'accuracy'])
len(model.layers)