-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_tf2.py
462 lines (366 loc) · 20.7 KB
/
model_tf2.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# Keras implementation of the paper:
# 3D MRI Brain Tumor Segmentation Using Autoencoder Regularization
# by Myronenko A. (https://arxiv.org/pdf/1810.11654.pdf)
# Author of this code: Suyog Jadhav (https://github.com/IAmSUyogJadhav)
### >>> For GenomeDB server
# from tensorflow.compat.v1 import ConfigProto
# from tensorflow.compat.v1 import InteractiveSession
# config = ConfigProto()
# config.gpu_options.allow_growth = True
# session = InteractiveSession(config=config)
### <<<
import tensorflow.keras.backend as K
from tensorflow.keras.losses import mse
from tensorflow.keras.layers import Conv3D, Activation, Add, UpSampling3D, Lambda, Dense
from tensorflow.keras.layers import Input, Reshape, Flatten, Dropout, SpatialDropout3D
from tensorflow.keras.regularizers import l2
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
# from tensorflow.keras.utils import multi_gpu_model
import tensorflow_addons as tfa
from tensorflow_addons.layers import GroupNormalization
from tensorflow import keras
import tensorflow as tf
class green_block(keras.layers.Layer):
def __init__(self, filters, regularizer, data_format='channels_first', name=None, **kwargs):
super().__init__(**kwargs)
self.hidden = [
Conv3D(filters=filters, kernel_size=(1, 1, 1), strides=1, kernel_regularizer = regularizer, data_format=data_format, name=f'Res_{name}' if name else None),
GroupNormalization(groups = 8, axis = 1 if data_format == 'channels_first' else 0, name = f'GroupNorm_1_{name}' if name else None),
Activation('relu', name=f'Relu_1_{name}' if name else None),
Conv3D(filters=filters, kernel_size=(3, 3, 3), strides=1, padding='same', kernel_regularizer = regularizer, data_format=data_format, name=f'Conv3D_1_{name}' if name else None),
GroupNormalization(groups = 8, axis = 1 if data_format == 'channels_first' else 0, name = f'GroupNorm_2_{name}' if name else None),
Activation('relu', name=f'Relu_2_{name}' if name else None),
Conv3D(filters=filters, kernel_size=(3, 3, 3), strides=1, padding='same', kernel_regularizer = regularizer, data_format=data_format, name=f'Conv3D_2_{name}' if name else None),
Add(name=f'Out_{name}' if name else None)
]
def call(self, inputs):
Z = inputs
inp_res = self.hidden[0](Z)
Z = self.hidden[1](Z)
for layer in self.hidden[2:7]:
Z = layer(Z)
Z = self.hidden[7]([Z, inp_res])
return Z
# From keras-team/keras/blob/master/examples/variational_autoencoder.py
class sampling(keras.layers.Layer):
"""Uses (z_mean, z_log_var) to sample z, the vector encoding a digit."""
def call(self, inputs):
z_mean, z_log_var = inputs
batch = tf.shape(z_mean)[0]
dim = tf.shape(z_mean)[1]
epsilon = tf.keras.backend.random_normal(shape=(batch, dim))
return z_mean + tf.exp(0.5 * z_log_var) * epsilon
def dice_coefficient(y_true, y_pred):
intersection = K.sum(K.abs(y_true * y_pred), axis=[-3,-2,-1])
dn = K.sum(K.square(y_true) + K.square(y_pred), axis=[-3,-2,-1]) + 1e-8
return K.mean(2 * intersection / dn, axis=[0,1])
class DiceLoss(keras.losses.Loss):
def call(self, y_true, y_pred):
y_pred = tf.convert_to_tensor(y_pred)
y_true = tf.cast(y_true, y_pred.dtype)
intersection = tf.reduce_sum(tf.abs(y_true * y_pred), axis=[-3,-2,-1])
dn = tf.reduce_sum(tf.square(y_true) + tf.square(y_pred), axis=[-3,-2,-1]) + 1e-8
return - tf.reduce_mean(2 * intersection / dn, axis=-1)
class LossVAE(keras.layers.Layer):
def __init__(self, weight_L2, weight_KL, n, **kwargs):
super().__init__(**kwargs)
self.weight_L2 = weight_L2
self.weight_KL = weight_KL
self.n = n
def call(self, inputs):
x, out_VAE, z_mean, z_var = inputs
loss_L2 = tf.reduce_mean(tf.square(x - out_VAE), axis=(1, 2, 3, 4)) # original axis value is (1,2,3,4).
loss_KL = (1 / self.n) * tf.reduce_sum(
tf.exp(z_var) + tf.square(z_mean) - 1. - z_var,
axis=-1
)
VAE_loss = tf.reduce_mean(tf.add(self.weight_L2 * loss_L2, self.weight_KL * loss_KL, name = "add_L2_KL"), name = "mean_VAELoss")
self.add_loss(VAE_loss)
return
def loss_gt(e=1e-8):
"""
loss_gt(e=1e-8)
------------------------------------------------------
Since keras does not allow custom loss functions to have arguments
other than the true and predicted labels, this function acts as a wrapper
that allows us to implement the custom loss used in the paper. This function
only calculates - L<dice> term of the following equation. (i.e. GT Decoder part loss)
L = - L<dice> + weight_L2 ∗ L<L2> + weight_KL ∗ L<KL>
Parameters
----------
`e`: Float, optional
A small epsilon term to add in the denominator to avoid dividing by
zero and possible gradient explosion.
Returns
-------
loss_gt_(y_true, y_pred): A custom keras loss function
This function takes as input the predicted and ground labels, uses them
to calculate the dice loss.
"""
def loss_gt_(y_true, y_pred):
intersection = K.sum(K.abs(y_true * y_pred), axis=[-3,-2,-1])
dn = K.sum(K.square(y_true) + K.square(y_pred), axis=[-3,-2,-1]) + e
return - K.mean(2 * intersection / dn, axis=[0,1])
return loss_gt_
def loss_VAE(input_shape, z_mean, z_var, weight_L2=0.1, weight_KL=0.1):
"""
loss_VAE(input_shape, z_mean, z_var, weight_L2=0.1, weight_KL=0.1)
------------------------------------------------------
Since keras does not allow custom loss functions to have arguments
other than the true and predicted labels, this function acts as a wrapper
that allows us to implement the custom loss used in the paper. This function
calculates the following equation, except for -L<dice> term. (i.e. VAE decoder part loss)
L = - L<dice> + weight_L2 ∗ L<L2> + weight_KL ∗ L<KL>
Parameters
----------
`input_shape`: A 4-tuple, required
The shape of an image as the tuple (c, H, W, D), where c is
the no. of channels; H, W and D is the height, width and depth of the
input image, respectively.
`z_mean`: An keras.layers.Layer instance, required
The vector representing values of mean for the learned distribution
in the VAE part. Used internally.
`z_var`: An keras.layers.Layer instance, required
The vector representing values of variance for the learned distribution
in the VAE part. Used internally.
`weight_L2`: A real number, optional
The weight to be given to the L2 loss term in the loss function. Adjust to get best
results for your task. Defaults to 0.1.
`weight_KL`: A real number, optional
The weight to be given to the KL loss term in the loss function. Adjust to get best
results for your task. Defaults to 0.1.
Returns
-------
loss_VAE_(y_true, y_pred): A custom keras loss function
This function takes as input the predicted and ground labels, uses them
to calculate the L2 and KL loss.
"""
def loss_VAE_(y_true, y_pred):
c, H, W, D = input_shape
n = c * H * W * D
loss_L2 = K.mean(K.square(y_true - y_pred), axis=(1, 2, 3, 4)) # original axis value is (1,2,3,4).
loss_KL = (1 / n) * K.sum(
K.exp(z_var) + K.square(z_mean) - 1. - z_var,
axis=-1
)
return loss_L2 + loss_KL
return loss_VAE_
class conv3d_autoenc_reg(keras.Model):
def __init__(self, input_shape=(4, 160, 192, 128), output_channels=3, l2_reg_weight = 1e-5, weight_L2=0.1, weight_KL=0.1,
dice_e=1e-8, test_mode = True, n_gpu = 1, GL_weight = 1, VL_weight = 0.1, **kwargs):
super().__init__(**kwargs)
self.c, self.H, self.W, self.D = input_shape
self.n = self.c * self.H * self.W * self.D
assert len(input_shape) == 4, "Input shape must be a 4-tuple"
if test_mode is not True: assert (self.c % 4) == 0, "The no. of channels must be divisible by 4"
assert (self.H % 16) == 0 and (self.W % 16) == 0 and (self.D % 16) == 0, "All the input dimensions must be divisible by 16"
self.l2_regularizer = l2(l2_reg_weight) if l2_reg_weight is not None else None
self.input_shape_p = input_shape
self.output_channels = output_channels
self.l2_reg_weight = l2_reg_weight
self.weight_L2 = weight_L2
self.weight_KL = weight_KL
self.dice_e = dice_e
self.GL_weight = GL_weight
self.VL_weight = VL_weight
self.LossVAE = LossVAE(weight_L2, weight_KL, self.n)
## The Initial Block
self.Input_x1 = Conv3D(
filters=32,
kernel_size=(3, 3, 3),
strides=1,
padding='same',
kernel_regularizer = self.l2_regularizer,
data_format='channels_first',
name='Input_x1')
## Dropout (0.2)
self.spatial_dropout = SpatialDropout3D(0.2, data_format='channels_first')
## Green Block x1 (output filters = 32)
self.x1 = green_block(32, regularizer = self.l2_regularizer, name='x1')
self.Enc_DownSample_32 = Conv3D(
filters=32,
kernel_size=(3, 3, 3),
strides=2,
padding='same',
kernel_regularizer = self.l2_regularizer,
data_format='channels_first',
name='Enc_DownSample_32')
## Green Block x2 (output filters = 64)
self.Enc_64_1 = green_block(64, regularizer = self.l2_regularizer, name='Enc_64_1')
self.x2 = green_block(64, regularizer = self.l2_regularizer, name='x2')
self.Enc_DownSample_64 = Conv3D(
filters=64,
kernel_size=(3, 3, 3),
strides=2,
padding='same',
kernel_regularizer = self.l2_regularizer,
data_format='channels_first',
name='Enc_DownSample_64')
## Green Blocks x2 (output filters = 128)
self.Enc_128_1 = green_block(128, regularizer = self.l2_regularizer, name='Enc_128_1')
self.x3 = green_block(128, regularizer = self.l2_regularizer, name='x3')
self.Enc_DownSample_128 = Conv3D(filters=128, kernel_size=(3, 3, 3), strides=2, padding='same', kernel_regularizer = self.l2_regularizer,
data_format='channels_first', name='Enc_DownSample_128')
## Green Blocks x4 (output filters = 256)
self.Enc_256_1 = green_block(256, regularizer = self.l2_regularizer, name='Enc_256_1')
self.Enc_256_2 = green_block(256, regularizer = self.l2_regularizer, name='Enc_256_2')
self.Enc_256_3 = green_block(256, regularizer = self.l2_regularizer, name='Enc_256_3')
self.x4 = green_block(256, regularizer = self.l2_regularizer, name='x4')
# -------------------------------------------------------------------------
# Decoder
# -------------------------------------------------------------------------
## GT (Groud Truth) Part
# -------------------------------------------------------------------------
### Green Block x1 (output filters=128)
self.Dec_GT_ReduceDepth_128 = Conv3D(filters=128, kernel_size=(1, 1, 1), strides=1, kernel_regularizer = self.l2_regularizer, data_format='channels_first', name='Dec_GT_ReduceDepth_128')
self.Dec_GT_UpSample_128 = UpSampling3D(size=2, data_format='channels_first', name='Dec_GT_UpSample_128')
self.Input_Dec_GT_128 = Add(name='Input_Dec_GT_128')
self.Dec_GT_128 = green_block(128, regularizer = self.l2_regularizer, name='Dec_GT_128')
### Green Block x1 (output filters=64)
self.Dec_GT_ReduceDepth_64 = Conv3D(filters=64, kernel_size=(1, 1, 1), strides=1, kernel_regularizer = self.l2_regularizer, data_format='channels_first', name='Dec_GT_ReduceDepth_64')
self.Dec_GT_UpSample_64 = UpSampling3D(size=2, data_format='channels_first', name='Dec_GT_UpSample_64')
self.Input_Dec_GT_64 = Add(name='Input_Dec_GT_64')
self.Dec_GT_64 = green_block(64, regularizer = self.l2_regularizer, name='Dec_GT_64')
### Green Block x1 (output filters=32)
self.Dec_GT_ReduceDepth_32 = Conv3D(filters=32, kernel_size=(1, 1, 1), strides=1, kernel_regularizer = self.l2_regularizer, data_format='channels_first',
name='Dec_GT_ReduceDepth_32')
self.Dec_GT_UpSample_32 = UpSampling3D(size=2, data_format='channels_first', name='Dec_GT_UpSample_32')
self.Input_Dec_GT_32 = Add(name='Input_Dec_GT_32')
self.Dec_GT_32 = green_block(32, regularizer = self.l2_regularizer, name='Dec_GT_32')
### Blue Block x1 (output filters=32)
self.Input_Dec_GT_Output = Conv3D(filters=32, kernel_size=(3, 3, 3), strides=1, padding='same', kernel_regularizer = self.l2_regularizer,
data_format='channels_first', name='Input_Dec_GT_Output')
### Output Block
self.Dec_GT_Output = Conv3D(filters=self.output_channels, kernel_size=(1, 1, 1), strides=1, kernel_regularizer = self.l2_regularizer,
data_format='channels_first', activation='sigmoid', name='Dec_GT_Output')
## VAE (Variational Auto Encoder) Part
# -------------------------------------------------------------------------
### VD Block (Reducing dimensionality of the data)
self.Dec_VAE_VD_GN = GroupNormalization(groups=8, axis=1, name='Dec_VAE_VD_GN')
self.Dec_VAE_VD_relu = Activation('relu', name='Dec_VAE_VD_relu')
self.Dec_VAE_VD_Conv3D = Conv3D(filters=16, kernel_size=(3, 3, 3), strides=2, padding='same', kernel_regularizer = self.l2_regularizer,
data_format='channels_first', name='Dec_VAE_VD_Conv3D')
# Not mentioned in the paper, but the author used a Flattening layer here.
self.Dec_VAE_VD_Flatten = Flatten(name='Dec_VAE_VD_Flatten')
self.Dec_VAE_VD_Dense = Dense(256, name='Dec_VAE_VD_Dense')
### VDraw Block (Sampling)
self.Dec_VAE_VDraw_Mean = Dense(128, name='Dec_VAE_VDraw_Mean')
self.Dec_VAE_VDraw_Var = Dense(128, name='Dec_VAE_VDraw_Var')
# self.Dec_VAE_VDraw_Sampling = Lambda(sampling, name='Dec_VAE_VDraw_Sampling')
self.Dec_VAE_VDraw_Sampling = sampling()
### VU Block (Upsizing back to a depth of 256)
c1 = 1
self.VU_Dense1 = Dense((c1) * (self.H//16) * (self.W//16) * (self.D//16))
self.VU_relu = Activation('relu')
self.VU_reshape = Reshape(((c1), (self.H//16), (self.W//16), (self.D//16)))
self.Dec_VAE_ReduceDepth_256 = Conv3D(filters=256, kernel_size=(1, 1, 1), strides=1, kernel_regularizer = self.l2_regularizer, data_format='channels_first',
name='Dec_VAE_ReduceDepth_256')
self.Dec_VAE_UpSample_256 = UpSampling3D(size=2, data_format='channels_first', name='Dec_VAE_UpSample_256')
### Green Block x1 (output filters=128)
self.Dec_VAE_ReduceDepth_128 = Conv3D(filters=128, kernel_size=(1, 1, 1), strides=1, kernel_regularizer = self.l2_regularizer, data_format='channels_first',
name='Dec_VAE_ReduceDepth_128')
self.Dec_VAE_UpSample_128 = UpSampling3D(size=2, data_format='channels_first', name='Dec_VAE_UpSample_128')
self.Dec_VAE_128 = green_block(128, regularizer = self.l2_regularizer, name='Dec_VAE_128')
### Green Block x1 (output filters=64)
self.Dec_VAE_ReduceDepth_64 = Conv3D(filters=64, kernel_size=(1, 1, 1), strides=1, kernel_regularizer = self.l2_regularizer, data_format='channels_first',
name='Dec_VAE_ReduceDepth_64')
self.Dec_VAE_UpSample_64 = UpSampling3D(size=2, data_format='channels_first', name='Dec_VAE_UpSample_64')
self.Dec_VAE_64 = green_block(64, regularizer = self.l2_regularizer, name='Dec_VAE_64')
### Green Block x1 (output filters=32)
self.Dec_VAE_ReduceDepth_32 = Conv3D(filters=32, kernel_size=(1, 1, 1), strides=1, kernel_regularizer = self.l2_regularizer, data_format='channels_first',
name='Dec_VAE_ReduceDepth_32')
self.Dec_VAE_UpSample_32 = UpSampling3D(size=2, data_format='channels_first', name='Dec_VAE_UpSample_32')
self.Dec_VAE_32 = green_block(32, regularizer = self.l2_regularizer, name='Dec_VAE_32')
### Blue Block x1 (output filters=32)
self.Input_Dec_VAE_Output = Conv3D(filters=32, kernel_size=(3, 3, 3), strides=1, padding='same', kernel_regularizer = self.l2_regularizer,
data_format='channels_first', name='Input_Dec_VAE_Output')
### Output Block
self.Dec_VAE_Output = Conv3D(filters=self.c, kernel_size=(1, 1, 1), strides=1, kernel_regularizer = self.l2_regularizer, data_format='channels_first',
name='Dec_VAE_Output')
# def build(self, batch_input_shape):
# n_inputs = batch_input_shape[-1]
# ### super build
# super().build(batch_input_shape)
def call(self, inputs, training=None):
Z = inputs
x = self.Input_x1(Z)
## Dropout (0.2)
x = self.spatial_dropout(x)
## Green Block x1 (output filters = 32)
x1 = self.x1(x)
x = self.Enc_DownSample_32(x1)
## Green Block x2 (output filters = 64)
x = self.Enc_64_1(x)
x2 = self.x2(x)
x = self.Enc_DownSample_64(x2)
## Green Blocks x2 (output filters = 128)
x = self.Enc_128_1(x)
x3 = self.x3(x)
x = self.Enc_DownSample_128(x3)
## Green Blocks x4 (output filters = 256)
x = self.Enc_256_1(x)
x = self.Enc_256_2(x)
x = self.Enc_256_3(x)
x4 = self.x4(x)
# -------------------------------------------------------------------------
# Decoder
# -------------------------------------------------------------------------
## GT (Groud Truth) Part
# -------------------------------------------------------------------------
### Green Block x1 (output filters=128)
x = self.Dec_GT_ReduceDepth_128(x4)
x = self.Dec_GT_UpSample_128(x)
x = self.Input_Dec_GT_128([x, x3])
x = self.Dec_GT_128(x)
### Green Block x1 (output filters=64)
x = self.Dec_GT_ReduceDepth_64(x)
x = self.Dec_GT_UpSample_64(x)
x = self.Input_Dec_GT_64([x, x2])
x = self.Dec_GT_64(x)
### Green Block x1 (output filters=32)
x = self.Dec_GT_ReduceDepth_32(x)
x = self.Dec_GT_UpSample_32(x)
x = self.Input_Dec_GT_32([x, x1])
x = self.Dec_GT_32(x)
### Blue Block x1 (output filters=32)
x = self.Input_Dec_GT_Output(x)
### Output Block
out_GT = self.Dec_GT_Output(x)
## VAE (Variational Auto Encoder) Part
# -------------------------------------------------------------------------
### VD Block (Reducing dimensionality of the data)
x = self.Dec_VAE_VD_GN(x4)
x = self.Dec_VAE_VD_relu(x)
x = self.Dec_VAE_VD_Conv3D(x)
# Not mentioned in the paper, but the author used a Flattening layer here.
x = self.Dec_VAE_VD_Flatten(x)
x = self.Dec_VAE_VD_Dense(x)
### VDraw Block (Sampling)
z_mean = self.Dec_VAE_VDraw_Mean(x)
z_var = self.Dec_VAE_VDraw_Var(x)
x = self.Dec_VAE_VDraw_Sampling([z_mean, z_var])
### VU Block (Upsizing back to a depth of 256)
x = self.VU_Dense1(x)
x = self.VU_relu(x)
x = self.VU_reshape(x)
x = self.Dec_VAE_ReduceDepth_256(x)
x = self.Dec_VAE_UpSample_256(x)
### Green Block x1 (output filters=128)
x = self.Dec_VAE_ReduceDepth_128(x)
x = self.Dec_VAE_UpSample_128(x)
x = self.Dec_VAE_128(x)
### Green Block x1 (output filters=64)
x = self.Dec_VAE_ReduceDepth_64(x)
x = self.Dec_VAE_UpSample_64(x)
x = self.Dec_VAE_64(x)
### Green Block x1 (output filters=32)
x = self.Dec_VAE_ReduceDepth_32(x)
x = self.Dec_VAE_UpSample_32(x)
x = self.Dec_VAE_32(x)
### Blue Block x1 (output filters=32)
x = self.Input_Dec_VAE_Output(x)
### Output Block
out_VAE = self.Dec_VAE_Output(x)
self.LossVAE([Z, out_VAE, z_mean, z_var])
return out_GT