-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_prep.py
339 lines (283 loc) · 14.4 KB
/
data_prep.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
# -*- coding: utf-8 -*-
from __future__ import print_function
import numpy as np
from skimage.measure import label, regionprops
from skimage.filters import sobel
import SimpleITK as sitk
from glob import glob
# import os
import re
import gc
import tensorflow as tf
import tensorlayer as tl
from keras.utils import to_categorical
import keras.backend.tensorflow_backend as K
# ----------------------------------------------------------------------------
# an argument to your Session's config arguments, it might help a little,
# though it won't release memory, it just allows growth at the cost of some memory efficiency
K.clear_session()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.allocator_type = 'BFC'
# with tf.Session(config = config) as s:
sess = tf.Session(config = config)
K.set_session(sess)
# ----------------------------------------------------------------------------
nclasses = 5
def convert(str):
return int("".join(re.findall("\d*", str)))
# copied from https://github.com/zsdonghao/u-net-brain-tumor
def distort_imgs(data):
""" data augmentation """
x1, x2, x3, x4, y = data
# previous without this, hard-dice=83.7
x1, x2, x3, x4, y = tl.prepro.flip_axis_multi([x1, x2, x3, x4, y],
axis=0, is_random=True) # up down
x1, x2, x3, x4, y = tl.prepro.flip_axis_multi([x1, x2, x3, x4, y],
axis=1, is_random=True) # left right
x1, x2, x3, x4, y = tl.prepro.elastic_transform_multi([x1, x2, x3, x4, y],
alpha=720, sigma=24, is_random=True)
x1, x2, x3, x4, y = tl.prepro.rotation_multi([x1, x2, x3, x4, y], rg=20,
is_random=True, fill_mode='constant') # nearest, constant
x1, x2, x3, x4, y = tl.prepro.shift_multi([x1, x2, x3, x4, y], wrg=0.10,
hrg=0.10, is_random=True, fill_mode='constant')
x1, x2, x3, x4, y = tl.prepro.shear_multi([x1, x2, x3, x4, y], 0.05,
is_random=True, fill_mode='constant')
x1, x2, x3, x4, y = tl.prepro.zoom_multi([x1, x2, x3, x4, y], zoom_range=[0.9, 1.1],
is_random=True, fill_mode='constant')
return x1, x2, x3, x4, y
def read_scans(file_path1, data_tr_test=False):
# nfiles = len(file_path1)
scan_idx = 0
nda_sum = 0
for name in file_path1:
# print ('\t', name)
file_scan = sitk.ReadImage(name) # (240, 240, 155) = (rows, cols, slices)
# if scan_idx == 99:
# print ('\t', name)
nda = sitk.GetArrayFromImage(file_scan) # convert to numpy array, (155, 240, 240)
if scan_idx == 0:
nda_sum = nda
# print(gt_sum.shape)
else:
# nda_sum = np.append(nda_sum, nda, axis=0)
nda_sum = np.concatenate((nda_sum, nda), axis=0) # faster
# print(nda_sum.shape)
# scan_idx += 1
if scan_idx < 99: # for BRATS 2015
scan_idx += 1
else:
break
#print(nda_sum.shape)
return nda_sum
# def resize_data(imgs_train1, imgs_train2, imgs_train3, imgs_train4, imgs_label):
def resize_data(imgs_train1, imgs_train2, imgs_train3, imgs_label):
# prepare data for CNNs with the softmax activation
nslices = 0
for n in range(imgs_train1.shape[0]):
label_temp = imgs_label[n] # imgs_label[n][:][:]
edges = sobel(label_temp)
# print(label_temp.shape)
c = np.count_nonzero(edges)
# print(c)
if c > 149:
train_resz1 = imgs_train1[n] # keep the original size of data
train_resz2 = imgs_train2[n]
train_resz3 = imgs_train3[n]
# train_resz4 = imgs_train4[n]
train_resz1 = train_resz1[..., np.newaxis]
train_resz2 = train_resz2[..., np.newaxis]
train_resz3 = train_resz3[..., np.newaxis]
# train_resz4 = train_resz4[..., np.newaxis]
train_sum = np.concatenate((train_resz1, train_resz2, train_resz3), axis=-1)
# train_sum = np.concatenate((train_sum, train_resz3), axis=-1) # 240, 240, 3
# train_sum = np.concatenate((train_sum, train_resz4), axis=-1) # 240, 240, 4
train_sum = train_sum[np.newaxis, ...] # 1, 240, 240, 3
label_resz = label_temp
label_resz2 = np.reshape(label_resz, 240*240).astype('int32')
label_resz2 = to_categorical(label_resz2, nclasses)
label_resz2 = label_resz2[np.newaxis, ...] # 1, 240*240, nclasses
if nslices == 0:
# flair_sum = np.asarray([flair_resz]) same as np.reshape(label_resz, (1, 64, 64))
# gt_sum = np.asarray([gt_resz])
data_sum = train_sum
label_sum = label_resz2
else:
data_sum = np.concatenate((data_sum, train_sum), axis=0) # faster
label_sum = np.concatenate((label_sum, label_resz2), axis=0)
nslices += 1
# print(train_sum.shape)
return data_sum, label_sum
# def resize_data(imgs_train1, imgs_train2, imgs_train3, imgs_train4, imgs_label):
def resize_data_aug(imgs_train1, imgs_train2, imgs_train3,imgs_train4, imgs_label):
# prepare data for CNNs with the softmax activation
# concat the original and augmented data
nslices = 0
for n in range(imgs_train1.shape[0]):
label_temp = imgs_label[n] # imgs_label[n][:][:]; n, 240, 240
edges = sobel(label_temp)
print("slice :",n)
c = np.count_nonzero(edges)
print("****",c)
if c > 149:
train_resz1 = imgs_train1[n] # keep the original size of data
train_resz2 = imgs_train2[n]
train_resz3 = imgs_train3[n]
train_resz4 = imgs_train4[n]
train_resz1 = train_resz1[..., np.newaxis]
train_resz2 = train_resz2[..., np.newaxis]
train_resz3 = train_resz3[..., np.newaxis]
train_resz4 = train_resz4[..., np.newaxis]
label_temp2 = label_temp.astype('int32')
label_temp2 = label_temp2[..., np.newaxis]
train_aug1, train_aug2, train_aug3, train_aug4, label_aug = distort_imgs([train_resz1, train_resz2, train_resz3,train_resz4, label_temp2])
# print(train_aug1.shape, label_aug.shape) # 240, 240, 1
train_sum = np.concatenate((train_resz1, train_resz2, train_resz3,train_resz4), axis=-1) # 240, 240, 3
train_sum = train_sum[np.newaxis, ...] # 1, 240, 240, 3
train_sum2 = np.concatenate((train_aug1, train_aug2, train_aug3,train_aug4), axis=-1) # 240, 240, 3
train_sum2 = train_sum2[np.newaxis, ...] # 1, 240, 240, 3
# label_resz = label_temp
label_resz = np.reshape(label_temp, 240*240).astype('int32')
label_resz = to_categorical(label_resz, nclasses)
label_resz = label_resz[np.newaxis, ...] # 1, 240*240, nclasses
label_aug2 = label_aug[:, :, 0] # 240, 240, 1 to 240, 240
label_resz2 = np.reshape(label_aug2, 240*240).astype('int32')
label_resz2 = to_categorical(label_resz2, nclasses)
label_resz2 = label_resz2[np.newaxis, ...] # 1, 240*240, nclasses
if nslices == 0:
data_sum = np.concatenate((train_sum, train_sum2), axis=0) # faster
label_sum = np.concatenate((label_resz, label_resz2), axis=0)
else:
data_sum = np.concatenate((data_sum, train_sum, train_sum2), axis=0) # faster
label_sum = np.concatenate((label_sum, label_resz, label_resz2), axis=0)
nslices += 1
return data_sum, label_sum
# for testing data
#def resize_data_full(imgs_train1, imgs_train2, imgs_train3, imgs_label):
def resize_data_full(imgs_train1, imgs_train2, imgs_train3, imgs_train4, imgs_label):
# prepare data for CNNs with the softmax activation
for n in range(imgs_label.shape[0]):
label_resz = imgs_label[n]
label_resz2 = np.reshape(label_resz, 240*240).astype('int32')
label_resz2 = to_categorical(label_resz2, nclasses)
label_resz2 = label_resz2[np.newaxis, ...] # 1, 240*240, nclasses
if n == 0:
label_sum = label_resz2
else:
label_sum = np.concatenate((label_sum, label_resz2), axis=0)
imgs_train1 = imgs_train1[..., np.newaxis] # 155, 240, 240, 1
imgs_train2 = imgs_train2[..., np.newaxis]
imgs_train3 = imgs_train3[..., np.newaxis]
imgs_train4 = imgs_train4[..., np.newaxis]
# concat data to 155, 240, 240, 3
data_sum = np.concatenate((imgs_train1, imgs_train2, imgs_train3,imgs_train4), axis=-1)
return data_sum, label_sum
def create_train_data(type_data='HGG'):
if type_data == 'HGG':
flairs = glob(r'mhafiles/BRATS2015_Training/HGG/*/*Flair*/*Flair*.mha')
t1cs = glob(r'mhafiles/BRATS2015_Training/HGG/*/*T1c*/*T1c*.mha')
t2s = glob(r'mhafiles/BRATS2015_Training/HGG/*/*T2*/*T2*.mha')
t1s = glob('mhafiles/BRATS2015_Training/HGG/*/*T1*/*T1.*.mha')
gts = glob('mhafiles/BRATS2015_Training/HGG/*/*OT*/*OT*.mha')
flairs.sort(key=convert)
t1cs.sort(key=convert)
t2s.sort(key=convert)
# t1s.sort(key=convert)
gts.sort(key=convert)
nfiles = len(flairs)
flair_sum = read_scans(flairs, True)
print(flair_sum.shape)
t1c_sum = read_scans(t1cs, True)
print(t1c_sum.shape)
t2_sum = read_scans(t2s, True)
print(t2_sum.shape)
t1_sum = read_scans(t1s, True)
print(t1_sum.shape)
gt_sum = read_scans(gts)
print(gt_sum.shape)
print('Combining training data for the softmax activation...')
total3_train, gt_train = resize_data_aug(flair_sum, t1c_sum, t2_sum,t1_sum, gt_sum)
print(total3_train.shape)
print(gt_train.shape)
if type_data == 'HGG':
# full HGG data of BRATS 2013
np.save('mhafiles/Data/imgs_train_unet_HG.npy', total3_train)
np.save('mhafiles/Data/imgs_label_train_unet_HG.npy', gt_train)
# np.save('D:\mhafiles\Data\imgs_train_unet_IN.npy', total3_train)
# np.save('D:\mhafiles\Data\imgs_label_train_unet_IN.npy', gt_train)
print('Saving all HGG training data to .npy files done.')
else:
print('Cannot save type of data as you want')
for i in range(30):
gc.collect()
def load_train_data(type_data='HGG'):
imgs_label=0
imgs_train=0
if type_data == 'HGG':
imgs_train = np.load('mhafiles/Data/imgs_train_unet_HG.npy')
imgs_label = np.load('mhafiles/Data/imgs_label_train_unet_HG.npy')
print('Imgs train shape', imgs_train.shape)
print('Imgs label shape', imgs_label_train.shape)
# imgs_train = np.load('D:\mhafiles\Data\imgs_train_unet_IN.npy')
# imgs_label = np.load('D:\mhafiles\Data\imgs_label_train_unet_IN.npy')
elif type_data == 'LGG':
imgs_train = np.load('mhafiles/Data/imgs_train_unet_LG.npy')
imgs_label = np.load('mhafiles/Data/imgs_label_train_unet_LG.npy')
elif type_data == 'Full_HGG':
imgs_train = np.load('mhafiles\Data/imgs_train_unet_FHG.npy')
imgs_label = np.load('mhafiles/Data/imgs_label_train_unet_FHG.npy')
else:
print('No type of data as you want')
return imgs_train, imgs_label
def create_test_data():
# flairs_test = glob('D:\mhafiles\HGG_Flair_6.mha')
# t1cs_test = glob('D:\mhafiles\HGG_T1c_6.mha')
# t2s_test = glob('D:\mhafiles\HGG_T2_6.mha')
# gts = glob('D:\mhafiles\HGG_OT_6.mha')
# BRATS 2015
flairs_test = glob(r'mhafiles/BRATS2015_Training/HGG/*2013*/*Flair*/*Flair*.mha')
t1cs_test = glob(r'mhafiles/BRATS2015_Training/HGG/*2013*/*T1c*/*T1c*.mha')
t2s_test = glob(r'mhafiles/BRATS2015_Training/HGG/*2013*/*T2*/*T2*.mha')
t1s_test = glob('mhafiles/BRATS2015_Training/HGG/*2013*/*T1.*/*T1.*.mha')
gts = glob('mhafiles/BRATS2015_Training/HGG/*2013*/*OT*/*OT*.mha')
flair_sum = read_scans(flairs_test, True)
# flair_sum = read_scans_IN(flairs_test, 0)
# flair_sum = read_scans_Nyul(flairs_test, 0)
# flair_sum = read_scans_Nyul_IN(flairs_test, 0)
print(flair_sum.shape)
t1c_sum = read_scans(t1cs_test, True)
# t1c_sum = read_scans_IN(t1cs_test, 1)
# t1c_sum = read_scans_Nyul(t1cs_test, 1)
# t1c_sum = read_scans_Nyul_IN(t1cs_test, 1)
print(t1c_sum.shape)
t2_sum = read_scans(t2s_test, True)
# t2_sum = read_scans_IN(t2s_test, 2)
# t2_sum = read_scans_Nyul(t2s_test, 2)
# t2_sum = read_scans_Nyul_IN(t2s_test, 2)
print(t2_sum.shape)
t1_sum = read_scans(t1s_test, True)
# t1_sum = read_scans_IN(t1s_test, 2)
print(t1_sum.shape)
gt_sum = read_scans(gts)
print(gt_sum.shape)
print('Resizing testing data for the softmax activation...')
total3_test, gt_test = resize_data_full(flair_sum, t1c_sum, t2_sum, t1_sum, gt_sum)
print(total3_test.shape)
print(gt_test.shape)
np.save('mhafiles/Data/imgs_test_unet_HN.npy', total3_test)
np.save('mhafiles/Data/imgs_label_test_unet_HN.npy', gt_test)
print('Saving testing data.')
for i in range(30):
gc.collect()
def load_test_data():
imgs_test = np.load('mhafiles/Data/imgs_test_unet_HN.npy')
imgs_label_test = np.load('mhafiles/Data/imgs_label_test_unet_HN.npy')
return imgs_test, imgs_label_test
def load_val_data():
imgs_val = np.load('mhafiles/Data/imgs_val_unet.npy')
imgs_label_val = np.load('mhafiles/Data/imgs_label_val_unet.npy')
return imgs_val, imgs_label_val
if __name__ == '__main__':
create_train_data('HGG')
#create_test_data()
#load_train_data('HGG')