forked from kenshohara/3D-ResNets-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spatial_transforms.py
215 lines (150 loc) · 5.44 KB
/
spatial_transforms.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
import random
from torchvision.transforms import transforms
from torchvision.transforms import functional as F
from PIL import Image
class Compose(transforms.Compose):
def randomize_parameters(self):
for t in self.transforms:
t.randomize_parameters()
class ToTensor(transforms.ToTensor):
def randomize_parameters(self):
pass
class Normalize(transforms.Normalize):
def randomize_parameters(self):
pass
class ScaleValue(object):
def __init__(self, s):
self.s = s
def __call__(self, tensor):
tensor *= self.s
return tensor
def randomize_parameters(self):
pass
class Resize(transforms.Resize):
def randomize_parameters(self):
pass
class Scale(transforms.Scale):
def randomize_parameters(self):
pass
class CenterCrop(transforms.CenterCrop):
def randomize_parameters(self):
pass
class CornerCrop(object):
def __init__(self,
size,
crop_position=None,
crop_positions=['c', 'tl', 'tr', 'bl', 'br']):
self.size = size
self.crop_position = crop_position
self.crop_positions = crop_positions
if crop_position is None:
self.randomize = True
else:
self.randomize = False
self.randomize_parameters()
def __call__(self, img):
image_width = img.size[0]
image_height = img.size[1]
h, w = (self.size, self.size)
if self.crop_position == 'c':
i = int(round((image_height - h) / 2.))
j = int(round((image_width - w) / 2.))
elif self.crop_position == 'tl':
i = 0
j = 0
elif self.crop_position == 'tr':
i = 0
j = image_width - self.size
elif self.crop_position == 'bl':
i = image_height - self.size
j = 0
elif self.crop_position == 'br':
i = image_height - self.size
j = image_width - self.size
img = F.crop(img, i, j, h, w)
return img
def randomize_parameters(self):
if self.randomize:
self.crop_position = self.crop_positions[random.randint(
0,
len(self.crop_positions) - 1)]
def __repr__(self):
return self.__class__.__name__ + '(size={0}, crop_position={1}, randomize={2})'.format(
self.size, self.crop_position, self.randomize)
class RandomHorizontalFlip(transforms.RandomHorizontalFlip):
def __init__(self, p=0.5):
super().__init__(p)
self.randomize_parameters()
def __call__(self, img):
"""
Args:
img (PIL.Image): Image to be flipped.
Returns:
PIL.Image: Randomly flipped image.
"""
if self.random_p < self.p:
return F.hflip(img)
return img
def randomize_parameters(self):
self.random_p = random.random()
class MultiScaleCornerCrop(object):
def __init__(self,
size,
scales,
crop_positions=['c', 'tl', 'tr', 'bl', 'br'],
interpolation=Image.BILINEAR):
self.size = size
self.scales = scales
self.interpolation = interpolation
self.crop_positions = crop_positions
self.randomize_parameters()
def __call__(self, img):
short_side = min(img.size[0], img.size[1])
crop_size = int(short_side * self.scale)
self.corner_crop.size = crop_size
img = self.corner_crop(img)
return img.resize((self.size, self.size), self.interpolation)
def randomize_parameters(self):
self.scale = self.scales[random.randint(0, len(self.scales) - 1)]
crop_position = self.crop_positions[random.randint(
0,
len(self.crop_positions) - 1)]
self.corner_crop = CornerCrop(None, crop_position)
def __repr__(self):
return self.__class__.__name__ + '(size={0}, scales={1}, interpolation={2})'.format(
self.size, self.scales, self.interpolation)
class RandomResizedCrop(transforms.RandomResizedCrop):
def __init__(self,
size,
scale=(0.08, 1.0),
ratio=(3. / 4., 4. / 3.),
interpolation=Image.BILINEAR):
super().__init__(size, scale, ratio, interpolation)
self.randomize_parameters()
def __call__(self, img):
if self.randomize:
self.random_crop = self.get_params(img, self.scale, self.ratio)
self.randomize = False
i, j, h, w = self.random_crop
return F.resized_crop(img, i, j, h, w, self.size, self.interpolation)
def randomize_parameters(self):
self.randomize = True
class ColorJitter(transforms.ColorJitter):
def __init__(self, brightness=0, contrast=0, saturation=0, hue=0):
super().__init__(brightness, contrast, saturation, hue)
self.randomize_parameters()
def __call__(self, img):
if self.randomize:
self.transform = self.get_params(self.brightness, self.contrast,
self.saturation, self.hue)
self.randomize = False
return self.transform(img)
def randomize_parameters(self):
self.randomize = True
class PickFirstChannels(object):
def __init__(self, n):
self.n = n
def __call__(self, tensor):
return tensor[:self.n, :, :]
def randomize_parameters(self):
pass