-
Notifications
You must be signed in to change notification settings - Fork 34
/
transformations.py
317 lines (260 loc) · 10.5 KB
/
transformations.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
import numpy as np
import random
def extract_section(im, x, y, z, padding, section_shape):
x_sect, y_sect, z_sect = section_shape
size_x, size_y, size_z = im.shape
take_x = x_sect + padding
take_y = y_sect + padding
take_z = z_sect + padding
if x - (take_x / 2) < 0:
sl_x = slice(0, take_x)
elif x + (take_x / 2) > size_x:
sl_x = slice(size_x - take_x, size_x)
else:
sl_x = slice(x - (take_x / 2), x + (take_x / 2))
if y - (take_y / 2) < 0:
sl_y = slice(0, take_y)
elif y + (take_y / 2) > size_y:
sl_y = slice(size_y - take_y, size_y)
else:
sl_y = slice(y - (take_y / 2), y + (take_y / 2))
if z - (take_z / 2) < 0:
sl_z = slice(0, take_z)
elif z + (take_z / 2) > size_z:
sl_z = slice(size_z - take_z, size_z)
else:
sl_z = slice(z - (take_z / 2), z + (take_z / 2))
return im[sl_x, sl_y, sl_z]
def random_brain_points(gt):
whole = np.array(np.where(gt != 0)).T
core = np.array(np.where((gt != 0) & (gt != 2))).T
active = np.array(np.where(gt == 4)).T
healthy = np.array(np.where(gt == 0)).T
region_candidates = [whole, core, active, healthy, healthy]
regions = []
for candidate in region_candidates:
if len(candidate) > 0:
regions.append(candidate)
if len(regions) == 0:
raise ValueError('Ground truth does not make sense.')
reg = random.choice(regions)
i = np.random.randint(0, len(reg))
point = reg[i]
return point
def random_hand_points(gt):
bone = np.array(np.where(gt > 0)).T
center = np.array([int(i) for i in np.round(np.mean(bone, axis=0))])
offset = np.random.randint(-25, 25, size=(3,))
point = center + offset
# region_candidates = [metacarpal, proximal, middle]
# regions = []
# for candidate in region_candidates:
# if len(candidate) > 0:
# regions.append(candidate)
#
# if len(regions) == 0:
# raise ValueError('Ground truth does not make sense.')
#
# reg = random.choice(regions)
# i = np.random.randint(0, len(reg))
# point = reg[i]
return point
def extract_random_section(image, gt, random_point_selection, section_shape):
point = random_point_selection(gt.argmax(axis=0))
x, y, z = point
sections = [extract_section(modality, x, y, z, padding=0, section_shape=section_shape) for modality in image]
gt_sections = [extract_section(class_map, x, y, z, padding=0, section_shape=section_shape) for class_map in gt]
return np.array(sections, dtype='int16'), np.array(gt_sections, dtype='int8')
class RandomSectionSelection(object):
def __init__(self, data_mode, followed_by=None):
if data_mode == 'brain':
self.random_point_selection = random_brain_points
self.section_shape = (80, 72, 64)
elif data_mode == 'hand':
self.random_point_selection = random_hand_points
self.section_shape = (144, 120, 96)
elif data_mode == 'debug_hand':
self.random_point_selection = random_hand_points
self.section_shape = (64, 64, 64)
elif data_mode == 'debug_brain':
self.random_point_selection = random_brain_points
self.section_shape = (32, 32, 32)
else:
raise ValueError('Data modes are: hand, brain.')
self.data_mode = data_mode
self.followed_by = followed_by
self.__name__ = 'RandomSectionSelection'
def __call__(self, x, z):
n_classes = z.shape[-1]
nx = np.transpose(x, (0, 2, 3, 4, 1))
nz = np.reshape(z, (1, x.shape[3], x.shape[4], x.shape[1], n_classes))
nz = np.transpose(nz, (0, 4, 1, 2, 3))
nx, nz = extract_random_section(nx[0], nz[0], self.random_point_selection, self.section_shape)
nx = np.transpose(nx[np.newaxis], (0, 4, 1, 2, 3))
nz = np.transpose(nz[np.newaxis], (0, 2, 3, 4, 1))
nz = np.reshape(nz, (nz.shape[0], nz.shape[1]*nz.shape[2]*nz.shape[3], n_classes))
if self.followed_by is None:
return (nx, nz)
else:
return self.followed_by(nx, nz)
def random_flip(x, z):
flip_axis = random.choice([0, 1, 2])
original_shape = z.shape
nz = np.reshape(z, (1, x.shape[3], x.shape[4], x.shape[1], z.shape[-1]))
if flip_axis == 0:
# flip along the height
nx = x[:, :, :, ::-1, :]
nz = nz[:, ::-1, :, :, :]
elif flip_axis == 1:
# flip along the width
nx = x[:, :, :, :, ::-1]
nz = nz[:, :, ::-1, :, :]
else:
# flip along the depth
nx = x[:, ::-1, :, :, :]
nz = nz[:, :, :, ::-1, :]
nz = np.reshape(nz, original_shape)
return (nx, nz)
def percentile_filter(x, z):
from scipy.ndimage import percentile_filter
from breze.learn.data import one_hot
percentile = np.random.randint(0, 10)
nx = np.transpose(x, (0, 2, 1, 3, 4))
nx[0] = [percentile_filter(modality, percentile, (2, 2, 2)) for modality in nx[0]]
nx = np.transpose(nx, (0, 2, 1, 3, 4))
n_classes = z.shape[-1]
nz = np.reshape(z, (x.shape[3], x.shape[4], x.shape[1], n_classes))
nz = np.transpose(nz, (3, 0, 1, 2))
nz = np.array([percentile_filter(class_map, percentile, (2, 2, 2)) for class_map in nz])
nz = nz.argmax(axis=0)
nz = np.reshape(nz, (-1,))
nz = np.reshape(one_hot(nz, n_classes), z.shape)
nx = np.asarray(nx, dtype=x.dtype)
nz = np.asarray(nz, dtype=z.dtype)
return (nx, nz)
def swirl_(im, strength, radius):
from skimage.transform import swirl
return [swirl(im_slice, rotation=0, strength=strength, radius=radius) for im_slice in im]
def swirl_transform(x, z):
"""
Adds a swirl effect to every depth slice.
Assuming a batch size of 1.
More specifically: x is (1, depth, channels, height, width) and z is (1, height*width*depth, classes)
"""
from breze.learn.data import one_hot
strength = np.random.uniform(1, 2)
radius = np.random.randint(90, 140)
z_original_shape = z.shape
n_classes = z.shape[-1]
nx = np.transpose(x, (0, 2, 1, 3, 4))
nz = np.reshape(z, (1, x.shape[3], x.shape[4], x.shape[1], n_classes))
nz = np.transpose(nz, (0, 4, 3, 1, 2))
nx[0] = [swirl_(modality, strength, radius) for modality in nx[0]]
nx = np.transpose(nx, (0, 2, 1, 3, 4))
nz[0] = [swirl_(class_map, strength, radius) for class_map in nz[0]]
nz = nz[0].argmax(axis=0)
nz = np.transpose(nz, (1, 2, 0))
nz = np.reshape(nz, (-1,))
nz = np.reshape(one_hot(nz, n_classes), z_original_shape)
nx = np.asarray(nx, dtype=x.dtype)
nz = np.asarray(nz, dtype=z.dtype)
return (nx, nz)
def minor_rotation(x, z):
"""
Assuming a batch size of 1.
More specifically: x is (1, depth, channels, height, width) and z is (1, height*width*depth, classes)
"""
from scipy.ndimage.interpolation import rotate as rotate_scipy
from breze.learn.data import one_hot
z_original_shape = z.shape
n_classes = z.shape[-1]
ang = float(np.random.uniform(-90, 90))
axes = np.random.permutation(3)[:2]
nx = np.transpose(x, (0, 2, 3, 4, 1))
nz = np.reshape(z, (1, x.shape[3], x.shape[4], x.shape[1], n_classes))
nz = np.transpose(nz, (0, 4, 1, 2, 3))
nx[0] = [rotate_scipy(modality, ang, axes=axes, order=3, reshape=False) for modality in nx[0]]
nx = np.transpose(nx, (0, 4, 1, 2, 3))
nz[0] = [rotate_scipy(class_map, ang, axes=axes, order=3, reshape=False) for class_map in nz[0]]
nz = nz[0].argmax(axis=0)
nz = np.reshape(nz, (-1,))
nz = np.reshape(one_hot(nz, n_classes), z_original_shape)
nx = np.asarray(nx, dtype=x.dtype)
nz = np.asarray(nz, dtype=z.dtype)
return (nx, nz)
def full_rotation(x, z):
"""
Assuming a batch size of 1.
More specifically: x is (1, depth, channels, height, width) and z is (1, height*width*depth, classes)
"""
from scipy.ndimage.interpolation import rotate as rotate_scipy
from breze.learn.data import one_hot
z_original_shape = z.shape
n_classes = z.shape[-1]
ang = float(np.random.uniform(0, 360))
axes = np.random.permutation(3)[:2]
nx = np.transpose(x, (0, 2, 3, 4, 1))
nz = np.reshape(z, (1, x.shape[3], x.shape[4], x.shape[1], n_classes))
nz = np.transpose(nz, (0, 4, 1, 2, 3))
nx[0] = [rotate_scipy(modality, ang, axes=axes, order=3, reshape=False) for modality in nx[0]]
nx = np.transpose(nx, (0, 4, 1, 2, 3))
nz[0] = [rotate_scipy(class_map, ang, axes=axes, order=3, reshape=False) for class_map in nz[0]]
nz = nz[0].argmax(axis=0)
nz = np.reshape(nz, (-1,))
nz = np.reshape(one_hot(nz, n_classes), z_original_shape)
nx = np.asarray(nx, dtype=x.dtype)
nz = np.asarray(nz, dtype=z.dtype)
return (nx, nz)
def identity(x, z):
return (x, z)
def nil(x, z):
nx = np.zeros(x.shape)
nz = np.zeros(z.shape)
return (nx, nz)
def random_transformation(x, z):
import random
transformations = ['identity', 'random_flip', 'percentile_filter', 'full_rotation']
transform_dict = {
'identity': identity,
'random_flip': random_flip,
'percentile_filter': percentile_filter,
'full_rotation': full_rotation
}
transform_key = random.choice(transformations)
transform_fun = transform_dict[transform_key]
nx, nz = transform_fun(x, z)
second_transform_key = random.choice(transformations)
if second_transform_key == transform_key:
return (nx, nz)
else:
second_transform_fun = transform_dict[second_transform_key]
return second_transform_fun(nx, nz)
def random_geometric_transformation(x, z):
import random
transformations = ['identity', 'random_flip', 'full_rotation']
transform_dict = {
'identity': identity,
'random_flip': random_flip,
'full_rotation': full_rotation
}
transform_key = random.choice(transformations)
transform_fun = transform_dict[transform_key]
nx, nz = transform_fun(x, z)
second_transform_key = random.choice(transformations)
if second_transform_key == transform_key:
return (nx, nz)
else:
second_transform_fun = transform_dict[second_transform_key]
return second_transform_fun(nx, nz)
def random_soft_geometric_transformation(x, z):
import random
transformations = ['identity', 'random_flip', 'full_rotation']
transform_dict = {
'identity': identity,
'random_flip': random_flip,
'full_rotation': full_rotation
}
transform_key = random.choice(transformations)
transform_fun = transform_dict[transform_key]
nx, nz = transform_fun(x, z)
return (nx, nz)