|
a |
|
b/code/train-mask.py |
|
|
1 |
""" |
|
|
2 |
Purpose: train a machine learning segmenter that can segment out the nodules on a given 2D patient CT scan slice |
|
|
3 |
Note: |
|
|
4 |
- this will train from scratch, with no preloaded weights |
|
|
5 |
- weights are saved to unet.hdf5 in the specified output folder |
|
|
6 |
""" |
|
|
7 |
|
|
|
8 |
from __future__ import print_function |
|
|
9 |
|
|
|
10 |
import numpy as np |
|
|
11 |
from keras.models import Model |
|
|
12 |
from keras.layers import Input, merge, Convolution2D, MaxPooling2D, UpSampling2D |
|
|
13 |
from keras.optimizers import Adam |
|
|
14 |
from keras.optimizers import SGD |
|
|
15 |
from keras.callbacks import ModelCheckpoint, LearningRateScheduler |
|
|
16 |
from keras import backend as K |
|
|
17 |
|
|
|
18 |
WORKING_PATH = "/home/ubuntu/data/output/" |
|
|
19 |
IMG_ROWS = 512 |
|
|
20 |
IMG_COLS = 512 |
|
|
21 |
|
|
|
22 |
SMOOTH = 1. |
|
|
23 |
|
|
|
24 |
K.set_image_dim_ordering('th') # Theano dimension ordering in this code |
|
|
25 |
|
|
|
26 |
def dice_coef(y_true, y_pred): |
|
|
27 |
y_true_f = K.flatten(y_true) |
|
|
28 |
y_pred_f = K.flatten(y_pred) |
|
|
29 |
intersection = K.sum(y_true_f * y_pred_f) |
|
|
30 |
return (2. * intersection + SMOOTH) / (K.sum(y_true_f) + K.sum(y_pred_f) + SMOOTH) |
|
|
31 |
|
|
|
32 |
def dice_coef_loss(y_true, y_pred): |
|
|
33 |
return -dice_coef(y_true, y_pred) |
|
|
34 |
|
|
|
35 |
def dice_coef_np(y_true,y_pred): |
|
|
36 |
y_true_f = y_true.flatten() |
|
|
37 |
y_pred_f = y_pred.flatten() |
|
|
38 |
intersection = np.sum(y_true_f * y_pred_f) |
|
|
39 |
return (2. * intersection + SMOOTH) / (np.sum(y_true_f) + np.sum(y_pred_f) + SMOOTH) |
|
|
40 |
|
|
|
41 |
def get_unet(): |
|
|
42 |
inputs = Input((1,IMG_ROWS, IMG_COLS)) |
|
|
43 |
conv1 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(inputs) |
|
|
44 |
conv1 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(conv1) |
|
|
45 |
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) |
|
|
46 |
|
|
|
47 |
conv2 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(pool1) |
|
|
48 |
conv2 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(conv2) |
|
|
49 |
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) |
|
|
50 |
|
|
|
51 |
conv3 = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(pool2) |
|
|
52 |
conv3 = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(conv3) |
|
|
53 |
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) |
|
|
54 |
|
|
|
55 |
conv4 = Convolution2D(256, 3, 3, activation='relu', border_mode='same')(pool3) |
|
|
56 |
conv4 = Convolution2D(256, 3, 3, activation='relu', border_mode='same')(conv4) |
|
|
57 |
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4) |
|
|
58 |
|
|
|
59 |
conv5 = Convolution2D(512, 3, 3, activation='relu', border_mode='same')(pool4) |
|
|
60 |
conv5 = Convolution2D(512, 3, 3, activation='relu', border_mode='same')(conv5) |
|
|
61 |
|
|
|
62 |
up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv4], mode='concat', concat_axis=1) |
|
|
63 |
conv6 = Convolution2D(256, 3, 3, activation='relu', border_mode='same')(up6) |
|
|
64 |
conv6 = Convolution2D(256, 3, 3, activation='relu', border_mode='same')(conv6) |
|
|
65 |
|
|
|
66 |
up7 = merge([UpSampling2D(size=(2, 2))(conv6), conv3], mode='concat', concat_axis=1) |
|
|
67 |
conv7 = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(up7) |
|
|
68 |
conv7 = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(conv7) |
|
|
69 |
|
|
|
70 |
up8 = merge([UpSampling2D(size=(2, 2))(conv7), conv2], mode='concat', concat_axis=1) |
|
|
71 |
conv8 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(up8) |
|
|
72 |
conv8 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(conv8) |
|
|
73 |
|
|
|
74 |
up9 = merge([UpSampling2D(size=(2, 2))(conv8), conv1], mode='concat', concat_axis=1) |
|
|
75 |
conv9 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(up9) |
|
|
76 |
conv9 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(conv9) |
|
|
77 |
|
|
|
78 |
conv10 = Convolution2D(1, 1, 1, activation='sigmoid')(conv9) |
|
|
79 |
|
|
|
80 |
model = Model(input=inputs, output=conv10) |
|
|
81 |
|
|
|
82 |
model.compile(optimizer=Adam(lr=1.0e-5), loss=dice_coef_loss, metrics=[dice_coef]) |
|
|
83 |
|
|
|
84 |
return model |
|
|
85 |
|
|
|
86 |
|
|
|
87 |
def train_and_evaluate(): |
|
|
88 |
print('-'*30) |
|
|
89 |
print('Loading and preprocessing train data...') |
|
|
90 |
print('-'*30) |
|
|
91 |
imgs_train = np.load(WORKING_PATH+"trainImages.npy").astype(np.float32) |
|
|
92 |
imgs_mask_train = np.load(WORKING_PATH+"trainMasks.npy").astype(np.float32) |
|
|
93 |
|
|
|
94 |
imgs_test = np.load(WORKING_PATH+"testImages.npy").astype(np.float32) |
|
|
95 |
imgs_mask_test_true = np.load(WORKING_PATH+"testMasks.npy").astype(np.float32) |
|
|
96 |
|
|
|
97 |
mean = np.mean(imgs_train) # mean for data centering |
|
|
98 |
std = np.std(imgs_train) # std for data normalization |
|
|
99 |
imgs_train -= mean # images should already be standardized, but just in case |
|
|
100 |
imgs_train /= std |
|
|
101 |
|
|
|
102 |
mean_test = np.mean(imgs_test) # mean for data centering |
|
|
103 |
std_test = np.std(imgs_test) # std for data normalization |
|
|
104 |
imgs_test -= mean_test # images should already be standardized, but just in case |
|
|
105 |
imgs_test /= std_test |
|
|
106 |
|
|
|
107 |
print('-'*30) |
|
|
108 |
print('Creating and compiling model...') |
|
|
109 |
print('-'*30) |
|
|
110 |
model = get_unet() |
|
|
111 |
|
|
|
112 |
# Saving weights to unet.hdf5 at checkpoints |
|
|
113 |
model_checkpoint = ModelCheckpoint('unet.hdf5', monitor='loss', save_best_only=True) |
|
|
114 |
|
|
|
115 |
print('-'*30) |
|
|
116 |
print('Fitting model...') |
|
|
117 |
print('-'*30) |
|
|
118 |
model.fit(imgs_train, imgs_mask_train, batch_size=2, nb_epoch=20, verbose=1, shuffle=True, callbacks=[model_checkpoint]) |
|
|
119 |
print('Fitting ends...') |
|
|
120 |
|
|
|
121 |
print('start evaluation...') |
|
|
122 |
print('evaluation result is: ', model.eva) |
|
|
123 |
|
|
|
124 |
if __name__ == '__main__': |
|
|
125 |
train_and_predict() |