-
Notifications
You must be signed in to change notification settings - Fork 0
/
UNet_model.py
360 lines (276 loc) · 12.7 KB
/
UNet_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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 18 12:54:22 2022
@author: idu
"""
## UNet Model for segmentation
import os, glob
import numpy as np
import matplotlib.pyplot as plt
import cv2
import nibabel as nib
import tensorflow as tf
from tensorflow import keras
from skimage import morphology
from skimage.feature import canny
#from scipy import ndimage as ndi
from skimage import io
from skimage.exposure import histogram
from PIL import Image as im
import skimage
from skimage.filters import sobel
# Mounting my google drive#
#from google.colab import drive
import os
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow import keras
from PIL import Image
##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Stage 1 : UNET MODEL BUILDING #########
##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
"""# Slicing and Saving"""
## The path here include the image volumes (308MB) and the lung masks (1MB) were extracted from the COVID-19 CT segmentation dataset
dataInputPath = '/home/idu/Desktop/COV19D/segmentation/volumes'
imagePathInput = os.path.join(dataInputPath, 'img/') ## Image volumes were exctracted to this subfolder
maskPathInput = os.path.join(dataInputPath, 'mask/') ## lung masks were exctracted to this subfolder
# Preparing the outputpath for slicing the CT volume from the above data
dataOutputPath = '/home/idu/Desktop/COV19D/segmentation/slices/'
imageSliceOutput = os.path.join(dataOutputPath, 'img/') ## Image volume slices will be placed here
maskSliceOutput = os.path.join(dataOutputPath, 'mask/') ## Annotated masks slices will be placed here
# Slicing only in Z direction
# Slices in Z direction shows the required lung area
SLICE_X = False
SLICE_Y = False
SLICE_Z = True
SLICE_DECIMATE_IDENTIFIER = 3
# Choosing normalization boundaries suitable from the chosen images
HOUNSFIELD_MIN = -1020
HOUNSFIELD_MAX = 2995
HOUNSFIELD_RANGE = HOUNSFIELD_MAX - HOUNSFIELD_MIN
# Normalizing the images
def normalizeImageIntensityRange (img):
img[img < HOUNSFIELD_MIN] = HOUNSFIELD_MIN
img[img > HOUNSFIELD_MAX] = HOUNSFIELD_MAX
return (img - HOUNSFIELD_MIN) / HOUNSFIELD_RANGE
#nImg = normalizeImageIntensityRange(img)
#np.min(nImg), np.max(nImg), nImg.shape, type(nImg)
# Reading image or mask volume
def readImageVolume(imgPath, normalize=True):
img = nib.load(imgPath).get_fdata()
if normalize:
return normalizeImageIntensityRange(img)
else:
return img
#readImageVolume(imgPath, normalize=False)
#readImageVolume(maskPath, normalize=False)
# Slicing image in all directions and save
def sliceAndSaveVolumeImage(vol, fname, path):
(dimx, dimy, dimz) = vol.shape
print(dimx, dimy, dimz)
cnt = 0
if SLICE_X:
cnt += dimx
print('Slicing X: ')
for i in range(dimx):
saveSlice(vol[i,:,:], fname+f'-slice{str(i).zfill(SLICE_DECIMATE_IDENTIFIER)}_x', path)
if SLICE_Y:
cnt += dimy
print('Slicing Y: ')
for i in range(dimy):
saveSlice(vol[:,i,:], fname+f'-slice{str(i).zfill(SLICE_DECIMATE_IDENTIFIER)}_y', path)
if SLICE_Z:
cnt += dimz
print('Slicing Z: ')
for i in range(dimz):
saveSlice(vol[:,:,i], fname+f'-slice{str(i).zfill(SLICE_DECIMATE_IDENTIFIER)}_z', path)
return cnt
# Saving volume slices to file
def saveSlice (img, fname, path):
img = np.uint8(img * 255)
fout = os.path.join(path, f'{fname}.png')
cv2.imwrite(fout, img)
print(f'[+] Slice saved: {fout}', end='\r')
# Reading and processing image volumes for TEST images
for index, filename in enumerate(sorted(glob.iglob(imagePathInput+'*.nii.gz'))):
img = readImageVolume(filename, True)
print(filename, img.shape, np.sum(img.shape), np.min(img), np.max(img))
numOfSlices = sliceAndSaveVolumeImage(img, 't'+str(index), imageSliceOutput)
print(f'\n{filename}, {numOfSlices} slices created \n')
# Reading and processing image mask volumes for TEST masks
for index, filename in enumerate(sorted(glob.iglob(maskPathInput+'*.nii.gz'))):
img = readImageVolume(filename, False)
print(filename, img.shape, np.sum(img.shape), np.min(img), np.max(img))
numOfSlices = sliceAndSaveVolumeImage(img, 't'+str(index), maskSliceOutput)
print(f'\n{filename}, {numOfSlices} slices created \n')
# Exploring the data
imgPath = os.path.join(imagePathInput, '1.nii.gz')
img = nib.load(imgPath).get_fdata()
np.min(img), np.max(img), img.shape, type(img)
maskPath = os.path.join(maskPathInput, '1.nii.gz')
mask = nib.load(maskPath).get_fdata()
np.min(mask), np.max(mask), mask.shape, type(mask)
# Showing Mask slice
imgSlice = mask[:,:,20]
plt.imshow(imgSlice, cmap='gray')
plt.show()
# Showing Corresponding Image slice
imgSlice = img[:,:,20]
plt.imshow(imgSlice, cmap='gray')
plt.show()
"""# Training and testing Generator"""
# Define constants
SEED = 42
### Setting the training and testing dataset for validation of the proposed VGG16-UNET model
### All t0&t1 Z-sliced slices (images and masks) were used for testing
BATCH_SIZE_TRAIN = 32
BATCH_SIZE_TEST = 32
IMAGE_HEIGHT = 224
IMAGE_WIDTH = 224
SIZE = IMAGE_HEIGHT = IMAGE_HEIGHT
IMG_SIZE = (IMAGE_HEIGHT, IMAGE_WIDTH)
#### Splitting the data into training and test sets happen manually
### t0 volumes and masks were chosen as test sets
data_dir = '/home/idu/Desktop/COV19D/segmentation/slices/'
data_dir_train = os.path.join(data_dir, 'training')
# The images should be stored under: "data/slices/training/img/img"
data_dir_train_image = os.path.join(data_dir_train, 'img')
# The images should be stored under: "data/slices/training/mask/img"
data_dir_train_mask = os.path.join(data_dir_train, 'mask')
data_dir_test = os.path.join(data_dir, 'test')
# The images should be stored under: "data/slices/test/img/img"
data_dir_test_image = os.path.join(data_dir_test, 'img')
# The images should be stored under: "data/slices/test/mask/img"
data_dir_test_mask = os.path.join(data_dir_test, 'mask')
def create_segmentation_generator_train(img_path, msk_path, BATCH_SIZE):
data_gen_args = dict(rescale=1./255,
featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=90,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.3
)
datagen = ImageDataGenerator(**data_gen_args)
img_generator = datagen.flow_from_directory(img_path, target_size=IMG_SIZE, class_mode=None, color_mode='grayscale', batch_size=BATCH_SIZE, seed=SEED)
msk_generator = datagen.flow_from_directory(msk_path, target_size=IMG_SIZE, class_mode=None, color_mode='grayscale', batch_size=BATCH_SIZE, seed=SEED)
return zip(img_generator, msk_generator)
# Remember not to perform any image augmentation in the test generator!
def create_segmentation_generator_test(img_path, msk_path, BATCH_SIZE):
data_gen_args = dict(rescale=1./255)
datagen = ImageDataGenerator(**data_gen_args)
img_generator = datagen.flow_from_directory(img_path, target_size=IMG_SIZE, class_mode=None, color_mode='grayscale', batch_size=BATCH_SIZE, seed=SEED)
msk_generator = datagen.flow_from_directory(msk_path, target_size=IMG_SIZE, class_mode=None, color_mode='grayscale', batch_size=BATCH_SIZE, seed=SEED)
return zip(img_generator, msk_generator)
train_generator = create_segmentation_generator_train(data_dir_train_image, data_dir_train_mask, BATCH_SIZE_TRAIN)
test_generator = create_segmentation_generator_test(data_dir_test_image, data_dir_test_mask, BATCH_SIZE_TEST)
NUM_TRAIN = 745
NUM_TEST = 84
NUM_OF_EPOCHS = 20
def display(display_list):
plt.figure(figsize=(15,15))
title = ['Input Image', 'True Mask', 'Predicted Mask']
for i in range(len(display_list)):
plt.subplot(1, len(display_list), i+1)
plt.title(title[i])
plt.imshow(tf.keras.preprocessing.image.array_to_img(display_list[i]), cmap='gray')
plt.show()
def show_dataset(datagen, num=1):
for i in range(0,num):
image,mask = next(datagen)
display([image[0], mask[0]])
show_dataset(train_generator, 4)
EPOCH_STEP_TRAIN = 6*NUM_TRAIN // BATCH_SIZE_TRAIN
EPOCH_STEP_TEST = NUM_TEST // BATCH_SIZE_TEST
"""# UNet Model"""
from tensorflow.keras.layers import Conv2D, Dropout, BatchNormalization, Activation, MaxPool2D, Conv2DTranspose, Concatenate, Input
from tensorflow.keras.models import Model
from tensorflow.keras.applications import VGG16
SIZE = 224
IMAGE_HEIGHT = SIZE
IMAGE_WIDTH = SIZE
#### UNET NETWORK
# Building 2D-UNET model
def unet(n_levels, initial_features=32, n_blocks=2, kernel_size=3, pooling_size=2, in_channels=1, out_channels=1):
inputs = keras.layers.Input(shape=(IMAGE_HEIGHT, IMAGE_WIDTH, in_channels))
x = inputs
convpars = dict(kernel_size=kernel_size, activation='relu', padding='same')
#downstream
skips = {}
for level in range(n_levels):
for _ in range(n_blocks):
x = keras.layers.Conv2D(initial_features * 2 ** level, **convpars)(x)
if level < n_levels - 1:
skips[level] = x
x = keras.layers.MaxPool2D(pooling_size)(x)
# upstream
for level in reversed(range(n_levels-1)):
x = keras.layers.Conv2DTranspose(initial_features * 2 ** level, strides=pooling_size, **convpars)(x)
x = keras.layers.Concatenate()([x, skips[level]])
for _ in range(n_blocks):
x = keras.layers.Conv2D(initial_features * 2 ** level, **convpars)(x)
# output
activation = 'sigmoid' if out_channels == 1 else 'softmax'
x = keras.layers.Conv2D(out_channels, kernel_size=1, activation=activation, padding='same')(x)
return keras.Model(inputs=[inputs], outputs=[x], name=f'UNET-L{n_levels}-F{initial_features}')
UNet_model = unet(3)
UNet_model.summary()
UNet_model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=[#tf.keras.metrics.Precision(), tf.keras.metrics.Recall(),
tf.keras.metrics.MeanIoU(num_classes = 2),
'accuracy'])
UNet_model.fit_generator(generator=train_generator,
steps_per_epoch=EPOCH_STEP_TRAIN,
validation_data=test_generator,
validation_steps=EPOCH_STEP_TEST,
epochs=NUM_OF_EPOCHS)
#Saving the model
UNet_model = keras.models.load_model('/home/idu/Desktop/COV19D/segmentation/UNet_model.h5')
# Displaying images
def display(display_list):
title = ['Input Image', 'True Mask', 'Predicted Mask']
for i in range(len(display_list)):
plt.subplot(1, len(display_list), i+1)
plt.title(title[i])
plt.imshow(tf.keras.preprocessing.image.array_to_img(display_list[i]), cmap='gray')
plt.show()
def show_dataset(datagen, num=1):
for i in range(0, num):
image,mask = next(datagen)
display((image[0], mask[0]))
from tensorflow.keras.metrics import MeanIoU
path = '/home/idu/Desktop/COV19D/segmentation/Segmentation Results/'
def show_prediction(datagen, num=1):
for i in range(0,num):
image,mask = next(datagen)
pred_mask = UNet_model.predict(image)[0] > 0.5
display([image[0], mask[0], pred_mask])
num_classes = 2
IOU_keras = MeanIoU(num_classes=num_classes)
IOU_keras.update_state(mask[0], pred_mask)
print("Mean IoU =", IOU_keras.result().numpy())
values = np.array(IOU_keras.get_weights()).reshape(num_classes, num_classes)
print(values)
show_prediction(test_generator, 12)
results = UNet_model.evaluate(test_generator, batch_size=32)
print("test loss, test acc:", results)
## Segmenting images based on k-means clustering and exctracting lung regions and saving them
#in the same directory as the original images (in .png format)
path = '/home/idu/Desktop/COV19D/segmentation/Segmentation Results/'
def show_prediction(datagen, num=1):
for i in range(0,num):
image,mask = next(datagen)
pred_mask = UNet_model.predict(image)[0] > 0.5
display([image[0], mask[0], pred_mask])
num_classes = 2
IOU_keras = MeanIoU(num_classes=num_classes)
IOU_keras.update_state(mask[0], pred_mask)
print("Mean IoU =", IOU_keras.result().numpy())
values = np.array(IOU_keras.get_weights()).reshape(num_classes, num_classes)
print(values)
show_prediction(test_generator, 12)
### Finish First Stage ---- KENAN MORANI