-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmodel.py
418 lines (366 loc) · 17.2 KB
/
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
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
## KPB: Attention U-Net for accurate radiotherapy dose prediction
# @author: Alexander F.I. Osman, April 2021
"""
This code demonstrates an attention U-Net model for voxel-wise dose prediction in radiation therapy.
The model is trained, validated, and tested using the OpenKBP—2020 AAPM Grand Challenge dataset.
"""
##########################################################
# Import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import glob
import tensorflow as tf
import random
from keras.models import load_model
from sklearn.model_selection import train_test_split
from keras.models import Model
from keras.layers import Input, Conv3D, MaxPooling3D, concatenate, Conv3DTranspose, BatchNormalization, Dropout, Lambda
from keras.optimizers import Adam
import tensorflow as tf
from tensorflow.keras import models, layers, regularizers
from tensorflow.keras import backend as K
###############################################################################
# 1. LOAD DATA AND PREFORM PRE-PROCESSING #####################################
###############################################################################
combined_X = np.load('saved_data/combined_X.npy')
combined_Y = np.load('saved_data/combined_Y.npy')
X_train, X_test, Y_train, Y_test = train_test_split(combined_X, combined_Y, test_size=0.20, random_state=1)
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size=0.20, random_state=1)
###############################################################################
# 2. BUILD THE MODEL ARCHITECTURE #############################################
###############################################################################
# For consistency
# Since the neural network starts with random initial weights, the results of this
# example will differ slightly every time it is run. The random seed is set to avoid
# this randomness. However this is not necessary for your own applications.
seed = 42
np.random.seed = seed
def conv_block(x, size, dropout):
# Convolutional layer.
conv = layers.Conv3D(size, (3, 3, 3), kernel_initializer='he_uniform', padding="same")(x)
conv = layers.Activation("relu")(conv)
conv = layers.Conv3D(size, (3, 3, 3), kernel_initializer='he_uniform', padding="same")(conv)
conv = layers.Activation("relu")(conv)
if dropout > 0:
conv = layers.Dropout(dropout)(conv)
return conv
def gating_signal(input, out_size):
# resize the down layer feature map into the same dimension as the up layer feature map
# using 1x1 conv
# :return: the gating feature map with the same dimension of the up layer feature map
x = layers.Conv3D(out_size, (1, 1, 1), kernel_initializer='he_uniform', padding='same')(input)
x = layers.Activation('relu')(x)
return x
def attention_block(x, gating, inter_shape):
shape_x = K.int_shape(x) # (None, 8, 8, 8, 128)
shape_g = K.int_shape(gating) # (None, 4, 4, 4, 128)
# Getting the x signal to the same shape as the gating signal
theta_x = layers.Conv3D(inter_shape, (2, 2, 2), strides=(2, 2, 2), kernel_initializer='he_uniform', padding='same')(
x) # 16
shape_theta_x = K.int_shape(theta_x)
# Getting the gating signal to the same number of filters as the inter_shape
phi_g = layers.Conv3D(inter_shape, (1, 1, 1), kernel_initializer='he_uniform', padding='same')(gating)
upsample_g = layers.Conv3DTranspose(inter_shape, (3, 3, 3),
strides=(shape_theta_x[1] // shape_g[1], shape_theta_x[2] // shape_g[2],
shape_theta_x[3] // shape_g[3]),
kernel_initializer='he_uniform', padding='same')(phi_g) # 16
concat_xg = layers.add([upsample_g, theta_x])
act_xg = layers.Activation('relu')(concat_xg)
psi = layers.Conv3D(1, (1, 1, 1), kernel_initializer='he_uniform', padding='same')(act_xg)
sigmoid_xg = layers.Activation('sigmoid')(psi)
shape_sigmoid = K.int_shape(sigmoid_xg)
upsample_psi = layers.UpSampling3D(
size=(shape_x[1] // shape_sigmoid[1], shape_x[2] // shape_sigmoid[2], shape_x[3] // shape_sigmoid[3]))(
sigmoid_xg) # 32
upsample_psi = repeat_elem(upsample_psi, shape_x[4])
y = layers.multiply([upsample_psi, x])
result = layers.Conv3D(shape_x[4], (1, 1, 1), kernel_initializer='he_uniform', padding='same')(y)
return result
# Parameters for model
img_height = X_train.shape[1] # 64
img_width = X_train.shape[2] # 64
img_depth = X_train.shape[3] # 64
img_channels = X_train.shape[4] # 12
input_shape = (img_height, img_width, img_depth, img_channels)
def UNet_3D_Model(input_shape):
# network structure
filter_numb = 16 # number of filters for the first layer
inputs = layers.Input(input_shape, dtype=tf.float32)
# Contraction path:
# DownRes 1, convolution + pooling
conv_64 = conv_block(inputs, filter_numb, dropout=0.10)
pool_32 = layers.MaxPooling3D((2, 2, 2), padding="same")(conv_64)
# DownRes 2
conv_32 = conv_block(pool_32, 2 * filter_numb, dropout=0.15)
pool_16 = layers.MaxPooling3D((2, 2, 2), padding="same")(conv_32)
# DownRes 3
conv_16 = conv_block(pool_16, 4 * filter_numb, dropout=0.20)
pool_8 = layers.MaxPooling3D((2, 2, 2), padding="same")(conv_16)
# DownRes 4
conv_8 = conv_block(pool_8, 8 * filter_numb, dropout=0.25)
pool_4 = layers.MaxPooling3D((2, 2, 2), padding="same")(conv_8)
# DownRes 5, convolution only
conv_4 = conv_block(pool_4, 16 * filter_numb, dropout=0.30)
# Upsampling layers
up_8 = layers.UpSampling3D((2, 2, 2), data_format="channels_last")(conv_4)
up_8 = layers.concatenate([up_8, conv_8])
up_conv_8 = conv_block(up_8, 8 * filter_numb, dropout=0.25)
# UpRes 7
up_16 = layers.UpSampling3D((2, 2, 2), data_format="channels_last")(up_conv_8)
up_16 = layers.concatenate([up_16, conv_16])
up_conv_16 = conv_block(up_16, 4 * filter_numb, dropout=0.20)
# UpRes 8
up_32 = layers.UpSampling3D((2, 2, 2), data_format="channels_last")(up_conv_16)
up_32 = layers.concatenate([up_32, conv_32])
up_conv_32 = conv_block(up_32, 2 * filter_numb, dropout=0.15)
# UpRes 9
up_64 = layers.UpSampling3D((2, 2, 2), data_format="channels_last")(up_conv_32)
up_64 = layers.concatenate([up_64, conv_64])
up_conv_64 = conv_block(up_64, filter_numb, dropout=0.10)
# final convolutional layer
conv_final = layers.Conv3D(1, (1, 1, 1))(up_conv_64)
conv_final = layers.Activation('linear')(conv_final)
model = models.Model(inputs=[inputs], outputs=[conv_final], name="UNet_3D_Model")
model.summary()
return model
# Test if everything is working ok.
model = UNet_3D_Model(input_shape)
print(model.input_shape)
print(model.output_shape)
def Attention_UNet_3D_Model(input_shape):
# network structure
filter_numb = 16 # number of filters for the first layer
inputs = layers.Input(input_shape, dtype=tf.float32)
# Downsampling layers
# DownRes 1, convolution + pooling
conv_64 = conv_block(inputs, filter_numb, dropout=0.10)
pool_32 = layers.MaxPooling3D((2, 2, 2), padding="same")(conv_64)
# DownRes 2
conv_32 = conv_block(pool_32, 2 * filter_numb, dropout=0.15)
pool_16 = layers.MaxPooling3D((2, 2, 2), padding="same")(conv_32)
# DownRes 3
conv_16 = conv_block(pool_16, 4 * filter_numb, dropout=0.20)
pool_8 = layers.MaxPooling3D((2, 2, 2), padding="same")(conv_16)
# DownRes 4
conv_8 = conv_block(pool_8, 8 * filter_numb, dropout=0.25)
pool_4 = layers.MaxPooling3D((2, 2, 2), padding="same")(conv_8)
# DownRes 5, convolution only
conv_4 = conv_block(pool_4, 16 * filter_numb, dropout=0.30)
# Upsampling layers
# UpRes 6, attention gated concatenation + upsampling + double residual convolution
gating_8 = gating_signal(conv_4, 8 * filter_numb)
att_8 = attention_block(conv_8, gating_8, 8 * filter_numb)
up_8 = layers.UpSampling3D((2, 2, 2), data_format="channels_last")(conv_4)
up_8 = layers.concatenate([up_8, att_8])
up_conv_8 = conv_block(up_8, 8 * filter_numb, dropout=0.25)
# UpRes 7
gating_16 = gating_signal(up_conv_8, 4 * filter_numb)
att_16 = attention_block(conv_16, gating_16, 4 * filter_numb)
up_16 = layers.UpSampling3D((2, 2, 2), data_format="channels_last")(up_conv_8)
up_16 = layers.concatenate([up_16, att_16])
up_conv_16 = conv_block(up_16, 4 * filter_numb, dropout=0.20)
# UpRes 8
gating_32 = gating_signal(up_conv_16, 2 * filter_numb)
att_32 = attention_block(conv_32, gating_32, 2 * filter_numb)
up_32 = layers.UpSampling3D((2, 2, 2), data_format="channels_last")(up_conv_16)
up_32 = layers.concatenate([up_32, att_32])
up_conv_32 = conv_block(up_32, 2 * filter_numb, dropout=0.15)
# UpRes 9
gating_64 = gating_signal(up_conv_32, filter_numb)
att_64 = attention_block(conv_64, gating_64, filter_numb)
up_64 = layers.UpSampling3D(size=(2, 2, 2), data_format="channels_last")(up_conv_32)
up_64 = layers.concatenate([up_64, att_64])
up_conv_64 = conv_block(up_64, filter_numb, dropout=0.10)
# final convolutional layer
conv_final = layers.Conv3D(1, (1, 1, 1))(up_conv_64)
conv_final = layers.Activation('linear')(conv_final)
model = models.Model(inputs=[inputs], outputs=[conv_final], name="Attention_UNet_3D_Model")
model.summary()
return model
# Test if everything is working ok.
model = Attention_UNet_3D_Model(input_shape)
print(model.input_shape)
print(model.output_shape)
###############################################################################
# 3. TRAIN AND VALIDATE THE CNN MODEL #########################################
###############################################################################
# Fit the model
epochs = 120
batch_size = 4
steps_per_epoch = len(X_train) // batch_size
val_steps_per_epoch = len(X_val) // batch_size
metrics = ['accuracy', 'mae']
loss = 'mean_squared_error'
LR = 0.001
optimizer = tf.keras.optimizers.Adam(LR)
# model = UNet_3D_Model(input_shape=input_shape)
model = Attention_UNet_3D_Model(input_shape=input_shape)
model.compile(optimizer=Adam(learning_rate=0.001), loss='mean_squared_error', metrics=['accuracy', 'mae'])
# model.compile(optimizer = optimizer, loss=loss, metrics=metrics)
print(model.summary())
print(model.input_shape)
print(model.output_shape)
## TO PREVENT OVERFITTING: Use early stopping method to solve model over-fitting problem
early_stopping = tf.keras.callbacks.EarlyStopping(patience=30, monitor='val_loss', verbose=1)
# The patience parameter is the amount of epochs to check for improvement
## Checkpoint: ModelCheckpoint callback saves a model at some interval.
# checkpoint_filepath = 'saved_model/UNet_best_model.epoch{epoch:02d}-loss{val_loss:.2f}.hdf5'
checkpoint_filepath = 'saved_model/Att_UNet_best_model.epoch{epoch:02d}-loss{val_loss:.2f}.hdf5'
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_filepath,
monitor='val_loss',
verbose=1,
save_best_only=True,
save_weights_only=False,
mode='min', # #Use Mode = max for accuracy and min for loss.
)
"""
# Decaying learning rate
reduce_lr = tf.keras.callbacks.callback_reduce_lr_on_plateau(
monitor = "val_loss",
factor = 0.1,
patience = 10,
verbose = 0,
mode = c("auto", "min", "max"),
min_delta = 1e-04,
cooldown = 0,
min_lr = 0)
"""
## CSVLogger logs epoch, acc, loss, val_acc, val_loss
log_csv = tf.keras.callbacks.CSVLogger('my_logs.csv', separator=',', append=False)
# Train the model
import time
start = time.time()
# start1 = datetime.now()
history = model.fit(X_train, Y_train,
steps_per_epoch=steps_per_epoch,
batch_size=batch_size,
epochs=epochs,
verbose=1,
callbacks=[early_stopping, checkpoint, log_csv],
validation_data=(X_val, Y_val),
validation_steps=val_steps_per_epoch,
shuffle=False,
)
finish = time.time()
# stop = datetime.now()
# Execution time of the model
print('total execution time in seconds is: ', finish - start)
# print(history.history.keys())
print('Training has been finished successfully')
## Plot training history
## LEARNING CURVE: plots the graph of the training loss vs.validation
# loss over the number of epochs.
def plot_history(history):
plt.figure()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('average training loss and validation loss')
plt.ylabel('mean-squared error')
plt.xlabel('epoch')
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.legend(['training loss', 'validation loss'], loc='upper right')
plt.show()
plot_history(history)
# Save Model
model.save('saved_model/dose_pred_Att_Unet3D_model_120epochs.hdf5')
## Evaluating the model
train_loss, train_acc, train_acc1 = model.evaluate(X_train, Y_train, batch_size = 8)
val_loss, test_acc, train_acc2 = model.evaluate(X_val, Y_val, batch_size = 8)
print('Train: %.3f, Test: %.3f' % (train_loss, val_loss))
###############################################################################
# 4. MAKE PREDICTIONS ON TEST DATASET #########################################
###############################################################################
# load test data >> for DVH plot and visualization
X_test = np.load('saved_data/X_test.npy')
Y_test = np.load('saved_data/Y_test.npy')
# Set compile=False as we are not loading it for training, only for prediction.
# Loading
#new_model = load_model('saved_model/UNet_best_model.epoch113-loss0.00.hdf5')
new_model = load_model('saved_model/Att_UNet_best_model.epoch112-loss0.00.hdf5')
# Check its architecture
new_model.summary()
# Predict on the test set: Let us see how the model generalize by using the test set.
predict_test = new_model.predict(X_test, verbose=1, batch_size = 4)
#Processing the negative values
predict_test2 = []
for pt_id in range(len(X_test)):
print("pt_id: ", pt_id)
predict_test1 = predict_test[pt_id].astype('float32')
id1 = X_test[pt_id,:,:,:,0].astype(int)
predict_test1[predict_test1 <=0] = 0
predict_test2.append(np.array(predict_test1))
predict_test2 = (np.array(predict_test2)).astype('float32')
predict_test = predict_test2
img_height = X_test.shape[1] # 64
img_width = X_test.shape[2] # 64
img_depth = X_test.shape[3] # 64
img_channels = X_test.shape[4] # 1
predict_test = np.reshape(predict_test, (len(predict_test), img_height, img_width, img_depth))
real_test = np.reshape(Y_test, (len(Y_test), img_height, img_width, img_depth))
# Renormalize the dose to original scale by multipying with the prescription (70 Gy)
predict_test = predict_test * 70
real_test = real_test * 70
# Axial, Sagital, & Coronal views
image_number = 42
slice_number = 11
fig = plt.figure()
grid = plt.GridSpec(3, 4, wspace = .15, hspace = .15)
exec (f"plt.subplot(grid{[0]})")
plt.imshow(X_test[image_number,:,:,slice_number,11], cmap='gray')
plt.title('ct'),
plt.colorbar(), plt.axis('off')
exec (f"plt.subplot(grid{[1]})")
plt.imshow(predict_test[image_number,:,:,slice_number], cmap='jet')
plt.title('Predicted Test'),
plt.colorbar(), plt.axis('off')
exec (f"plt.subplot(grid{[2]})")
plt.imshow(real_test[image_number,:,:,slice_number], cmap='jet')
plt.title('GT'),
plt.colorbar(), plt.axis('off')
exec (f"plt.subplot(grid{[3]})")
plt.imshow((predict_test[image_number,:,:,slice_number] - real_test[image_number,:,:,slice_number]), cmap='jet')
plt.title('residual'),
plt.colorbar(label='Gy'), plt.axis('off')
image_number = 51
slice_number = 30
exec (f"plt.subplot(grid{[4]})")
plt.imshow(X_test[image_number,:,slice_number,:,11].T, cmap='gray')
plt.title('ct'),
plt.colorbar(), plt.axis('off')
exec (f"plt.subplot(grid{[5]})")
plt.imshow(predict_test[image_number,:,slice_number,:].T, cmap='jet')
plt.title('Predicted Test'),
plt.colorbar(), plt.axis('off')
exec (f"plt.subplot(grid{[6]})")
plt.imshow(real_test[image_number,:,slice_number,:].T, cmap='jet')
plt.title('GT'),
plt.colorbar(label='Gy'), plt.axis('off')
exec (f"plt.subplot(grid{[7]})")
plt.imshow((predict_test[image_number,:,slice_number,:] - real_test[image_number,:,slice_number,:]).T, cmap='jet')
plt.title('residual'),
plt.colorbar(label='Gy'), plt.axis('off')
image_number = 51
slice_number = 28
exec (f"plt.subplot(grid{[8]})")
plt.imshow(X_test[image_number,slice_number,:,:,11].T, cmap='gray')
plt.title('ct'),
plt.colorbar(), plt.axis('off')
exec (f"plt.subplot(grid{[9]})")
plt.imshow(predict_test[image_number,slice_number,:,:].T, cmap='jet')
plt.title('Predicted Test'),
plt.colorbar(label='Gy'), plt.axis('off')
exec (f"plt.subplot(grid{[10]})")
plt.imshow(real_test[image_number,slice_number,:,:].T, cmap='jet')
plt.title('GT'),
plt.axis('off')
plt.colorbar(label='Gy')
exec (f"plt.subplot(grid{[11]})")
plt.imshow((predict_test[image_number,slice_number,:,:] - real_test[image_number,slice_number,:,:]).T, cmap='jet')
plt.title('residual'),
plt.axis('off')
plt.colorbar(label='Gy')
plt.show()
###############################################################################
###############################################################################