-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMRIDataset.py
217 lines (202 loc) · 7.77 KB
/
MRIDataset.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
import os
import os.path
import numpy as np
import random
import h5py
import torch
import cv2
import glob
import torch.utils.data as udata
import nibabel as ni
from scipy.ndimage import zoom
def normalize(data):
ratio = np.amax(data) / 255
data = (data / ratio).astype('uint8')/255.
data = data.astype('float32')
return data, ratio
def data_augmentation(image, mode):
if image.ndim == 3:
out = np.transpose(image, (1,2,0))
elif image.ndim == 4:
out = np.transpose(image,(1,2,3,0))
elif image.ndim == 5:
out = np.transpose(image,(1,2,3,4,0))
if mode == 0:
# original
out = out
elif mode == 1:
# flip up and down
out = np.flipud(out)
elif mode == 2:
# rotate counterwise 90 degree
out = np.rot90(out)
elif mode == 3:
# rotate 90 degree and flip up and down
out = np.rot90(out)
out = np.flipud(out)
elif mode == 4:
# rotate 180 degree
out = np.rot90(out, k=2)
elif mode == 5:
# rotate 180 degree and flip
out = np.rot90(out, k=2)
out = np.flipud(out)
elif mode == 6:
# rotate 270 degree
out = np.rot90(out, k=3)
elif mode == 7:
# rotate 270 degree and flip
out = np.rot90(out, k=3)
out = np.flipud(out)
if image.ndim == 3:
out = np.transpose(out, (2,0,1))
elif image.ndim == 4:
out = np.transpose(out,(3,0,1,2))
elif image.ndim == 5:
out = np.transpose(out,(4,0,1,2,3))
return out
def Im2Patch(img, win, stride=1, dim=3):
l = 0
endc = int(img.shape[0])
endw = int(img.shape[1])
endh = int(img.shape[2])
endd = int(img.shape[3])
win = int(win)
stride = int(stride)
xlen = range(endw//4,3*endw//4)
ylen = range(endh//4,3*endh//4)
zlen = range(endd//4,3*endd//4)
print(img.shape)
img = img[:,endw//4:3*endw//4, endh//4:3*endh//4, endd//4:3*endd//4]
endc = int(img.shape[0])
endw = int(img.shape[1])
endh = int(img.shape[2])
endd = int(img.shape[3])
print(img.shape)
if dim==3:
patch = img[:, 0:endw-win+0+1:stride, 0:endh-win+0+1:stride, 0:endd-win+0+1:stride]
TotalPatNum = patch.shape[1] * patch.shape[2] * patch.shape[3]
Y = np.zeros([endc, win*win*win, TotalPatNum], np.float32)
for i in range(win):
for j in range(win):
for k in range(win):
patch = img[:,i:endw-win+i+1:stride,j:endh-win+j+1:stride,k:endd-win+k+1:stride]
Y[:,l,:] = np.array(patch[:]).reshape(endc, TotalPatNum)
l = l + 1
out_patch = Y.reshape([endc, win, win, win, TotalPatNum])
if dim==2:
patch = img[:, 0:int(endw-win+0+1):stride, 0:int(endh-win+0+1):stride, 0]
TotalPatNum = patch.shape[1] * patch.shape[2]
Y = np.zeros([endc, win*win*len(zlen), TotalPatNum], np.float32)
for i in range(win):
for j in range(win):
for k in range(endd):
patch = img[:,i:endw-win+i+1:stride,j:endh-win+j+1:stride, k]
Y[:,l,:] = np.array(patch[:]).reshape(endc, TotalPatNum)
l = l + 1
out_patch = Y.reshape([endc, win, win, TotalPatNum*len(zlen)])
return out_patch
def prepare_data(data_path, patch_size, stride, aug_times=1, dim=3, fname='mri'):
# train
print('process training data')
scales = [1]
files_clean = glob.glob(os.path.join(data_path, 'train', '*clean.nii'))
files_clean.sort()
files_noisy = glob.glob(os.path.join(data_path, 'train', '*noisy.nii'))
files_noisy.sort()
files_clean = files_clean[:100]
file_noisy = files_noisy[:100]
h5f = h5py.File(fname + '_train.h5', 'w')
train_num = 0
for i in range(len(files_clean)):
print(files_clean[i])
nii = ni.load(files_clean[i])
img_clean = np.array(nii.dataobj).astype('float32')
nii = ni.load(files_noisy[i])
img_noisy = np.array(nii.dataobj).astype('float32')
h, w, d = img_clean.shape
for k in range(len(scales)):
Img_clean = zoom(img_clean, scales[k])
Img_clean = np.expand_dims(Img_clean.copy(), 0)
Img_clean, ratio_c = normalize(Img_clean)
patches_clean = Im2Patch(Img_clean, win=patch_size, stride=stride, dim=dim)
Img_noisy = zoom(img_noisy, scales[k])
Img_noisy = np.expand_dims(Img_noisy.copy(), 0)
Img_noisy, ratio_n = normalize(Img_noisy)
patches_noisy = Im2Patch(Img_noisy, win=patch_size, stride=stride, dim=dim)
print("file: %s scale %.1f # samples: %d" % (files_clean[i], scales[k], patches_clean.shape[-1]*aug_times))
for n in range(patches_clean.shape[-1]):
data_clean = patches_clean[...,n].copy()
data_noisy = patches_noisy[...,n].copy()
h5f.create_dataset(str(train_num), data=(data_clean, data_noisy))
train_num += 1
for m in range(aug_times-1):
aug_mode = np.random.randint(1,8)
data_aug_clean = data_augmentation(data_clean, aug_mode)
data_aug_noisy = data_augmentation(data_noisy, aug_mode)
h5f.create_dataset(str(train_num)+"_aug_%d" % (m+1), data=(data_aug_clean, data_aug_noisy))
train_num += 1
h5f.close()
# val
print('\nprocess validation data')
files_clean.clear()
files_clean = glob.glob(os.path.join(data_path, 'val', '*clean.nii'))
files_clean.sort()
files_noisy.clear()
files_noisy = glob.glob(os.path.join(data_path, 'val', '*noisy.nii'))
files_noisy.sort()
files_clean = files_clean[:20]
files_noisy = files_noisy[:20]
print(files_clean)
print(files_noisy)
h5f = h5py.File(fname + '_val.h5', 'w')
val_num = 0
for i in range(len(files_clean)):
print("file: %s" % files_clean[i])
nii = ni.load(files_clean[i])
img_clean = np.array(nii.dataobj).astype('float32')
img_clean = np.expand_dims(img_clean, 0)
img_clean, ratio_c = normalize(img_clean)
nii = ni.load(files_noisy[i])
img_noisy = np.array(nii.dataobj).astype('float32')
img_noisy = np.expand_dims(img_noisy, 0)
img_noisy, ratio_n = normalize(img_noisy)
if dim == 2:
#for n in range(img_clean.shape[-1]):
data_clean = img_clean[...,124].copy()
data_noisy = img_noisy[...,124].copy()
h5f.create_dataset(str(val_num), data=(data_clean, data_noisy))
val_num += 1
if dim == 3:
print(img_clean.shape)
print(val_num)
data_clean = img_clean.copy()
data_noisy = img_noisy.copy()
h5f.create_dataset(str(val_num), data=(data_clean, data_noisy))
val_num += 1
h5f.close()
print('training set, # samples %d\n' % train_num)
print('val set, # samples %d\n' % val_num)
class Dataset(udata.Dataset):
def __init__(self, train=True, fname='mri'):
super(Dataset, self).__init__()
self.train = train
self.fname = fname
if self.train:
h5f = h5py.File(fname + '_train.h5', 'r')
else:
h5f = h5py.File(fname + '_val.h5', 'r')
self.keys = list(h5f.keys())
random.shuffle(self.keys)
h5f.close()
def __len__(self):
return len(self.keys)
def __getitem__(self, index):
if self.train:
h5f = h5py.File(self.fname + '_train.h5', 'r')
else:
h5f = h5py.File(self.fname + '_val.h5', 'r')
key = self.keys[index]
data = np.array(h5f[key])
h5f.close()
return torch.Tensor(data)