-
Notifications
You must be signed in to change notification settings - Fork 49
/
3D-dense-unet.py
224 lines (164 loc) · 7.6 KB
/
3D-dense-unet.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
from __future__ import print_function
import os
import keras.models as models
from skimage.transform import resize
from skimage.io import imsave
import numpy as np
np.random.seed(1337)
import tensorflow as tf
tf.set_random_seed(1337)
from keras.models import Model
from keras.layers import Input, concatenate, Conv3D, MaxPooling3D, Conv3DTranspose, AveragePooling3D, ZeroPadding3D
from keras.optimizers import RMSprop, Adam, SGD, Adagrad, Adadelta
from keras.callbacks import ModelCheckpoint, CSVLogger
from keras import backend as K
from keras.regularizers import l2
from keras.utils import plot_model
from data3D import load_train_data, load_test_data, preprocess_squeeze
K.set_image_data_format('channels_last')
project_name = '3D-Dense-Unet'
img_rows = 400
img_cols = 400
img_depth = 16
smooth = 1.
def dice_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 (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
def dice_coef_loss(y_true, y_pred):
return -dice_coef(y_true, y_pred)
def get_unet():
inputs = Input((img_depth, img_rows, img_cols, 1))
conv11 = Conv3D(32, (3, 3, 3), activation='relu', padding='same')(inputs)
conc11 = concatenate([inputs, conv11], axis=4)
conv12 = Conv3D(32, (3, 3, 3), activation='relu', padding='same')(conc11)
conc12 = concatenate([inputs, conv12], axis=4)
pool1 = MaxPooling3D(pool_size=(2, 2, 2))(conc12)
conv21 = Conv3D(64, (3, 3, 3), activation='relu', padding='same')(pool1)
conc21 = concatenate([pool1, conv21], axis=4)
conv22 = Conv3D(64, (3, 3, 3), activation='relu', padding='same')(conc21)
conc22 = concatenate([pool1, conv22], axis=4)
pool2 = MaxPooling3D(pool_size=(2, 2, 2))(conc22)
conv31 = Conv3D(128, (3, 3, 3), activation='relu', padding='same')(pool2)
conc31 = concatenate([pool2, conv31], axis=4)
conv32 = Conv3D(128, (3, 3, 3), activation='relu', padding='same')(conc31)
conc32 = concatenate([pool2, conv32], axis=4)
pool3 = MaxPooling3D(pool_size=(2, 2, 2))(conc32)
conv41 = Conv3D(256, (3, 3, 3), activation='relu', padding='same')(pool3)
conc41 = concatenate([pool3, conv41], axis=4)
conv42 = Conv3D(256, (3, 3, 3), activation='relu', padding='same')(conc41)
conc42 = concatenate([pool3, conv42], axis=4)
pool4 = MaxPooling3D(pool_size=(2, 2, 2))(conc42)
conv51 = Conv3D(512, (3, 3, 3), activation='relu', padding='same')(pool4)
conc51 = concatenate([pool4, conv51], axis=4)
conv52 = Conv3D(512, (3, 3, 3), activation='relu', padding='same')(conc51)
conc52 = concatenate([pool4, conv52], axis=4)
up6 = concatenate([Conv3DTranspose(256, (2, 2, 2), strides=(2, 2, 2), padding='same')(conc52), conc42], axis=4)
conv61 = Conv3D(256, (3, 3, 3), activation='relu', padding='same')(up6)
conc61 = concatenate([up6, conv61], axis=4)
conv62 = Conv3D(256, (3, 3, 3), activation='relu', padding='same')(conc61)
conc62 = concatenate([up6, conv62], axis=4)
up7 = concatenate([Conv3DTranspose(128, (2, 2, 2), strides=(2, 2, 2), padding='same')(conc62), conv32], axis=4)
conv71 = Conv3D(128, (3, 3, 3), activation='relu', padding='same')(up7)
conc71 = concatenate([up7, conv71], axis=4)
conv72 = Conv3D(128, (3, 3, 3), activation='relu', padding='same')(conc71)
conc72 = concatenate([up7, conv72], axis=4)
up8 = concatenate([Conv3DTranspose(64, (2, 2, 2), strides=(2, 2, 2), padding='same')(conc72), conv22], axis=4)
conv81 = Conv3D(64, (3, 3, 3), activation='relu', padding='same')(up8)
conc81 = concatenate([up8, conv81], axis=4)
conv82 = Conv3D(64, (3, 3, 3), activation='relu', padding='same')(conc81)
conc82 = concatenate([up8, conv82], axis=4)
up9 = concatenate([Conv3DTranspose(32, (2, 2, 2), strides=(2, 2, 2), padding='same')(conc82), conv12], axis=4)
conv91 = Conv3D(32, (3, 3, 3), activation='relu', padding='same')(up9)
conc91 = concatenate([up9, conv91], axis=4)
conv92 = Conv3D(32, (3, 3, 3), activation='relu', padding='same')(conc91)
conc92 = concatenate([up9, conv92], axis=4)
conv10 = Conv3D(1, (1, 1, 1), activation='sigmoid')(conc92)
model = Model(inputs=[inputs], outputs=[conv10])
model.summary()
#plot_model(model, to_file='model.png')
model.compile(optimizer=Adam(lr=1e-5, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.000000199), loss='binary_crossentropy', metrics=['accuracy'])
return model
def train():
print('-'*30)
print('Loading and preprocessing train data...')
print('-'*30)
imgs_train, imgs_mask_train = load_train_data()
imgs_mask_train = imgs_mask_train.astype('float32')
imgs_train = imgs_train.astype('float32')
imgs_mask_train /= 255. # scale masks to [0, 1]
imgs_train /= 255. # scale masks to [0, 1]
print('-'*30)
print('Creating and compiling model...')
print('-'*30)
model = get_unet()
weight_dir = 'weights'
if not os.path.exists(weight_dir):
os.mkdir(weight_dir)
model_checkpoint = ModelCheckpoint(os.path.join(weight_dir, project_name + '.h5'), monitor='val_loss', save_best_only=True)
log_dir = 'logs'
if not os.path.exists(log_dir):
os.mkdir(log_dir)
csv_logger = CSVLogger(os.path.join(log_dir, project_name + '.txt'), separator=',', append=False)
print('-'*30)
print('Fitting model...')
print('-'*30)
model.fit(imgs_train, imgs_mask_train, batch_size=1, epochs=50, verbose=1, shuffle=True, validation_split=0.10, callbacks=[model_checkpoint, csv_logger])
print('-'*30)
print('Training finished')
print('-'*30)
def predict():
print('-'*30)
print('Loading and preprocessing test data...')
print('-'*30)
imgs_test = load_test_data()
imgs_test = imgs_test.astype('float32')
imgs_test /= 255. # scale masks to [0, 1]
print('-'*30)
print('Loading saved weights...')
print('-'*30)
model = get_unet()
weight_dir = 'weights'
if not os.path.exists(weight_dir):
os.mkdir(weight_dir)
model.load_weights(os.path.join(weight_dir, project_name + '.h5'))
print('-'*30)
print('Predicting masks on test data...')
print('-'*30)
imgs_mask_test = model.predict(imgs_test, batch_size=1, verbose=1)
npy_mask_dir = 'test_mask_npy'
if not os.path.exists(npy_mask_dir):
os.mkdir(npy_mask_dir)
np.save(os.path.join(npy_mask_dir, project_name + '_mask.npy'), imgs_mask_test)
print('-' * 30)
print('Saving predicted masks to files...')
print('-' * 30)
imgs_mask_test = preprocess_squeeze(imgs_mask_test)
# imgs_mask_test /= 1.7
imgs_mask_test = np.around(imgs_mask_test, decimals=0)
imgs_mask_test = (imgs_mask_test*255.).astype(np.uint8)
count_visualize = 1
count_processed = 0
pred_dir = 'preds'
if not os.path.exists(pred_dir):
os.mkdir(pred_dir)
pred_dir = os.path.join('preds/', project_name)
if not os.path.exists(pred_dir):
os.mkdir(pred_dir)
for x in range(0, imgs_mask_test.shape[0]):
for y in range(0, imgs_mask_test.shape[1]):
if (count_visualize > 1) and (count_visualize < 16):
imsave(os.path.join(pred_dir, 'pred_' + str(f"{count_processed:03}") + '.png'), imgs_mask_test[x][y])
count_processed += 1
count_visualize += 1
if count_visualize == 17:
count_visualize = 1
if (count_processed % 100) == 0:
print('Done: {0}/{1} test images'.format(count_processed, imgs_mask_test.shape[0]*14))
print('-'*30)
print('Prediction finished')
print('-'*30)
if __name__ == '__main__':
train()
predict()