-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_processing_2d_classification.py
389 lines (263 loc) · 14.8 KB
/
data_processing_2d_classification.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
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
import numpy as np
import tensorflow as tf
from collections import defaultdict
import random
from sklearn import preprocessing
import os
def apply_mask(array, mask):
##### array --- (dim1, dim2)
##### mask --- (dim1, dim2)
##### masks the array so that the non-ROI part is zero ####
array[mask < 1.0] = 0.0
return array
def one_hot_encoder(input,dim_output,list_values):
dictionar=defaultdict()
for value,control in zip(list_values,np.arange(dim_output)):
dictionar[value] = control
object = np.zeros(shape=(input.shape[0],dim_output))
for i in range(input.shape[0]):
object[i,dictionar[int(input[i,0])]] = 1.0
return object
def blockshaped(arr, nrows, ncols):
############################################################
#### Used to get non-overlapping patches from an image #####
############################################################
"""
Return an array of shape (n, nrows, ncols) where
n * nrows * ncols = arr.size
If arr is a 2D array, the returned array should look like n subblocks with
each subblock preserving the "physical" layout of arr.
"""
h, w = arr.shape
return (arr.reshape(h//nrows, nrows, -1, ncols)
.swapaxes(1,2)
.reshape(-1, nrows, ncols))
def unblockshaped(arr, h, w):
"""
Return an array of shape (h, w) where
h * w = arr.size
If arr is of shape (n, nrows, ncols), n sublocks of shape (nrows, ncols),
then the returned array preserves the "physical" layout of the sublocks.
"""
n, nrows, ncols = arr.shape
return (arr.reshape(h//nrows, -1, nrows, ncols)
.swapaxes(1,2)
.reshape(h, w))
def output_transformation(inputul):
inputul = np.round(inputul)
return inputul
def crop_2D_block(image, central_points, semi_block_size1, semi_block_size2):
#### basically crops a small 2D cube from a bigger 2D object ####
### image -- shape (height, width, channels)
### central_points -- (c1,c2)
### semi_block_size -- (l1,l2)
plm = image[central_points[0]-semi_block_size1:central_points[0]+semi_block_size2,
central_points[1]-semi_block_size1:central_points[1]+semi_block_size2,:]
#print(plm.shape)
return plm
def check_and_add_zero_padding_2d_image(input_image, output_image, central_points, semi_block_size1, semi_block_size2):
#### checks if extracting a patch need padding or not
#### accounts for the case where the central_points are close to the boundary of the image and expands it with the minimum of the image
### image -- shape (height, width, channels)
### central_points -- (c1,c2)
### semi_block_size -- (l1,l2)
current_shape = input_image.shape
min_value_image = np.min(input_image)
padding_dimensions = []
control=0
for _ in range(2):
dim_list = []
if central_points[_]-semi_block_size1 < 0:
dim_list.append(np.abs(central_points[_]-semi_block_size1))
control+=1
else:
dim_list.append(0)
if central_points[_]+semi_block_size2 > current_shape[_]:
dim_list.append(np.abs(central_points[_]+semi_block_size2 - current_shape[_]))
control+=1
else:
dim_list.append(0)
padding_dimensions.append(tuple(dim_list))
if control > 0:
padding_dimensions = tuple(padding_dimensions)
padding_dimensions_extra = list(padding_dimensions)
padding_dimensions_extra.append(tuple([0,0]))
padding_dimensions_extra = tuple(padding_dimensions_extra)
#print(padding_dimensions_extra)
input_image = np.pad(input_image, padding_dimensions_extra, mode='constant', constant_values = min_value_image)
output_image = np.pad(output_image, padding_dimensions_extra, mode='constant')
central_points = [central_points[_]+padding_dimensions[_][0] for _ in range(2)]
return input_image, output_image, central_points
def extract_2d_blocks_training(inputul, outputul, iteration, block_size_input, block_size_output, dim_output):
## inputul -- shape (num_batch, width, height, num_imaging_modalities)
## outputul -- shape (num_batch, width, height, num_imaging_modalitie)
#### this will extract 4 training examples ####
lista = np.arange(inputul.shape[0])
np.random.seed(iteration)
np.random.shuffle(lista)
current_index = lista[:2]
semi_block_size_input = int(block_size_input//2)
semi_block_size_input2 = block_size_input - semi_block_size_input
semi_block_size_output = int(block_size_output//2)
semi_block_size_output2 = block_size_output - semi_block_size_output
list_blocks_input = []
list_blocks_segmentation = []
for _ in current_index:
##### iterating over 2D images #####
### pad current input and output scan to avoid problems ####
current_input = inputul[_,...]
current_output = outputul[_,...]
#### shape of current scan ####
current_shape = inputul[_,...].shape
#################################################################################################################
#### random places being extracted -- most likely not containing any segmentation besides background class ######
#################################################################################################################
list_of_random_places1 = random.sample(range(semi_block_size_output, current_shape[0]-semi_block_size_output2), 2)
list_of_random_places2 = random.sample(range(semi_block_size_output, current_shape[1]-semi_block_size_output2), 2)
for __ in range(2):
#### iterate over the 2 locations of the 3D cubes #####
central_points = [list_of_random_places1[__], list_of_random_places2[__]]
current_input_padded, current_output_padded, central_points = check_and_add_zero_padding_2d_image(current_input,
current_output, central_points, semi_block_size_input, semi_block_size_input2)
list_blocks_segmentation.append(crop_2D_block(current_output_padded, central_points, semi_block_size_output, semi_block_size_output2))
list_blocks_input.append(crop_2D_block(current_input_padded, central_points, semi_block_size_input, semi_block_size_input2))
###############################################################################################
##### specifically extract 2D patches with a non-background class #############################
###############################################################################################
#########################
##### Class number 1 ####
#########################
indices_tumor = np.where(current_output[...,0] == 1.0)
indices_tumor_dim1 = indices_tumor[0]
indices_tumor_dim2 = indices_tumor[1]
if len(indices_tumor_dim1)==0:
print('tumor not found')
else:
list_of_random_places = random.sample(range(0,len(indices_tumor_dim1)), 2)
for __ in range(2):
central_points = [indices_tumor_dim1[list_of_random_places[__]],
indices_tumor_dim2[list_of_random_places[__]]]
current_input_padded, current_output_padded, central_points = check_and_add_zero_padding_2d_image(current_input,
current_output, central_points, semi_block_size_input, semi_block_size_input2)
list_blocks_segmentation.append(crop_2D_block(current_output_padded, central_points, semi_block_size_output,semi_block_size_output2))
list_blocks_input.append(crop_2D_block(current_input_padded, central_points, semi_block_size_input,semi_block_size_input2))
list_blocks_input = np.stack(list_blocks_input)
list_blocks_segmentation = np.stack(list_blocks_segmentation)
shape_of_seg = list_blocks_segmentation.shape
list_blocks_segmentation = list_blocks_segmentation.reshape((-1,1))
#list_blocks_segmentation = output_transformation(list_blocks_segmentation)
#enc = preprocessing.OneHotEncoder()
#enc.fit(list_blocks_segmentation)
#list_blocks_segmentation = enc.transform(list_blocks_segmentation).toarray()
#list_blocks_segmentation = list_blocks_segmentation.reshape((-1,1))
list_blocks_segmentation = OneHotEncoder(list_blocks_segmentation)
list_blocks_segmentation = list_blocks_segmentation.reshape((shape_of_seg[0],shape_of_seg[1],shape_of_seg[2],dim_output))
return list_blocks_input, list_blocks_segmentation
def dice_score_multiclass(predicted_labels, labels, num_classes, type_unet):
#### Dice Score for at least 3 classes #####
### predicted_labels -- shape (num_batch, height, width, depth, num_classes)
### labels -- shape (num_batch, height, width, depth, num_classes)
print('shape of predicted labels')
print(predicted_labels)
print('shape of actual labels')
print(labels)
shape_of_data = labels.get_shape().as_list()
if type_unet=='3D':
indices_predictions = tf.argmax(tf.reshape(predicted_labels, [-1 , shape_of_data[4]]),axis=-1)
indices_predictions = tf.reshape(indices_predictions,[-1 , shape_of_data[1] * shape_of_data[2] * shape_of_data[3] * 1])
indices_labels = tf.argmax(tf.reshape(labels, [-1 , shape_of_data[4]]),axis=-1)
indices_labels = tf.reshape(indices_labels,[-1 , shape_of_data[1] * shape_of_data[2] * shape_of_data[3] * 1])
else:
indices_predictions = tf.argmax(tf.reshape(predicted_labels, [-1 , shape_of_data[3]]),axis=-1)
indices_predictions = tf.reshape(indices_predictions,[-1 , shape_of_data[1] * shape_of_data[2] * 1])
indices_labels = tf.argmax(tf.reshape(labels, [-1 , shape_of_data[3]]),axis=-1)
indices_labels = tf.reshape(indices_labels,[-1 , shape_of_data[1] * shape_of_data[2] * 1])
print('after transformation')
print(indices_predictions)
print(indices_labels)
dice_score = defaultdict()
for _ in range(num_classes):
shared_bool = tf.logical_and( tf.equal(tf.cast(indices_predictions,tf.float32),tf.ones_like(indices_predictions,dtype=tf.float32)* tf.cast(_,tf.float32)) ,
tf.equal(tf.cast(indices_labels,tf.float32),tf.ones_like(indices_predictions,dtype=tf.float32)*tf.cast(_,tf.float32)))
area_shared = tf.reduce_sum(tf.cast(shared_bool,tf.float32),1)
predictions_bool = tf.equal(tf.cast(indices_predictions,tf.float32),tf.ones_like(indices_predictions,dtype=tf.float32)* tf.cast(_,tf.float32))
area_predictions = tf.reduce_sum(tf.cast(predictions_bool,tf.float32),1)
labels_bool = tf.equal(tf.cast(indices_labels,tf.float32),tf.ones_like(indices_predictions,dtype=tf.float32)* tf.cast(_,tf.float32))
area_labels = tf.reduce_sum(tf.cast(labels_bool,tf.float32),1)
dice_score[_] = tf.reduce_mean( (2.0 * area_shared + 1e-6) / (area_predictions + area_labels + 1e-6))
return dice_score
def dice_score(predicted_labels, labels, dim_output, type_unet):
####### Dice score for binary classification #######
### predicted_labels -- shape (num_batch, height, width)
### labels -- shape (num_batch, height, width)
print('shape of predicted labels')
print(predicted_labels)
print('shape of actual labels')
print(labels)
shape_of_data = labels.get_shape().as_list()
indices_predictions = tf.round(tf.reshape(predicted_labels, [-1 , dim_output]))
if type_unet=='3D':
indices_predictions = tf.reshape(indices_predictions,[-1 , shape_of_data[1] * shape_of_data[2] * shape_of_data[3] * 1])
else:
indices_predictions = tf.reshape(indices_predictions,[-1 , shape_of_data[1] * shape_of_data[2] * 1])
indices_labels = tf.round(tf.reshape(labels, [-1 , dim_output]))
if type_unet=='3D':
indices_labels = tf.reshape(indices_labels,[-1 , shape_of_data[1] * shape_of_data[2] * shape_of_data[3] * 1])
else:
indices_labels = tf.reshape(indices_labels,[-1 , shape_of_data[1] * shape_of_data[2] * 1])
print('after transofrmation')
print(indices_predictions)
print(indices_labels)
dice_score = defaultdict()
for _ in range(2):
shared_bool = tf.logical_and( tf.equal(tf.cast(indices_predictions,tf.float32),tf.ones_like(indices_predictions,dtype=tf.float32)* tf.cast(_,tf.float32)) ,
tf.equal(tf.cast(indices_labels,tf.float32),tf.ones_like(indices_predictions,dtype=tf.float32)*tf.cast(_,tf.float32)))
area_shared = tf.reduce_sum(tf.cast(shared_bool,tf.float32),1)
predictions_bool = tf.equal(tf.cast(indices_predictions,tf.float32),tf.ones_like(indices_predictions,dtype=tf.float32)* tf.cast(_,tf.float32))
area_predictions = tf.reduce_sum(tf.cast(predictions_bool,tf.float32),1)
labels_bool = tf.equal(tf.cast(indices_labels,tf.float32),tf.ones_like(indices_predictions,dtype=tf.float32)* tf.cast(_,tf.float32))
area_labels = tf.reduce_sum(tf.cast(labels_bool,tf.float32),1)
dice_score[_] = tf.reduce_mean( (2.0 * area_shared+1e-6) / (area_predictions + area_labels + 1e-6))
return dice_score
###### Extract non-overlapping 2D patches in segmentation space #############
###### also extracts the overlapping bigger 2D patches in raw input space ####
def extract_2D_cubes_input_seg(input_image, output_image, semi_block_size_input1, semi_block_size_output1,
semi_block_size_input2, semi_block_size_output2, dim_output):
#### input_image -- shape (height, width, num_raw_modalities)
#### output_image -- shape (height, width)
block_size_output = semi_block_size_output1 + semi_block_size_output2
block_size_input = semi_block_size_input1 + semi_block_size_input2
shape_of_input_data = input_image.shape
num_cubes_dim1 = np.int(shape_of_input_data[0] // block_size_output)
num_cubes_dim2 = np.int(shape_of_input_data[1] // block_size_output)
list_input_cubes = []
list_output_cubes = []
min_value_image = np.min(input_image)
diff_semi_block1 = semi_block_size_input1 - semi_block_size_output1
diff_semi_block2 = semi_block_size_input2 - semi_block_size_output2
print('size of input image going in padding operation')
print(input_image.shape)
print(diff_semi_block1)
print(diff_semi_block2)
input_image_padded = np.pad(input_image, ((diff_semi_block1, diff_semi_block2),
(diff_semi_block1, diff_semi_block2), (0,0)), mode='constant', constant_values = min_value_image)
for i in range(num_cubes_dim1):
for j in range(num_cubes_dim2):
### extract segmentation space 3D cube ###
list_output_cubes.append(output_image[block_size_output*i:block_size_output*(i+1),
block_size_output*j:block_size_output*(j+1)])
print(list_output_cubes[-1].shape)
### extract raw input space 3D cube ###
list_input_cubes.append(input_image_padded[block_size_output*i:(block_size_output*i+block_size_input),
block_size_output*j:(block_size_output*j+block_size_input),:])
print(list_input_cubes[-1].shape)
list_output_cubes = np.stack(list_output_cubes)
list_output_cubes = output_transformation(list_output_cubes)
shape_of_seg_output = list_output_cubes.shape
list_output_cubes = list_output_cubes.reshape((-1,1))
enc = preprocessing.OneHotEncoder()
enc.fit(list_output_cubes)
list_output_cubes = enc.transform(list_output_cubes).toarray()
list_output_cubes = list_output_cubes.reshape((shape_of_seg_output[0],shape_of_seg_output[1],shape_of_seg_output[2], dim_output))
list_input_cubes = np.stack(list_input_cubes)
return list_input_cubes, list_output_cubes