-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.py
359 lines (294 loc) · 13.1 KB
/
util.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
# NOTE: Some code was borrowed from: https://github.com/ellisdg/3DUnetCNN/blob/master/unet3d/
import cv2
import h5py
import imageio
import keras
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from IPython.display import Image
from keras import backend as K
from keras.engine import Input, Model
from keras.layers import (
Activation,
Conv3D,
Deconvolution3D,
MaxPooling3D,
UpSampling3D,
)
from keras.layers.merge import concatenate
from keras.optimizers import Adam
from keras.utils import to_categorical
from tensorflow.compat.v1.logging import INFO, set_verbosity
set_verbosity(INFO)
K.set_image_data_format("channels_first")
def plot_image_grid(image):
data_all = []
data_all.append(image)
fig, ax = plt.subplots(3, 6, figsize=[16, 9])
# coronal plane
coronal = np.transpose(data_all, [1, 3, 2, 4, 0])
coronal = np.rot90(coronal, 1)
# transversal plane
transversal = np.transpose(data_all, [2, 1, 3, 4, 0])
transversal = np.rot90(transversal, 2)
# sagittal plane
sagittal = np.transpose(data_all, [2, 3, 1, 4, 0])
sagittal = np.rot90(sagittal, 1)
for i in range(6):
n = np.random.randint(coronal.shape[2])
ax[0][i].imshow(np.squeeze(coronal[:, :, n, :]))
ax[0][i].set_xticks([])
ax[0][i].set_yticks([])
if i == 0:
ax[0][i].set_ylabel('Coronal', fontsize=15)
for i in range(6):
n = np.random.randint(transversal.shape[2])
ax[1][i].imshow(np.squeeze(transversal[:, :, n, :]))
ax[1][i].set_xticks([])
ax[1][i].set_yticks([])
if i == 0:
ax[1][i].set_ylabel('Transversal', fontsize=15)
for i in range(6):
n = np.random.randint(sagittal.shape[2])
ax[2][i].imshow(np.squeeze(sagittal[:, :, n, :]))
ax[2][i].set_xticks([])
ax[2][i].set_yticks([])
if i == 0:
ax[2][i].set_ylabel('Sagittal', fontsize=15)
fig.subplots_adjust(wspace=0, hspace=0)
def visualize_data_gif(data_):
images = []
for i in range(data_.shape[0]):
x = data_[min(i, data_.shape[0] - 1), :, :]
y = data_[:, min(i, data_.shape[1] - 1), :]
z = data_[:, :, min(i, data_.shape[2] - 1)]
img = np.concatenate((x, y, z), axis=1)
images.append(img)
imageio.mimsave("/tmp/gif.gif", images, duration=0.01)
return Image(filename="/tmp/gif.gif", format='png')
def visualize_data_gif_patch(data_):
images = []
for i in range(data_.shape[0]):
x = data_[min(i, data_.shape[0] - 1), :, :]
y = data_[:, min(i, data_.shape[1] - 1), :]
z = data_[:, :, min(i, data_.shape[2] - 1)]
dx = x*0
dy = y*0
img = np.concatenate((dx, x, dy, y, dy, z), axis=1)
images.append(img)
imageio.mimsave("/tmp/gif.gif", images, duration=0.08)
return Image(filename="/tmp/gif.gif", format='png')
def create_convolution_block(input_layer, n_filters, batch_normalization=False,
kernel=(3, 3, 3), activation=None,
padding='same', strides=(1, 1, 1),
instance_normalization=False):
"""
:param strides:
:param input_layer:
:param n_filters:
:param batch_normalization:
:param kernel:
:param activation: Keras activation layer to use. (default is 'relu')
:param padding:
:return:
"""
layer = Conv3D(n_filters, kernel, padding=padding, strides=strides)(
input_layer)
if activation is None:
return Activation('relu')(layer)
else:
return activation()(layer)
def get_up_convolution(n_filters, pool_size, kernel_size=(2, 2, 2),
strides=(2, 2, 2),
deconvolution=False):
if deconvolution:
return Deconvolution3D(filters=n_filters, kernel_size=kernel_size,
strides=strides)
else:
return UpSampling3D(size=pool_size)
def unet_model_3d(loss_function, input_shape=(4, 160, 160, 16),
pool_size=(2, 2, 2), n_labels=3,
initial_learning_rate=0.00001,
deconvolution=False, depth=4, n_base_filters=32,
include_label_wise_dice_coefficients=False, metrics=[],
batch_normalization=False, activation_name="sigmoid"):
"""
Builds the 3D UNet Keras model.f
:param metrics: List metrics to be calculated during model training (default is dice coefficient).
:param include_label_wise_dice_coefficients: If True and n_labels is greater than 1, model will report the dice
coefficient for each label as metric.
:param n_base_filters: The number of filters that the first layer in the convolution network will have. Following
layers will contain a multiple of this number. Lowering this number will likely reduce the amount of memory required
to train the model.
:param depth: indicates the depth of the U-shape for the model. The greater the depth, the more max pooling
layers will be added to the model. Lowering the depth may reduce the amount of memory required for training.
:param input_shape: Shape of the input data (n_chanels, x_size, y_size, z_size). The x, y, and z sizes must be
divisible by the pool size to the power of the depth of the UNet, that is pool_size^depth.
:param pool_size: Pool size for the max pooling operations.
:param n_labels: Number of binary labels that the model is learning.
:param initial_learning_rate: Initial learning rate for the model. This will be decayed during training.
:param deconvolution: If set to True, will use transpose convolution(deconvolution) instead of up-sampling. This
increases the amount memory required during training.
:return: Untrained 3D UNet Model
"""
inputs = Input(input_shape)
current_layer = inputs
levels = list()
# add levels with max pooling
for layer_depth in range(depth):
layer1 = create_convolution_block(input_layer=current_layer,
n_filters=n_base_filters * (
2 ** layer_depth),
batch_normalization=batch_normalization)
layer2 = create_convolution_block(input_layer=layer1,
n_filters=n_base_filters * (
2 ** layer_depth) * 2,
batch_normalization=batch_normalization)
if layer_depth < depth - 1:
current_layer = MaxPooling3D(pool_size=pool_size)(layer2)
levels.append([layer1, layer2, current_layer])
else:
current_layer = layer2
levels.append([layer1, layer2])
# add levels with up-convolution or up-sampling
for layer_depth in range(depth - 2, -1, -1):
up_convolution = get_up_convolution(pool_size=pool_size,
deconvolution=deconvolution,
n_filters=
current_layer._keras_shape[1])(
current_layer)
concat = concatenate([up_convolution, levels[layer_depth][1]], axis=1)
current_layer = create_convolution_block(
n_filters=levels[layer_depth][1]._keras_shape[1],
input_layer=concat, batch_normalization=batch_normalization)
current_layer = create_convolution_block(
n_filters=levels[layer_depth][1]._keras_shape[1],
input_layer=current_layer,
batch_normalization=batch_normalization)
final_convolution = Conv3D(n_labels, (1, 1, 1))(current_layer)
act = Activation(activation_name)(final_convolution)
model = Model(inputs=inputs, outputs=act)
if not isinstance(metrics, list):
metrics = [metrics]
model.compile(optimizer=Adam(lr=initial_learning_rate), loss=loss_function,
metrics=metrics)
return model
def visualize_patch(X, y):
fig, ax = plt.subplots(1, 2, figsize=[10, 5], squeeze=False)
ax[0][0].imshow(X[:, :, 0], cmap='Greys_r')
ax[0][0].set_yticks([])
ax[0][0].set_xticks([])
ax[0][1].imshow(y[:, :, 0], cmap='Greys_r')
ax[0][1].set_xticks([])
ax[0][1].set_yticks([])
fig.subplots_adjust(wspace=0, hspace=0)
class VolumeDataGenerator(keras.utils.Sequence):
def __init__(self,
sample_list,
base_dir,
batch_size=1,
shuffle=True,
dim=(160, 160, 16),
num_channels=4,
num_classes=3,
verbose=1):
self.batch_size = batch_size
self.shuffle = shuffle
self.base_dir = base_dir
self.dim = dim
self.num_channels = num_channels
self.num_classes = num_classes
self.verbose = verbose
self.sample_list = sample_list
self.on_epoch_end()
def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(len(self.sample_list))
if self.shuffle == True:
np.random.shuffle(self.indexes)
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.floor(len(self.sample_list) / self.batch_size))
def __data_generation(self, list_IDs_temp):
'Generates data containing batch_size samples'
# Initialization
X = np.zeros((self.batch_size, self.num_channels, *self.dim),
dtype=np.float64)
y = np.zeros((self.batch_size, self.num_classes, *self.dim),
dtype=np.float64)
# Generate data
for i, ID in enumerate(list_IDs_temp):
# Store sample
if self.verbose == 1:
print("Training on: %s" % self.base_dir + ID)
with h5py.File(self.base_dir + ID, 'r') as f:
X[i] = np.array(f.get("x"))
# remove the background class
y[i] = np.moveaxis(np.array(f.get("y")), 3, 0)[1:]
return X, y
def __getitem__(self, index):
'Generate one batch of data'
# Generate indexes of the batch
indexes = self.indexes[
index * self.batch_size: (index + 1) * self.batch_size]
# Find list of IDs
sample_list_temp = [self.sample_list[k] for k in indexes]
# Generate data
X, y = self.__data_generation(sample_list_temp)
return X, y
def get_labeled_image(image, label, is_categorical=False):
if not is_categorical:
label = to_categorical(label, num_classes=4).astype(np.uint8)
image = cv2.normalize(image[:, :, :, 0], None, alpha=0, beta=255,
norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F).astype(
np.uint8)
labeled_image = np.zeros_like(label[:, :, :, 1:])
# remove tumor part from image
labeled_image[:, :, :, 0] = image * (label[:, :, :, 0])
labeled_image[:, :, :, 1] = image * (label[:, :, :, 0])
labeled_image[:, :, :, 2] = image * (label[:, :, :, 0])
# color labels
labeled_image += label[:, :, :, 1:] * 255
return labeled_image
def predict_and_viz(image, label, model, threshold, loc=(100, 100, 50)):
image_labeled = get_labeled_image(image.copy(), label.copy())
model_label = np.zeros([3, 320, 320, 160])
for x in range(0, image.shape[0], 160):
for y in range(0, image.shape[1], 160):
for z in range(0, image.shape[2], 16):
patch = np.zeros([4, 160, 160, 16])
p = np.moveaxis(image[x: x + 160, y: y + 160, z:z + 16], 3, 0)
patch[:, 0:p.shape[1], 0:p.shape[2], 0:p.shape[3]] = p
pred = model.predict(np.expand_dims(patch, 0))
model_label[:, x:x + p.shape[1],
y:y + p.shape[2],
z: z + p.shape[3]] += pred[0][:, :p.shape[1], :p.shape[2],
:p.shape[3]]
model_label = np.moveaxis(model_label[:, 0:240, 0:240, 0:155], 0, 3)
model_label_reformatted = np.zeros((240, 240, 155, 4))
model_label_reformatted = to_categorical(label, num_classes=4).astype(
np.uint8)
model_label_reformatted[:, :, :, 1:4] = model_label
model_labeled_image = get_labeled_image(image, model_label_reformatted,
is_categorical=True)
fig, ax = plt.subplots(2, 3, figsize=[10, 7])
# plane values
x, y, z = loc
ax[0][0].imshow(np.rot90(image_labeled[x, :, :, :]))
ax[0][0].set_ylabel('Ground Truth', fontsize=15)
ax[0][0].set_xlabel('Sagital', fontsize=15)
ax[0][1].imshow(np.rot90(image_labeled[:, y, :, :]))
ax[0][1].set_xlabel('Coronal', fontsize=15)
ax[0][2].imshow(np.squeeze(image_labeled[:, :, z, :]))
ax[0][2].set_xlabel('Transversal', fontsize=15)
ax[1][0].imshow(np.rot90(model_labeled_image[x, :, :, :]))
ax[1][0].set_ylabel('Prediction', fontsize=15)
ax[1][1].imshow(np.rot90(model_labeled_image[:, y, :, :]))
ax[1][2].imshow(model_labeled_image[:, :, z, :])
fig.subplots_adjust(wspace=0, hspace=.12)
for i in range(2):
for j in range(3):
ax[i][j].set_xticks([])
ax[i][j].set_yticks([])
return model_label_reformatted