-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
253 lines (217 loc) · 10.3 KB
/
Copy pathdata_loader.py
File metadata and controls
253 lines (217 loc) · 10.3 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
import random
import SimpleITK as sitk
import numpy as np
import torch
import torch.nn.functional as F
import torchvision.transforms as transforms
import torchvision.transforms.functional as TF
from torch.utils.data import Dataset
def loop_translate(a, d):
n = np.ndarray(a.shape)
for k in d:
n[a == k] = d[k]
return n
def get_datas(data_path, excluded_classes=None, n_classes=None):
if type(data_path) != str:
data_path = data_path.decode("utf-8") # convert bytes to str
path = data_path.split(",")
image_path = path[0]
label_path = path[1]
itk_image = sitk.ReadImage(image_path)
itk_mask = sitk.ReadImage(label_path)
image = sitk.GetArrayFromImage(itk_image)
mask = sitk.GetArrayFromImage(itk_mask)
if excluded_classes is not None:
mask_not_excluded = 1 - np.isin(mask, excluded_classes)
mask = mask * mask_not_excluded
post_mapping_dict = {}
original_classes = list(range(n_classes + len(excluded_classes)))
remain_classes = [item for item in original_classes if item not in excluded_classes]
for new_value, value in enumerate(remain_classes):
post_mapping_dict[value] = new_value
mask = loop_translate(mask, post_mapping_dict)
return image.transpose([1, 2, 0]), mask.transpose([1, 2, 0])
def extract_patch(image, mask, patch_size=(384, 384, 3), num_class=2, augmentation=False,
num_patches=1, training=True, mid_slice_idx=None, zoom_crop=False):
"""Extracts a patch of given resolution and size at a specific location."""
image_patches = []
mask_patches = []
num_patches_now = 0
while num_patches_now < num_patches:
if training:
z = random_patch_center_z(mask, patch_size=patch_size) # define the centre of current patch
else:
z = mid_slice_idx
image_patch = image[:, :, z - 1:z + 2]
mask_patch = mask[:, :, z]
image_patch = torch.from_numpy(image_patch.astype(np.float32)).permute(2, 0, 1)
mask_patch = torch.from_numpy(mask_patch.astype(np.float32))
# data augmentation
if augmentation:
if random.randint(0, 1) == 1:
rand_degree = random.randint(-10, 10)
image_patch = TF.rotate(image_patch.unsqueeze(0), rand_degree, resample=2).squeeze()
mask_patch = TF.rotate(mask_patch.unsqueeze(0), rand_degree, resample=2).squeeze()
if zoom_crop:
if random.randint(0, 1) == 1:
zoom_crop_scale = random.uniform(1, 1.5)
image_patch = F.interpolate(image_patch.unsqueeze(0), scale_factor=zoom_crop_scale,
mode='bilinear').squeeze()
image_patch = transforms.CenterCrop(384)(image_patch)
mask_patch = F.interpolate(mask_patch.unsqueeze(0).unsqueeze(0), scale_factor=zoom_crop_scale,
mode='nearest').squeeze()
mask_patch = transforms.CenterCrop(384)(mask_patch)
if num_class == 2: # for multi-class dataset, flip could mislead the classification
if random.randint(0, 1) == 1:
image_patch = torch.flip(image_patch, dims=[-1])
mask_patch = torch.flip(mask_patch, dims=[-1])
if random.randint(0, 1) == 1:
image_patch = translate(image_patch)
mask_patch = translate(mask_patch.unsqueeze(0)).squeeze()
image_patches.append(image_patch)
mask_patches.append(mask_patch)
num_patches_now += 1
image_patches = image_patches[0]
mask_patches = mask_patches[0]
return image_patches, mask_patches
def random_patch_center_z(mask, patch_size):
z = np.random.randint(patch_size[2] // 2, mask.shape[2] - patch_size[2] // 2)
return z
def crop_center(img, new_height, new_width=None):
height = img.shape[0]
width = img.shape[1]
if not new_width:
new_width = new_height
starty = height // 2 - (new_height // 2)
startx = width // 2 - (new_width // 2)
return img[starty:starty + new_height, startx:startx + new_width, ...]
def translate(img, shift_std=4):
shift = int(np.around(np.random.randn(1)[0] * shift_std))
if shift == 0:
return img
else:
i = random.randint(1, 2)
img = torch.roll(img, shifts=shift, dims=i)
return img
class SiteSet(Dataset):
def __init__(self, datalist, iters=2400, training=True, augmentation=False, source='Overall', zoom_crop=False,
whitening=True, batchsize=4, site_num=3, n_classes=2, excluded_classes=None
):
images = []
masks = []
case_idx = 0
self.test_idxlist = []
self.site = source
self.training = training
self.whitening = whitening
self.n_classes = n_classes
if excluded_classes is not None and training:
post_mapping_dict = {}
original_classes = list(range(n_classes + len(excluded_classes)))
remain_classes = [item for item in original_classes if item not in excluded_classes]
for new_value, value in enumerate(remain_classes):
post_mapping_dict[value] = new_value
print('Post mapping dict:', post_mapping_dict)
if self.site == 'Overall':
for sidx, sitelist in enumerate(datalist):
site_images = []
site_masks = []
np.random.shuffle(sitelist)
for case in sitelist:
if len(case) > 0:
case_data = get_datas(case, excluded_classes=excluded_classes, n_classes=n_classes)
image = case_data[0]
mask = case_data[1]
if not training:
for mid_slice in range(1, image.shape[2] - 1):
self.test_idxlist.append([case_idx, mid_slice])
case_idx += 1
site_images.append(image)
site_masks.append(mask)
images.append(site_images)
masks.append(site_masks)
else:
np.random.shuffle(datalist)
for case in datalist:
if len(case) > 0:
case_data = get_datas(case, excluded_classes=excluded_classes, n_classes=n_classes)
image = case_data[0]
mask = case_data[1]
if not training:
for mid_slice in range(1, image.shape[2] - 1):
self.test_idxlist.append([case_idx, mid_slice])
case_idx += 1
images.append(image)
masks.append(mask)
if whitening:
if self.site == 'Overall':
for site_idx, site_images in enumerate(images):
for image_idx, image in enumerate(site_images):
mean = image.mean()
std = image.std()
images[site_idx][image_idx] = (image - mean) / std
else:
for image_idx, image in enumerate(images):
mean = image.mean()
std = image.std()
images[image_idx] = (image - mean) / std
self.images = images
self.masks = masks
self.iteration = iters
self.batchsize = batchsize
self.site_num = site_num
self.augmentation = augmentation
self.zoom_crop = zoom_crop
if self.training:
if self.site == 'Overall':
set_length = self.iteration // site_num
index_list = {}
for i in range(site_num):
site_length = len(self.images[i])
index_origin = np.arange(len(self.images[i]))
index_list[i] = index_origin.copy()
for turn in range(set_length // site_length):
np.random.shuffle(index_origin)
index_list[i] = np.hstack((index_list[i], index_origin.copy()))
self.index_list = index_list
else:
set_length = self.iteration
site_length = len(self.images)
index_origin = np.arange(len(self.images))
index_list = index_origin.copy()
for i in range(set_length // site_length):
np.random.shuffle(index_origin)
index_list = np.hstack((index_list, index_origin.copy()))
self.index_list = index_list
def __getitem__(self, idx):
if self.training:
# multiple sites
if self.site == 'Overall':
site_idx = (idx % (self.site_num * self.batchsize)) // self.batchsize
idx = ((idx // (self.site_num * self.batchsize)) * self.batchsize + (
(idx % (self.site_num * self.batchsize)) - site_idx * self.batchsize))
idx = self.index_list[site_idx][idx]
image, mask = extract_patch(self.images[site_idx][idx], self.masks[site_idx][idx],
augmentation=self.augmentation, num_class=self.n_classes,
zoom_crop=self.zoom_crop)
else:
# only one site
idx = self.index_list[idx]
site_idx_dict = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5}
site_idx = site_idx_dict[self.site]
image, mask = extract_patch(self.images[idx], self.masks[idx],
augmentation=self.augmentation, num_class=self.n_classes,
zoom_crop=self.zoom_crop)
return image, mask, site_idx
else:
case_idx = self.test_idxlist[idx]
image, mask = extract_patch(self.images[case_idx[0]], self.masks[case_idx[0]],
augmentation=False, num_class=self.n_classes, training=False,
mid_slice_idx=case_idx[1])
return image, mask
def __len__(self):
if self.training:
length = self.iteration
else:
length = len(self.test_idxlist)
return length