-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformations.py
More file actions
326 lines (239 loc) · 10.3 KB
/
transformations.py
File metadata and controls
326 lines (239 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
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
import torchvision
import random
from PIL import Image, ImageOps
import numpy as np
import numbers
import torch
import Augmentor
from Augmentor import Operations
'''
Below Class has been referred from
https://github.com/yjxiong/tsn-pytorch/blob/master/transforms.py
'''
class GroupRandomCrop(object):
def __init__(self, size):
if isinstance(size, numbers.Number):
self.size = (int(size), int(size))
else:
self.size = size
def __call__(self, img_group):
w, h = img_group[0].size
th, tw = self.size
out_images = list()
x1 = random.randint(0, w - tw)
y1 = random.randint(0, h - th)
for img in img_group:
assert (img.size[0] == w and img.size[1] == h)
if w == tw and h == th:
out_images.append(img)
else:
out_images.append(img.crop((x1, y1, x1 + tw, y1 + th)))
return out_images
class GroupCenterCrop(object):
def __init__(self, size):
self.worker = torchvision.transforms.CenterCrop(size)
def __call__(self, img_group):
return [self.worker(img) for img in img_group]
class Translate(object):
def __init__(self, probability=0.5, translate=(0.2, 0.2)):
self.affline = torchvision.transforms.RandomAffine(degrees=0, translate=translate,
scale=None, shear=None, resample=False, fillcolor=0)
self.probability = probability
def __call__(self, img_group):
p = random.random()
if p < self.probability:
return [self.affline(img) for img in img_group]
else:
return img_group
class Brightness(object):
def __init__(self, probability=0.5, brightness=0.5):
self.color_jitter = torchvision.transforms.ColorJitter(brightness=brightness)
self.probability = probability
def __call__(self, img_group):
p = random.random()
if p < self.probability:
return [self.color_jitter(img) for img in img_group]
else:
return img_group
class Contrast(object):
def __init__(self, probability=0.5, contrast=0.5):
self.color_jitter = torchvision.transforms.ColorJitter(contrast=contrast)
self.probability = probability
def __call__(self, img_group):
p = random.random()
if p < self.probability:
return [self.color_jitter(img) for img in img_group]
else:
return img_group
class Saturation(object):
def __init__(self, probability=0.25, saturation=0.25):
self.color_jitter = torchvision.transforms.ColorJitter(saturation=saturation)
self.probability = probability
def __call__(self, img_group):
p = random.random()
if p < self.probability:
return [self.color_jitter(img) for img in img_group]
else:
return img_group
class Hue(object):
def __init__(self, probability=0.25, hue=0.25):
self.color_jitter = torchvision.transforms.ColorJitter(hue=hue)
self.probability = probability
def __call__(self, img_group):
p = random.random()
if p < self.probability:
return [self.color_jitter(img) for img in img_group]
else:
return img_group
class ColorJitter(object):
def __init__(self):
self.coljit = torchvision.transforms.ColorJitter(brightness=0.5, contrast=0.75, saturation=0, hue=0.25)
def __call__(self, img_group):
return [self.coljit(img) for img in img_group]
class RandomCrop(object):
def __init__(self):
self.p = Augmentor.Operations.CropRandom(probability=0.5, percentage_area=0.5)
def __call__(self, img_group):
return self.p.perform_operation(img_group)
class Scale(object):
def __init__(self):
self.p = Augmentor.Operations.Scale(probability=0.5, scale_factor=1.2)
def __call__(self, img_group):
return self.p.perform_operation(img_group)
class Rotate(object):
def __init__(self, probability=0.5, max_left_rotation=15, max_right_rotation=15):
self.p = Augmentor.Operations.RotateRange(probability=probability,
max_left_rotation=max_left_rotation,
max_right_rotation=max_right_rotation)
def __call__(self, img_group):
return self.p.perform_operation(img_group)
class Skew(object):
def __init__(self, probability=0.5, skew_type='TILT', magnitude=1.0):
self.p = Augmentor.Operations.Skew(probability=probability, skew_type=skew_type, magnitude=magnitude)
def __call__(self, img_group):
return self.p.perform_operation(img_group)
class Shear(object):
def __init__(self, probability=0.25, max_shear_left=8, max_shear_right=8):
self.p = Augmentor.Operations.Shear(probability=probability, max_shear_left=max_shear_left,
max_shear_right=max_shear_right)
def __call__(self, img_group):
return self.p.perform_operation(img_group)
class GrayScale(object):
def __init__(self, probability=0.05):
self.p = Augmentor.Operations.Greyscale(probability=probability)
def __call__(self, img_group):
return self.p.perform_operation(img_group)
class RandomNoise(object):
def __init__(self):
self.p = Augmentor.Operations.RandomErasing(probability=0.2, rectangle_area=0.5)
def __call__(self, img_group):
return self.p.perform_operation(img_group)
class Noise(object):
def __init__(self, probability=0.05):
self.p = Augmentor.Operations.RandomErasing(probability=1, rectangle_area=0.1)
self.p1 = Augmentor.Operations.GaussianDistortion(probability=1, grid_width=3, grid_height=3, magnitude=5,
corner='bell', method='in', mex=0.5, mey=0.5, sdx=0.05,
sdy=0.05)
self.probability = probability
def __call__(self, img_group):
prob = random.random()
prob2 = random.random()
if prob < self.probability:
if prob2 > 0.5:
return self.p.perform_operation(img_group)
else:
return self.p1.perform_operation(img_group)
else:
return img_group
class GaussianNoise(object):
def __init__(self):
self.p = Augmentor.Operations.GaussianDistortion(probability=1, grid_width=3, grid_height=3, magnitude=5,
corner='bell', method='in', mex=0.5, mey=0.5, sdx=0.05,
sdy=0.05)
def __call__(self, img_group):
return self.p.perform_operation(img_group)
class GroupRandomHorizontalFlip(object):
"""Randomly horizontally flips the given PIL.Image with a probability of 0.5
"""
def __init__(self, is_flow=False):
self.is_flow = is_flow
def __call__(self, img_group, is_flow=False):
v = random.random()
if v < 0.5:
ret = [img.transpose(Image.FLIP_LEFT_RIGHT) for img in img_group]
if self.is_flow:
for i in range(0, len(ret), 2):
ret[i] = ImageOps.invert(ret[i]) # invert flow pixel values when flipping
return ret
else:
return img_group
class GroupNormalizeNumpy(object):
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, input):
rep_mean = self.mean * (input.shape[-1] // len(self.mean))
rep_std = self.std * (input.shape[-1] // len(self.std))
rep_mean = np.array(rep_mean, dtype=np.float32)
rep_std = np.array(rep_std, dtype=np.float32)
normalized = ((input / 255) - rep_mean) / rep_std
# print(input.max(), input.min(), rep_mean.shape)
return normalized
class GroupNormalize(object):
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, tensor):
rep_mean = self.mean * (tensor.size()[0] // len(self.mean))
rep_std = self.std * (tensor.size()[0] // len(self.std))
# TODO: make efficient
for t, m, s in zip(tensor, rep_mean, rep_std):
t.sub_(m).div_(s)
return tensor
'''
Below Class has been referred from
https://github.com/yjxiong/tsn-pytorch/blob/master/transforms.py
'''
class GroupScale(object):
""" Rescales the input PIL.Image to the given 'size'.
'size' will be the size of the smaller edge.
For example, if height > width, then image will be
rescaled to (size * height / width, size)
size: size of the smaller edge
interpolation: Default: PIL.Image.BILINEAR
"""
def __init__(self, size, interpolation=Image.BILINEAR):
self.worker = torchvision.transforms.Resize(size, interpolation)
def __call__(self, img_group):
return [self.worker(img) for img in img_group]
class Stack(object):
def __init__(self, roll=False):
self.roll = roll
def __call__(self, img_group):
if img_group[0].mode == 'L':
return np.concatenate([np.expand_dims(x, 2) for x in img_group], axis=2)
elif img_group[0].mode == 'RGB':
if self.roll:
return np.concatenate([np.array(x)[:, :, ::-1] for x in img_group], axis=2)
else:
return np.concatenate(img_group, axis=2)
class ToTorchFormatTensor(object):
""" Converts a PIL.Image (RGB) or numpy.ndarray (H x W x C) in the range [0, 255]
to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0] """
def __init__(self, div=True):
self.div = div
def __call__(self, pic):
if isinstance(pic, np.ndarray):
# handle numpy array
img = torch.from_numpy(pic).permute(2, 0, 1).contiguous()
else:
# handle PIL Image
img = torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes()))
img = img.view(pic.size[1], pic.size[0], len(pic.mode))
# put it from HWC to CHW format
# yikes, this transpose takes 80% of the loading time/CPU
img = img.transpose(0, 1).transpose(0, 2).contiguous()
return img.float().div(255) if self.div else img.float()
class IdentityTransform(object):
def __call__(self, data):
return data