-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdata_load.py
256 lines (219 loc) · 8.58 KB
/
data_load.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
import nibabel as nib
import numpy as np
import os
import h5py
import random
import cv2
import copy
from matplotlib import pyplot as plt
from PIL import Image
import time
def nii_to_h5(path_nii,path_save,ratio=0.8):
data = []
label = []
ori = []
list_site = os.listdir(path_nii)
list_data = []
ori_min = 10000
ori_max = 0
for dir_num, dir_site in enumerate(list_site):
if dir_site[-3:] == 'csv':
continue
list_patients = os.listdir(path_nii+'/'+dir_site)
for dir_patients in list_patients:
for t0n in ['/t01/', '/t02/']:
try:
location = path_nii+'/' + dir_site + '/' + dir_patients + t0n
location_all = os.listdir(location)
for i in range(len(location_all)):
location_all[i] = location+location_all[i]
list_data.append(location_all)
except:
continue
random.shuffle(list_data)
for num, data_dir in enumerate(list_data):
for i, deface in enumerate(data_dir):
if deface.find('deface') != -1:
ori = nib.load(deface)
ori = ori.get_fdata()
ori = np.array(ori)
ori = ori.transpose((2, 1, 0))
if ori_max < ori.max():
ori_max = ori.max()
if ori_min > ori.min():
ori_min = ori.min()
del list_data[num][i]
break
label_merge = np.zeros_like(ori)
for i, dir_data in enumerate(list_data[num]):
img = nib.load(dir_data)
img = np.array(img.get_fdata())
img = img.transpose((2, 1, 0))
label_merge = label_merge + img
print(str(num)+'/'+str(len(list_data)),'max=',str(ori.max()),'min=',str(ori.min()))
if num == 0 or num == int(ratio * len(list_data)):
data = copy.deepcopy(ori)
label = copy.deepcopy(label_merge)
else:
data = np.concatenate((data, ori), axis=0)
label = np.concatenate((label, label_merge), axis=0)
if num == int(ratio * len(list_data))-1:
print('saving train set...')
data = np.array(data, dtype=float)
label = np.array(label, dtype=bool)
#'''
file = h5py.File(path_save + '/train_' + str(ratio), 'w')
file.create_dataset('data', data=data)
file.create_dataset('label', data=label)
file.close()
data = []
label = []
print('Finished!')
elif num == len(list_data)-1:
print('saving test set...')
data = np.array(data, dtype=float)
label = np.array(label, dtype=bool)
file = h5py.File(path_save + '/test_' + str(ratio), 'w')
file.create_dataset('data', data=data)
file.create_dataset('label', data=label)
file.close()
print('Finished!')
return ori_max, ori_min
#'''
def data_adjust(max, min, h5_path, ratio=0.8):
file = h5py.File(h5_path + '/test_' + str(ratio))
data = file['data']
label = file['label']
data = data - min
data = data / max
data = data*255
file_adjust = h5py.File(h5_path + '/detection/test', 'w')
file_adjust.create_dataset('data', data=data)
file_adjust.create_dataset('label', data=label)
file.close()
file_adjust.close()
file = h5py.File(h5_path + '/train_' + str(ratio))
data = file['data']
label = file['label']
data = data - min
data = data / max
data = data*255
file_adjust = h5py.File(h5_path + '/detection/train', 'w')
file_adjust.create_dataset('data', data=data)
file_adjust.create_dataset('label', data=label)
file.close()
file_adjust.close()
def load_h5(path_h5, shuffle=False, size=None, test_programme=None, only=False):
h5 = h5py.File(path_h5)
data = h5['data'][:]
label = h5['label'][:]
if test_programme is not None:
data = data[:test_programme]
label = label[:test_programme]
data_only = []
label_only = []
if only is True:
for i in range(len(data)):
if label[i].max() == 1:
data_only.append(data[i])
label_only.append(label[i])
del data, label
data = data_only
label = label_only
data = np.uint8(np.multiply(data, 2.55))
label = np.uint8(np.multiply(label, 255))
if size is not None:
data_resize = []
label_resize = []
for i in range(len(data)):
data_resize_single = Image.fromarray(data[i]).crop((10, 40, 190, 220))
data_resize_single = data_resize_single.resize(size, Image.ANTIALIAS)
data_resize_single = np.asarray(data_resize_single)
label_resize_single = Image.fromarray(label[i]).crop((10, 40, 190, 220))
label_resize_single = label_resize_single.resize(size, Image.ANTIALIAS)
label_resize_single = np.asarray(label_resize_single)
data_resize.append(data_resize_single)
label_resize.append(label_resize_single)
data = np.array(data_resize, dtype=float)
label = np.array(label_resize, dtype=int)
data = data - data.min()
data = data / data.max()
label = label - label.min()
label = label / label.max()
if shuffle is True:
orders = []
data_output = np.zeros_like(data)
label_output = np.zeros_like(label)
for i in range(len(data)):
orders.append(i)
random.shuffle(orders)
for i, order in enumerate(orders):
data_output[i] = data[order]
label_output[i] = label[order]
else:
data_output = data
label_output = label
# for i in range(500):
# plt.subplot(1,2,1)
# plt.imshow(data_output[i],cmap='gray')
# plt.subplot(1,2,2)
# plt.imshow(label_output[i],cmap='gray')
# plt.pause(0.1)
# print(data_output[i].max(),data_output[i].min(),label_output[i].max(),label_output[i].min())
return data_output, label_output
def data_toxn(data, z):
data_xn = np.zeros((data.shape[0], data.shape[1], data.shape[2], z))
for patient in range(int(len(data) / 189)):
for i in range(189):
for j in range(z):
if i + j - z // 2 >= 0 and i + j - z // 2 < 189:
data_xn[patient * 189 + i, :, :, j] = data[patient * 189 + i + j - z // 2]
print(i, i + j - z // 2)
else:
data_xn[patient * 189 + i, :, :, j] = np.zeros_like(data[0])
return data_xn
if __name__ == "__main__":
start = time.time()
path_nii = './ATLAS_R1.1'
path_save = './h5'
ratio = 0.8
img_size = [192, 192]
ori_max, ori_min = nii_to_h5(path_nii, path_save, ratio=ratio)
data_adjust(ori_max, ori_min, path_save)
print('using :{}'.format(time.time()-start))
print('loading training-data...')
time_start = time.time()
original, label = load_h5(path_save + 'train_' + str(ratio), size=(img_size[1], img_size[0]),
test_programme = None)
file = h5py.File(path_save+'/train', 'w')
original = data_toxn(original, 4)
file.create_dataset('data', data=original)
original = original.transpose((0, 3, 1, 2))
original = np.expand_dims(original, axis=-1)
file.create_dataset('data_lstm', data=original)
del original
label_change = data_toxn(label, 4)
file.create_dataset('label_change', data=label_change)
del label_change
label = np.expand_dims(label, axis=-1)
file.create_dataset('label', data=label)
del label
file.close()
print('training_data done!, using:', str(time.time() - time_start) + 's\n\nloading validation-data...')
time_start = time.time()
original_val, label_val = load_h5(path_save + 'test_' + str(ratio), size=(img_size[1], img_size[0]))
file = h5py.File(path_save+'/train', 'w')
original_val = data_toxn(original_val, 4)
file.create_dataset('data_val', data=original_val)
original_val = original_val.transpose((0, 3, 1, 2))
original_val = np.expand_dims(original_val, axis=-1)
file.create_dataset('data_val_lstm', data=original_val)
del original_val
label_val_change = data_toxn(label_val, 4)
file.create_dataset('label_val_change', data=label_val_change)
del label_val_change
label_val = np.expand_dims(label_val, axis=-1)
file.create_dataset('label_val', data=label_val)
del label_val
file.close()
print('validation_data done!, using:', str(time.time() - time_start) + 's\n\n')