forked from AsmaBaccouche/Connected-Unets-and-more
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConnected_Unets_segmnetation.py
345 lines (273 loc) · 12.9 KB
/
Connected_Unets_segmnetation.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 19 15:43:43 2021
@author: Asma Baccouche
"""
from __future__ import print_function
import os, glob
from skimage.io import imsave
import numpy as np
import tensorflow as tf
from keras.models import Model
from keras.layers import Input, concatenate, Conv2D, Add, MaxPooling2D, Conv2DTranspose, BatchNormalization
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint
from keras import backend as K
#from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
from data import load_train_data, load_test_data
from sklearn.model_selection import train_test_split
K.set_image_data_format('channels_last') # TF dimension ordering in this code
#identify GPU
device_name = tf.test.gpu_device_name()
if not tf.config.list_physical_devices('GPU'):
raise SystemError('GPU device not found')
print('Found GPU at: {}'.format(device_name))
gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction = 0.75)
sess = tf.compat.v1.Session(config = tf.compat.v1.ConfigProto(gpu_options = gpu_options))
tf.compat.v1.keras.backend.set_session(sess)
img_rows = 256
img_cols = 256
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 iou_coef(y_true, y_pred, smooth=1):
intersection = K.sum(K.abs(y_true * y_pred), axis=[1,2,3])
union = K.sum(y_true,[1,2,3])+K.sum(y_pred,[1,2,3])-intersection
iou = K.mean((intersection + smooth) / (union + smooth), axis=0)
return iou
def dice_coef_loss(y_true, y_pred):
return -dice_coef(y_true, y_pred)
def focal_loss(y_true, y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
BCE = K.binary_crossentropy(y_true_f, y_pred_f)
BCE_EXP = K.exp(-BCE)
focal_loss = K.mean(0.8 * K.pow((1-BCE_EXP), 2.) * BCE)
return focal_loss
def tversky(y_true, y_pred):
y_true_pos = K.flatten(y_true)
y_pred_pos = K.flatten(y_pred)
true_pos = K.sum(y_true_pos * y_pred_pos)
false_neg = K.sum(y_true_pos * (1-y_pred_pos))
false_pos = K.sum((1-y_true_pos)*y_pred_pos)
alpha = 0.7
return (true_pos + smooth)/(true_pos + alpha*false_neg + (1-alpha)*false_pos + smooth)
def loss(y_true, y_pred):
return -(0.4*dice_coef(y_true, y_pred)+0.6*iou_coef(y_true, y_pred))
def aspp_block(x, num_filters, rate_scale=1):
x1 = Conv2D(num_filters, (3, 3), dilation_rate=(6 * rate_scale, 6 * rate_scale), padding="same")(x)
x1 = BatchNormalization()(x1)
x2 = Conv2D(num_filters, (3, 3), dilation_rate=(12 * rate_scale, 12 * rate_scale), padding="same")(x)
x2 = BatchNormalization()(x2)
x3 = Conv2D(num_filters, (3, 3), dilation_rate=(18 * rate_scale, 18 * rate_scale), padding="same")(x)
x3 = BatchNormalization()(x3)
x4 = Conv2D(num_filters, (3, 3), padding="same")(x)
x4 = BatchNormalization()(x4)
y = Add()([x1, x2, x3, x4])
y = Conv2D(num_filters, (1, 1), padding="same")(y)
return y
def get_wnet():
inputs = Input((img_rows, img_cols, 3))
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
conv1 = BatchNormalization()(conv1)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
conv1 = BatchNormalization()(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
conv2 = BatchNormalization()(conv2)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
conv2 = BatchNormalization()(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)
conv3 = BatchNormalization()(conv3)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
conv3 = BatchNormalization()(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool3)
conv4 = BatchNormalization()(conv4)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)
conv4 = BatchNormalization()(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = aspp_block(pool4, 512)
up6 = concatenate([Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv5), conv4], axis=3)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(up6)
conv6 = BatchNormalization()(conv6)
conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)
conv6 = BatchNormalization()(conv6)
up7 = concatenate([Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv6), conv3], axis=3)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7)
conv7 = BatchNormalization()(conv7)
conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)
conv7 = BatchNormalization()(conv7)
up8 = concatenate([Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv7), conv2], axis=3)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(up8)
conv8 = BatchNormalization()(conv8)
conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv8)
conv8 = BatchNormalization()(conv8)
up9 = concatenate([Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(conv8), conv1], axis=3)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(up9)
conv9 = BatchNormalization()(conv9)
conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)
conv9 = BatchNormalization()(conv9)
down10 = concatenate([Conv2D(32, (3, 3), activation='relu', padding='same')(conv9), conv9], axis=3)
conv10 = Conv2D(32, (3, 3), activation='relu', padding='same')(down10)
conv10 = BatchNormalization()(conv10)
conv10 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv10)
conv10 = BatchNormalization()(conv10)
pool10 = MaxPooling2D(pool_size=(2, 2))(conv10)
down11 = concatenate([Conv2D(64, (3, 3), activation='relu', padding='same')(pool10), conv8], axis=3)
conv11 = Conv2D(64, (3, 3), activation='relu', padding='same')(down11)
conv11 = BatchNormalization()(conv11)
conv11 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv11)
conv11 = BatchNormalization()(conv11)
pool11 = MaxPooling2D(pool_size=(2, 2))(conv11)
down12 = concatenate([Conv2D(128, (3, 3), activation='relu', padding='same')(pool11), conv7], axis=3)
conv12 = Conv2D(128, (3, 3), activation='relu', padding='same')(down12)
conv12 = BatchNormalization()(conv12)
conv12 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv12)
conv12 = BatchNormalization()(conv12)
pool12 = MaxPooling2D(pool_size=(2, 2))(conv12)
down13 = concatenate([Conv2D(256, (3, 3), activation='relu', padding='same')(pool12), conv6], axis=3)
conv13 = Conv2D(256, (3, 3), activation='relu', padding='same')(down13)
conv13 = BatchNormalization()(conv13)
conv13 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv13)
conv13 = BatchNormalization()(conv13)
pool13 = MaxPooling2D(pool_size=(2, 2))(conv13)
conv14 = aspp_block(pool13, 512)
up15 = concatenate([Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv14), conv13], axis=3)
conv15 = Conv2D(256, (3, 3), activation='relu', padding='same')(up15)
conv15 = BatchNormalization()(conv15)
conv15 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv15)
conv15 = BatchNormalization()(conv15)
up16 = concatenate([Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv15), conv12], axis=3)
conv16 = Conv2D(128, (3, 3), activation='relu', padding='same')(up16)
conv16 = BatchNormalization()(conv16)
conv16 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv16)
conv16 = BatchNormalization()(conv16)
up17 = concatenate([Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv16), conv11], axis=3)
conv17 = Conv2D(64, (3, 3), activation='relu', padding='same')(up17)
conv17 = BatchNormalization()(conv17)
conv17 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv17)
conv17 = BatchNormalization()(conv17)
up18 = concatenate([Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(conv17), conv10], axis=3)
conv18 = Conv2D(32, (3, 3), activation='relu', padding='same')(up18)
conv18 = BatchNormalization()(conv18)
conv18 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv18)
conv18 = BatchNormalization()(conv18)
conv18 = aspp_block(conv18, 32)
conv19 = Conv2D(1, (1, 1), activation='sigmoid')(conv18)
model = Model(inputs=[inputs], outputs=[conv19])
model.compile(optimizer=Adam(1e-4), loss=[loss], metrics=[dice_coef, iou_coef])
return model
name = 'mydata'
#imgs_train1, imgs_mask_train1 = load_train_data('inbreast_cycleGAN')
#imgs_train2, imgs_mask_train2 = load_train_data('mydata')
#
#imgs_train = np.concatenate([imgs_train1, imgs_train2])
#imgs_mask_train = np.concatenate([imgs_mask_train1, imgs_mask_train2])
imgs_train, imgs_mask_train = load_train_data(name)
fname = 'UNets files/wnet_'+name+'_weights.h5'
pred_dir = fname[:-11]
imgs_train = imgs_train.astype('float32')
mean = np.mean(imgs_train) # mean for data centering
std = np.std(imgs_train) # std for data normalization
imgs_train -= mean
imgs_train /= std
imgs_mask_train = imgs_mask_train.astype('float32')
imgs_mask_train /= 255. # scale masks to [0, 1]
imgs_mask_train = imgs_mask_train[..., np.newaxis]
imgs_train, imgs_val, imgs_mask_train, imgs_mask_val = train_test_split(imgs_train, imgs_mask_train, test_size=0.2, random_state=42)
print('-'*30)
print('Creating and compiling model...')
print('-'*30)
model = get_wnet()
model_checkpoint = ModelCheckpoint(fname, monitor='val_loss', save_best_only=True)
print('-'*30)
print('Fitting model...')
print('-'*30)
history = model.fit(imgs_train, imgs_mask_train, batch_size=16, epochs=100, verbose=1, shuffle=True,
validation_data=(imgs_val, imgs_mask_val),
callbacks=[model_checkpoint])
print('-'*30)
print('Loading and preprocessing test data...')
print('-'*30)
#imgs_test1, imgs_id_test1 = load_test_data('inbreast_cycleGAN')
#imgs_test2, imgs_id_test2 = load_test_data('mydata')
#
#imgs_test = np.concatenate([imgs_test1, imgs_test2])
#imgs_id_test = np.concatenate([imgs_id_test1, imgs_id_test2])
imgs_test, imgs_id_test = load_test_data(name)
imgs_test = imgs_test.astype('float32')
imgs_test -= mean
imgs_test /= std
print('-'*30)
print('Loading saved weights...')
print('-'*30)
model.load_weights(fname)
print('-'*30)
print('Predicting masks on test data...')
print('-'*30)
imgs_mask_test = model.predict(imgs_test, verbose=1)
np.save('imgs_mask_test_'+name+'_wunet.npy', imgs_mask_test)
print('-' * 30)
print('Saving predicted masks to files...')
print('-' * 30)
if not os.path.exists(pred_dir):
os.mkdir(pred_dir)
#data_path2 = 'D:/Files/MYDATA/Breast_Cancer-Begonya/Images/Test_Seg/'
#data_path2 = 'D:/INbreast/Test_Seg/'
#data_path2 = 'D:/CBIS_augmented/Test_Seg/'
#data_path2 = 'D:/CSAW-S/CsawS/Test_Seg/'
#data_path2 = 'D:/Files/MYDATA/newdata_mammo/test/'
data_path2 = 'D:/Files/MYDATA/test/'
d = data_path2+'msk/*.png'
files = glob.glob(d)
#files1 = files
#
#data_path2 = 'D:/Files/MYDATA/Breast_Cancer-Begonya/Images/Test_Seg/'
#
#d = data_path2+'roi/*.png'
#files = glob.glob(d)
#
#files2 = files
#
#files = files1 + files2
files = [file.split('\\')[-1][:-4] for file in files]
idx = 0
for image, image_id in zip(imgs_mask_test, imgs_id_test):
image = (image[:, :, 0] * 255.).astype(np.uint8)
imsave(os.path.join(pred_dir, files[idx] + '_pred.png'), image)
idx = idx + 1
imgs_id_test = imgs_id_test.astype('float32')
imgs_id_test = imgs_id_test[..., np.newaxis]
imgs_id_test = imgs_id_test // 255
ev = model.evaluate(imgs_test, imgs_id_test)
dice, iou = ev[1], ev[2]
print("dice score:", dice)
print("iou score:", iou)
plt.plot(history.history['dice_coef'])
plt.plot(history.history['val_dice_coef'])
plt.title('model dice coef')
plt.ylabel('dice coef')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
plt.plot(history.history['iou_coef'])
plt.plot(history.history['val_iou_coef'])
plt.title('model iou coef')
plt.ylabel('iou coef')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()