-
Notifications
You must be signed in to change notification settings - Fork 6
/
transforms3ds.py
353 lines (266 loc) · 11.1 KB
/
transforms3ds.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import torch
import math
import sys
import random
from PIL import Image
try:
import accimage
except ImportError:
accimage = None
import numpy as np
import numbers
import types
import collections
import warnings
from torchvision.transforms import functional as F
from torchvision import transforms
from collections import Iterable
class MTResize(object):
def __init__(self, new_shape, interpolation=Image.BILINEAR, labeled=True):
self.shape = new_shape
self.interpolation = interpolation
self.labeled = labeled
def __call__(self, sample):
rdict = {}
input_data = sample['input']
input_metadata = sample['input_metadata']
input_data = input_data.resize(self.shape, resample=self.interpolation)
rdict['input'] = input_data
if self.labeled:
gt_data = sample['gt']
gt_metadata = sample['gt_metadata']
gt_data = gt_data.resize(self.shape, resample=self.interpolation)
np_gt_data = np.array(gt_data)
np_gt_data[np_gt_data >= 0.5] = 1.0
np_gt_data[np_gt_data < 0.5] = 0.0
gt_data = Image.fromarray(np_gt_data, mode='F')
rdict['gt'] = gt_data
sample.update(rdict)
return sample
class ToPILImage3D(object):
def __call__(self, pic):
pil_list = []
for im in pic:
pil_list.append(Image.fromarray(im.numpy()))
return pil_list
def __repr__(self):
format_string = self.__class__.__name__ + '('
if self.mode is not None:
format_string += 'mode={0}'.format(self.mode)
format_string += ')'
return format_string
class ToTensor3D(object):
def __call__(self, pic):
for i, im in enumerate(pic):
pic[i] = F.to_tensor(im)
return torch.stack(pic)
def __repr__(self):
format_string = self.__class__.__name__ + '('
if self.mode is not None:
format_string += 'mode={0}'.format(self.mode)
format_string += ')'
return format_string
class MTNormalize(object):
def __init__(self, proc='min_max'):
# proc: z_score and min_max
self.proc = proc
def __call__(self, tensor_dict):
if self.proc == 'z_score':
mean, std = torch.mean(tensor_dict['input']), torch.std(tensor_dict['input'])
tensor_dict['input'] -= mean
tensor_dict['input'] /= std+1e-8
elif self.proc == 'min_max':
nmin, nmax = torch.min(tensor_dict['input']), torch.max(tensor_dict['input'])
inp = (tensor_dict['input'] - nmin) / (nmax-nmin+1e-8)
tensor_dict['input'] = inp
return tensor_dict
def __repr__(self):
return self.__class__.__name__ + '(mean={0}, std={1})'.format(self.mean, self.std)
class IndividualNormalize3D(object):
def __init__(self, proc='min_max'):
# proc: z_score and min_max
self.proc = proc
def __call__(self, blk):
if self.proc == 'z_score':
for i, b in enumerate(blk):
mean, std = torch.mean(b), torch.std(b)
blk[i] = (b - mean)/(std+1e-8)
elif self.proc == 'min_max':
for i, b in enumerate(blk):
nmin, nmax = torch.min(b), torch.max(b)
blk[i] = (b - nmin) / (nmax-nmin+1e-8)
blk = blk.squeeze(2)
return blk
def __repr__(self):
return self.__class__.__name__ + '(mean={0}, std={1})'.format(self.mean, self.std)
class Normalize3D(object):
def __init__(self, proc='min_max'):
# proc: z_score and min_max
self.proc = proc
def __call__(self, blk):
if self.proc == 'z_score':
mean, std = torch.mean(blk), torch.std(blk)
blk = (blk - mean)/(std+1e-8)
elif self.proc == 'min_max':
nmin, nmax = torch.min(blk), torch.max(blk)
blk = (blk - nmin) / (nmax-nmin+1e-8)
return blk
def __repr__(self):
return self.__class__.__name__ + '(mean={0}, std={1})'.format(self.mean, self.std)
class Resize3D(object):
def __init__(self, size, interpolation=Image.BILINEAR):
assert isinstance(size, int) or (isinstance(size, Iterable) and len(size) == 2)
self.size = size
self.interpolation = interpolation
def __call__(self, img):
for i, im in enumerate(img):
img[i] = F.resize(im, self.size, self.interpolation)
return img
def __repr__(self):
interpolate_str = _pil_interpolation_to_str[self.interpolation]
return self.__class__.__name__ + '(size={0}, interpolation={1})'.format(self.size, interpolate_str)
class Pad3D(object):
def __init__(self, padding, fill=0, padding_mode='constant'):
assert isinstance(padding, (numbers.Number, tuple))
assert isinstance(fill, (numbers.Number, str, tuple))
assert padding_mode in ['constant', 'edge', 'reflect', 'symmetric']
if isinstance(padding, Sequence) and len(padding) not in [2, 4]:
raise ValueError("Padding must be an int or a 2, or 4 element tuple, not a " +
"{} element tuple".format(len(padding)))
self.padding = padding
self.fill = fill
self.padding_mode = padding_mode
def __call__(self, img):
for im in img:
F.pad(im, self.padding, self.fill, self.padding_mode, inplace=True)
return img
def __repr__(self):
return self.__class__.__name__ + '(padding={0}, fill={1}, padding_mode={2})'.\
format(self.padding, self.fill, self.padding_mode)
class RandomHorizontalFlip3D(object):
def __init__(self, p=0.5):
self.p = p
def __call__(self, img):
if random.random() < self.p:
for i, im in enumerate(img):
img[i] = F.hflip(im)
return img
def __repr__(self):
return self.__class__.__name__ + '(p={})'.format(self.p)
class RandomVerticalFlip3D(object):
def __init__(self, p=0.5):
self.p = p
def __call__(self, img):
if random.random() < self.p:
for i, im in enumerate(img):
img[i] = F.vflip(im)
return img
def __repr__(self):
return self.__class__.__name__ + '(p={})'.format(self.p)
class RandomRotation3D(object):
def __init__(self, degrees, resample=False, expand=False, center=None):
if isinstance(degrees, numbers.Number):
if degrees < 0:
raise ValueError("If degrees is a single number, it must be positive.")
self.degrees = (-degrees, degrees)
else:
if len(degrees) != 2:
raise ValueError("If degrees is a sequence, it must be of len 2.")
self.degrees = degrees
self.resample = resample
self.expand = expand
self.center = center
@staticmethod
def get_params(degrees):
angle = random.uniform(degrees[0], degrees[1])
return angle
def __call__(self, img):
angle = self.get_params(self.degrees)
for i, im in enumerate(img):
img[i] = F.rotate(im, angle, self.resample, self.expand, self.center)
return img
def __repr__(self):
format_string = self.__class__.__name__ + '(degrees={0}'.format(self.degrees)
format_string += ', resample={0}'.format(self.resample)
format_string += ', expand={0}'.format(self.expand)
if self.center is not None:
format_string += ', center={0}'.format(self.center)
format_string += ')'
return format_string
class RandomAffine3D(object):
def __init__(self, degrees, translate=None, scale=None, shear=None, resample=False, fillcolor=0):
if isinstance(degrees, numbers.Number):
if degrees < 0:
raise ValueError("If degrees is a single number, it must be positive.")
self.degrees = (-degrees, degrees)
else:
assert isinstance(degrees, (tuple, list)) and len(degrees) == 2, \
"degrees should be a list or tuple and it must be of length 2."
self.degrees = degrees
if translate is not None:
assert isinstance(translate, (tuple, list)) and len(translate) == 2, \
"translate should be a list or tuple and it must be of length 2."
for t in translate:
if not (0.0 <= t <= 1.0):
raise ValueError("translation values should be between 0 and 1")
self.translate = translate
if scale is not None:
assert isinstance(scale, (tuple, list)) and len(scale) == 2, \
"scale should be a list or tuple and it must be of length 2."
for s in scale:
if s <= 0:
raise ValueError("scale values should be positive")
self.scale = scale
if shear is not None:
if isinstance(shear, numbers.Number):
if shear < 0:
raise ValueError("If shear is a single number, it must be positive.")
self.shear = (-shear, shear)
else:
assert isinstance(shear, (tuple, list)) and len(shear) == 2, \
"shear should be a list or tuple and it must be of length 2."
self.shear = shear
else:
self.shear = shear
self.resample = resample
self.fillcolor = fillcolor
@staticmethod
def get_params(degrees, translate, scale_ranges, shears, img_size):
angle = random.uniform(degrees[0], degrees[1])
if translate is not None:
max_dx = translate[0] * img_size[0]
max_dy = translate[1] * img_size[1]
translations = (np.round(random.uniform(-max_dx, max_dx)),
np.round(random.uniform(-max_dy, max_dy)))
else:
translations = (0, 0)
if scale_ranges is not None:
scale = random.uniform(scale_ranges[0], scale_ranges[1])
else:
scale = 1.0
if shears is not None:
shear = random.uniform(shears[0], shears[1])
else:
shear = 0.0
return angle, translations, scale, shear
def __call__(self, img):
ret = self.get_params(self.degrees, self.translate, self.scale, self.shear, img.size)
for im in img:
F.affine(im, *ret, resample=self.resample, fillcolor=self.fillcolor, inplace=True)
return img
def __repr__(self):
s = '{name}(degrees={degrees}'
if self.translate is not None:
s += ', translate={translate}'
if self.scale is not None:
s += ', scale={scale}'
if self.shear is not None:
s += ', shear={shear}'
if self.resample > 0:
s += ', resample={resample}'
if self.fillcolor != 0:
s += ', fillcolor={fillcolor}'
s += ')'
d = dict(self.__dict__)
d['resample'] = _pil_interpolation_to_str[d['resample']]
return s.format(name=self.__class__.__name__, **d)