[7c5f70]: / Crawler / crawler_segmentation.py

Download this file

402 lines (292 with data), 12.4 kB

  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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import os
from glob2 import glob
import nibabel as nib
import numpy as np
import keras
import tensorflow as tf
import itertools
from time import time
import pandas as pd
from pylab import rcParams
import matplotlib.pyplot as plt
import seaborn as sns
from PIL import Image
from skimage import color, measure
# Set up plotting properties
sns.set(style='ticks', palette='Spectral', font_scale=1.5)
rcParams['figure.figsize'] = 6, 4
def load_data(filenames, block_size, oversamp, lab_trun, adptive_hist=False):
X = np.empty(shape=(0, 0, 0, 0, 0))
file = filenames
tmpx = load_data_3D(file)
sz = tmpx.shape
# tmpy = load_label_3D(file[-1], sz)
tmpy = np.zeros(shape=(sz[0], sz[1], sz[2], 1))
# If the image dimensions of the label and inputs match proceed
if tmpx.shape[:3] == tmpy.shape[:3]:
# Crop the data
# mask = np.zeros(shape=(sz[1]), dtype=bool)
# mask[30:-30] = True
# tmpx = tmpx[:, :, mask, :]
# tmpy = tmpy[:, :, mask, :]
orig_size = tmpx.shape
# Make patches
tmpx, tmpy = make_train_3D(tmpx, tmpy, block_size=block_size,
oversamp=oversamp,
lab_trun=lab_trun)
try:
X = np.concatenate((X, tmpx), axis=0)
# Y = np.concatenate((Y, tmpy), axis=0)
except ValueError:
X = tmpx
# Y = tmpy
return X, orig_size
def load_data_3D(files):
images = np.array([nib.load(file).get_data().astype('float32').squeeze() for file in files])
images = np.swapaxes(images, 0, 3)
# Normalize images
sz = images.shape
for z in range(sz[3]):
mn = images[:, :, :, z].mean()
std = images[:, :, :, z].std()
images[:, :, :, z] -= mn
images[:, :, :, z] /= std
return images
def make_train_3D(X, Y, block_size, oversamp=1, lab_trun=4):
print('Making test volume patches')
sz = X.shape # z, x, y, channels, vols
if len(sz) == 4:
sz = sz + (1,)
X = X[:, :, :, :, np.newaxis]
Y = Y[:, :, :, :, np.newaxis]
# Number of volumes in each dimension
num_vols = [int((sz[i] * oversamp) // (block_size[i] - lab_trun) + 1) for i in range(len(block_size))]
# Get starting indices of each block (zind[0}:zind[0] + block_size[0])
zind = np.linspace(0, sz[0] - block_size[0], num_vols[0], dtype=int)
xind = np.linspace(0, sz[1] - block_size[1], num_vols[1], dtype=int)
yind = np.linspace(0, sz[2] - block_size[2], num_vols[2], dtype=int)
# Preallocate volumes - samples, block, block, block, channels
in_patches = np.zeros(shape=(np.product(num_vols) * sz[4], block_size[0], block_size[1], block_size[2], sz[3]))
lab_patches = np.zeros(shape=(np.product(num_vols) * sz[4], block_size[0] - lab_trun, block_size[1] - lab_trun, block_size[2] - lab_trun, 1))
# Make volumes
n = 0
for vol in range(sz[4]):
for z in itertools.product(zind, xind, yind):
# print(z)
in_patches[n, :, :, :, :] = X[z[0]:z[0] + block_size[0], z[1]:z[1] + block_size[1], z[2]:z[2] + block_size[2], :, vol]
lab_patches[n, :, :, :, :] = Y[z[0] + lab_trun//2:z[0] + block_size[0] - lab_trun//2, z[1] + lab_trun//2:z[1] + block_size[1] - lab_trun//2, z[2] + lab_trun//2:z[2] + block_size[2] - lab_trun//2, :, vol]
n += 1
return in_patches, lab_patches
def load_models(path):
"""
Loads a list of models
Args:
paths (list): list of paths to models (not including the filename)
Returns:
"""
model_name = os.path.join(path, 'Trained_model.h5')
model = keras.models.load_model(model_name,
custom_objects=
{'dice_loss': dice_loss,
'dice_metric': dice_metric})
return model
def seg_from_model(model_path, im_paths, threshold):
"""
Args:
model_path (str): path to trained model (path only)
im_paths (list of lists of 3 strings): paths to three contrast images
Returns:
"""
# Set up data constants
block_size = [18, 142, 142]
oversamp_test = 2.0
lab_trun = 2
# Load models
model = load_models(model_path)
# Load data
x, orig_size = load_data(im_paths, block_size, oversamp_test, lab_trun)
# Make predictions
t = time()
with tf.device('GPU:1'):
y_pred = model.predict(x, batch_size=20)
print('Time to run segmentations: %0.3f seconds' % (time() - t))
# Reconstruct images
x, y_pred = recon_test_3D(X=x, Y=y_pred, orig_size=orig_size, block_size=block_size, oversamp=oversamp_test,
lab_trun=lab_trun)
# Swap axes
y_pred = np.rollaxis(y_pred, 0, 2).swapaxes(1, 2)
x = np.rollaxis(x, 0, 2).swapaxes(1, 2)
# Threshold segmentation
y_thresh = y_pred > threshold
# Remove all but the tumor label
y_thresh = remove_extra_labels(y_thresh)
return x[:, :, :, -1, -1], y_thresh.astype(np.single)
def recon_test_3D(X, Y, orig_size, block_size, oversamp=1, lab_trun=4):
print('Reconstructing test volume from patches')
# Add volume dimension if it does not exist
if len(orig_size) == 4:
orig_size = orig_size + (1,)
# Add volume axis if it does not exist
sz = X.shape
if len(sz) < 6:
X = X[:, :, :, :, :, np.newaxis]
Y = Y[:, :, :, :, :, np.newaxis]
# Number of volumes in each dimension
num_vols = [int((orig_size[i] * oversamp) // (block_size[i] - lab_trun) + 1) for i in range(len(block_size))]
# Get starting indices of each block (zind[0}:zind[0] + block_size[0])
zind = np.linspace(0, orig_size[0] - block_size[0], num_vols[0], dtype=int)
xind = np.linspace(0, orig_size[1] - block_size[1], num_vols[1], dtype=int)
yind = np.linspace(0, orig_size[2] - block_size[2], num_vols[2], dtype=int)
# Preallocate arrays - z, x, y, channels, vols
in_recon = np.zeros(shape=(orig_size))
lab_recon = np.zeros(shape=(orig_size[0], orig_size[1], orig_size[2], 1, 1))
inds_in = np.zeros_like(in_recon, dtype=np.int8)
inds_lab = np.zeros_like(lab_recon, dtype=np.int8)
# Reconstruct images
print('orig_size', orig_size)
for vol in range(orig_size[4]):
n = 0
for z in itertools.product(zind, xind, yind):
# print(z)
# Update images
in_recon[z[0]:z[0] + block_size[0], z[1]:z[1] + block_size[1], z[2]:z[2] + block_size[2], :, vol] += X[n, :, :, :, :, vol]
lab_recon[z[0] + lab_trun//2:z[0] + block_size[0] - lab_trun//2, z[1] + lab_trun//2:z[1] + block_size[1] - lab_trun//2, z[2] + lab_trun//2:z[2] + block_size[2] - lab_trun//2, :, vol] += Y[n, :, :, :, :, vol]
# Keep track of duplicate values
inds_in[z[0]:z[0] + block_size[0], z[1]:z[1] + block_size[1], z[2]:z[2] + block_size[2], :, vol] += 1
inds_lab[z[0] + lab_trun//2:z[0] + block_size[0] - lab_trun//2, z[1] + lab_trun//2:z[1] + block_size[1] - lab_trun//2, z[2] + lab_trun//2:z[2] + block_size[2] - lab_trun//2, :, vol] += 1
n += 1
in_recon /= inds_in
lab_recon[inds_lab > 0] /= inds_lab[inds_lab > 0]
return in_recon, lab_recon
def dice_loss(y_true, y_pred):
threshold = 0.5
smooth = 1e-5
mask = y_pred > threshold
mask = tf.cast(mask, dtype=tf.float32)
y_pred = tf.multiply(y_pred, mask)
mask = y_true > threshold
mask = tf.cast(mask, dtype=tf.float32)
y_true = tf.multiply(y_true, mask)
# y_pred = tf.cast(y_pred > threshold, dtype=tf.float32)
# y_true = tf.cast(y_true > threshold, dtype=tf.float32)
inse = tf.reduce_sum(tf.multiply(y_pred, y_true))
l = tf.reduce_sum(y_pred)
r = tf.reduce_sum(y_true)
# new haodong
hard_dice = (2. * inse + smooth) / (l + r + smooth)
hard_dice = 1 - tf.reduce_mean(hard_dice)
return hard_dice
def dice_metric(y_true, y_pred):
threshold = 0.5
mask = y_pred > threshold
mask = tf.cast(mask, dtype=tf.float32)
y_pred = tf.multiply(y_pred, mask)
mask = y_true > threshold
mask = tf.cast(mask, dtype=tf.float32)
y_true = tf.multiply(y_true, mask)
# y_pred = tf.cast(y_pred > threshold, dtype=tf.float32)
# y_true = tf.cast(y_true > threshold, dtype=tf.float32)
inse = tf.reduce_sum(tf.multiply(y_pred, y_true))
l = tf.reduce_sum(y_pred)
r = tf.reduce_sum(y_true)
# new haodong
hard_dice = (2. * inse) / (l + r)
hard_dice = tf.reduce_mean(hard_dice)
return hard_dice
def display_segmentations(t2, y_pred, save_path, sname='segs.png'):
"""
Saves segmentations as an overlayed montage.
Args:
t2 (3D numpy array): T2 image
y_pred (3D numpy array): Binary segmentation
save_path (str): directory in which to save images
Returns:
"""
# Make mask see-through
y_mask = y_pred.squeeze()
# Set up slices to plot
ims = 4
slices = range(0, y_pred.shape[2], ims)
rows = 2
cols = len(slices) // rows
resh = (y_pred.shape[0] * rows, y_pred.shape[1] * cols)
t2_im = np.zeros(shape=(resh))
y_mask_im = np.zeros_like(t2_im)
r = [0, y_pred.shape[0]]
c = [0, y_pred.shape[0]]
n = 0
for i in range(rows):
c = [0, y_pred.shape[0]]
for ii in range(cols):
t2_im[r[0]:r[1], c[0]:c[1]] = t2[:, :, slices[n]].T
y_mask_im[r[0]:r[1], c[0]:c[1]] = y_mask[:, :, slices[n]].T
n += 1
c = [i + y_pred.shape[0] for i in c]
r = [i + y_pred.shape[0] for i in r]
# Mask out background
y_mask_im = np.ma.masked_where(y_mask_im.astype(bool) == 0, y_mask_im.astype(bool))
# Save images
# https://stackoverflow.com/questions/9193603/applying-a-coloured-overlay-to-an-image-in-either-pil-or-imagemagik
# Convert images to RGB
t2_im_rep = np.dstack((t2_im, t2_im, t2_im))
y_mask_im_rep = np.zeros_like(t2_im_rep)
y_mask_im_rep[:, :, 1] = y_mask_im
# Convert the input image and color mask to Hue Saturation Value (HSV)
# colorspace
y_mask_im_hsv = color.rgb2hsv(y_mask_im_rep)
t2_im_hsv = color.rgb2hsv(t2_im_rep)
# Replace the hue and saturation of the original image
# with that of the color mask
alpha = 0.6
t2_im_hsv[:, :, 0] = y_mask_im_hsv[:, :, 0]
t2_im_hsv[:, :, 1] = y_mask_im_hsv[:, :, 1] * alpha;
# Convert bach to RGB
im_masked = color.hsv2rgb(t2_im_hsv)
# Convert image to 8-bit
im_masked -= im_masked.min()
im_masked /= im_masked.max() * 0.8
im_masked *= 255
im_masked = im_masked.astype(np.uint8)
# Save as image using PIL
im = Image.fromarray(im_masked)
sname = os.path.join(save_path, sname)
im.save(sname, 'png')
def remove_extra_labels(y_pred):
"""
Finds the largest continous region in the tumor label. All other regions are discarded.
Args:
y_pred (3D numpy array): thresholded network ouput
Returns:
(3D numpy array): returned segmentation
"""
# Convert predictions to integer mask
y_mask = y_pred.astype(np.uint8).squeeze()
# Find continous regions
labels = measure.label(y_mask, connectivity=3)
# Find the number of counts for each region
vals, counts = np.unique(labels, return_counts=True)
# Remove background
bg = labels[0, 0, 0]
counts = counts[vals != bg]
vals = vals[vals != bg]
# Get the largest labels
ind = np.argmax(counts)
tumor_val = vals[ind]
# Return clean predictions
y_out = (labels == tumor_val).astype(np.float)
return y_out
if __name__ == '__main__':
# No skip network
paths = ['E:/MR Data/ML_Results/2019_01_17_20-26-27_onlyT2_lr2e-4_1000ep',
'E:/MR Data/ML_Results/2019_01_18_08-58-04_all_contrasts_lr2e-4_1000ep'
]
spath = 'E:/MR Data/ML_Results/SPIE/NoSkip'
thresholds = [0.958, 0.812]
seg_from_model(paths, spath, thresholds)
# Skip network
paths = ['W:/Matt/ML_Sarcoma_Results/2019_01_21_16-38-29_skip_onlyT2_lr2e-4_400ep',
'W:/Matt/ML_Sarcoma_Results/2019_01_22_01-33-59_skip_all_contrasts_lr2e-4_400ep'
]
spath = 'E:/MR Data/ML_Results/SPIE/Skip'
thresholds = [0.958, 0.812]
seg_from_model(paths, spath, thresholds)