-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_LAA.py
208 lines (164 loc) · 8.23 KB
/
dataset_LAA.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
import os
import random
import h5py
import numpy as np
import torch
from scipy import ndimage
from scipy.ndimage.interpolation import zoom
from torch.utils.data import Dataset
import cv2
import argparse
from torchvision import transforms
from torch.utils.data import DataLoader
import SimpleITK as sitk
def random_rotate(image, label, min_value):
angle = np.random.randint(-15, 15) # -20--20
rotate_axes = [(0, 1), (1, 2), (0, 2)]
k = np.random.randint(0, 3)
# image = ndimage.rotate(image, angle, reshape=False, order=3, mode='constant', cval=-2.0)
image = ndimage.interpolation.rotate(image, angle, axes=rotate_axes[k], reshape=False, order=3, mode='constant',
cval=min_value)
# label = ndimage.rotate(label, angle, reshape=False, order=0, mode='constant', cval=0.0)
label = ndimage.interpolation.rotate(label, angle, axes=rotate_axes[k], reshape=False, order=0, mode='constant',
cval=0.0)
return image, label
def intensity_shift(image):
channel_id = np.array([np.random.randint(2)])
shift_value = np.array([random.uniform(-0.1, 0.1)])
value = (channel_id * shift_value).reshape(1, 1, 1)
image = image + value
return image
def intensity_scale(image):
channel_id = np.array([np.random.randint(2)])
scale_value = np.array([random.uniform(0.9, 1.1)])
value = (channel_id * scale_value).reshape(1, 1, 1)
image = image * value
return image
class RandomGenerator(object):
def __init__(self, output_size, mode):
self.output_size = output_size
self.mode = mode
def __call__(self, sample):
image, label = sample['image'], sample['label']
min_value = np.min(image)
# centercop
# crop alongside with the ground truth
index = np.nonzero(label)
index = np.transpose(index)
z, y, x = label.shape
# print('shape: ', z, y, x)
z_min = np.min(index[:, 0])
z_max = np.max(index[:, 0])
y_min = np.min(index[:, 1])
y_max = np.max(index[:, 1])
x_min = np.min(index[:, 2])
x_max = np.max(index[:, 2])
patch_z = np.int(self.output_size[0] / 2 * 1.25)
patch_y = np.int(self.output_size[1] / 2)
patch_x = np.int(self.output_size[2] / 2 * 1.25)
# middle point
z_middle = np.int((z_min + z_max) / 2)
y_middle = np.int((y_min + y_max) / 2)
x_middle = np.int((x_min + x_max) / 2)
Delta_z = np.int((z_max - z_min) / 3) # 3
Delta_y = np.int((y_max - y_min) / 3) # 3
Delta_x = np.int((x_max - x_min) / 3) # 3
# random number of x, y, z
if random.random() > 0.2:
z_random = random.randint(z_middle - Delta_z, z_middle + Delta_z)
y_random = random.randint(y_middle - Delta_y, y_middle + Delta_y)
x_random = random.randint(x_middle - Delta_x, x_middle + Delta_x)
else:
z_random = random.randint(z_middle - Delta_z - patch_z, z_middle + Delta_z)
y_random = random.randint(y_middle - Delta_y - patch_y, y_middle + Delta_y + patch_y)
x_random = random.randint(x_middle - Delta_x - patch_x, x_middle + Delta_x + patch_x)
# crop patch
crop_z_down = z_random - np.int(self.output_size[0] / 2)
crop_z_up = z_random + np.int(self.output_size[0] / 2)
crop_y_down = y_random - np.int(self.output_size[1] / 2)
crop_y_up = y_random + np.int(self.output_size[1] / 2)
crop_x_down = x_random - np.int(self.output_size[2] / 2)
crop_x_up = x_random + np.int(self.output_size[2] / 2)
if crop_z_down < 0 or crop_z_up > image.shape[0]:
delta_z = np.maximum(np.abs(crop_z_down), np.abs(crop_z_up - image.shape[0]))
image = np.pad(image, ((delta_z, delta_z), (0, 0), (0, 0)), 'constant', constant_values=min_value)
label = np.pad(label, ((delta_z, delta_z), (0, 0), (0, 0)), 'constant', constant_values=0.0)
crop_z_down = crop_z_down + delta_z
crop_z_up = crop_z_up + delta_z
if crop_y_down < 0 or crop_y_up > image.shape[1]:
delta_y = np.maximum(np.abs(crop_y_down), np.abs(crop_y_up - image.shape[1]))
image = np.pad(image, ((0, 0), (delta_y, delta_y), (0, 0)), 'constant', constant_values=min_value)
label = np.pad(label, ((0, 0), (delta_y, delta_y), (0, 0)), 'constant', constant_values=0.0)
crop_y_down = crop_y_down + delta_y
crop_y_up = crop_y_up + delta_y
if crop_x_down < 0 or crop_x_up > image.shape[2]:
delta_x = np.maximum(np.abs(crop_x_down), np.abs(crop_x_up - image.shape[2]))
image = np.pad(image, ((0, 0), (0, 0), (delta_x, delta_x)), 'constant', constant_values=min_value)
label = np.pad(label, ((0, 0), (0, 0), (delta_x, delta_x)), 'constant', constant_values=0.0)
crop_x_down = crop_x_down + delta_x
crop_x_up = crop_x_up + delta_x
label = label[crop_z_down: crop_z_up, crop_y_down: crop_y_up, crop_x_down: crop_x_up]
image = image[crop_z_down: crop_z_up, crop_y_down: crop_y_up, crop_x_down: crop_x_up]
label = np.round(label)
# data augmentation
if self.mode == 'train':
if random.random() > 0.5:
image, label = random_rotate(image, label, min_value)
label = np.round(label)
if random.random() > 0.5:
image = intensity_shift(image)
if random.random() > 0.5:
image = intensity_scale(image)
image = torch.from_numpy(image.astype(np.float)).unsqueeze(0).float()
label = torch.from_numpy(label.astype(np.float32)).float()
sample = {'image': image, 'label': label.long()}
return sample
class LAA_dataset(Dataset):
def __init__(self, base_dir, list_dir, split, num_classes, transform=None):
self.transform = transform
self.split = split
self.sample_list = open(os.path.join(list_dir, self.split + '.txt')).readlines()
self.data_dir = base_dir
self.num_classes = num_classes
def __len__(self):
return len(self.sample_list)
def __getitem__(self, idx):
if self.split == "train":
slice_name = self.sample_list[idx].strip('\n')
img_path = os.path.join(self.data_dir + '/LAA_norm_image', slice_name)
image = sitk.ReadImage(img_path)
label_path = os.path.join(self.data_dir + '/LAA_label', slice_name)
label = sitk.ReadImage(label_path)
origin = np.array(image.GetOrigin())
spacing = np.array(image.GetSpacing())
image = sitk.GetArrayFromImage(image)
label = sitk.GetArrayFromImage(label)
elif self.split == "val":
slice_name = self.sample_list[idx].strip('\n')
img_path = os.path.join(self.data_dir + '/LAA_norm_image', slice_name)
image = sitk.ReadImage(img_path)
label_path = os.path.join(self.data_dir + '/LAA_label', slice_name)
label = sitk.ReadImage(label_path)
origin = np.array(image.GetOrigin())
spacing = np.array(image.GetSpacing())
image = sitk.GetArrayFromImage(image)
label = sitk.GetArrayFromImage(label)
else:
slice_name = self.sample_list[idx].strip('\n')
img_path = os.path.join(self.data_dir + '/LAA_norm_image', slice_name)
image = sitk.ReadImage(img_path)
label_path = os.path.join(self.data_dir + '/LAA_label', slice_name)
label = sitk.ReadImage(label_path)
origin = np.array(image.GetOrigin())
spacing = np.array(image.GetSpacing())
image = sitk.GetArrayFromImage(image).astype(np.float)
label = sitk.GetArrayFromImage(label).astype(np.int)
label[label < 0.5] = 0.0 # maybe some voxels is a minus value
label[label > 2.5] = 0.0
sample = {'image': image, 'label': label}
if self.transform:
sample = self.transform(sample)
sample['case_name'] = self.sample_list[idx].strip('\n')
sample['origin'] = origin
sample['spacing'] = spacing
return sample