-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtransforms.py
404 lines (293 loc) · 12 KB
/
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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import random
import math
import numbers
import cv2
import numpy as np
import torch
class Compose:
"""Composes several transforms together.
Args:
transforms(list of 'Transform' object): list of transforms to compose
"""
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, img):
for trans in self.transforms:
img = trans(img)
return img
def __repr__(self):
format_string = self.__class__.__name__ + '('
for t in self.transforms:
format_string += '\n'
format_string += ' {0}'.format(t)
format_string += '\n)'
return format_string
class ToCVImage:
"""Convert an Opencv image to a 3 channel uint8 image
"""
def __call__(self, image):
"""
Args:
image (numpy array): Image to be converted to 32-bit floating point
Returns:
image (numpy array): Converted Image
"""
if len(image.shape) == 2:
image = cv2.cvtColor(iamge, cv2.COLOR_GRAY2BGR)
image = image.astype('uint8')
return image
class RandomResizedCrop:
"""Randomly crop a rectangle region whose aspect ratio is randomly sampled
in [3/4, 4/3] and area randomly sampled in [8%, 100%], then resize the cropped
region into a 224-by-224 square image.
Args:
size: expected output size of each edge
scale: range of size of the origin size cropped
ratio: range of aspect ratio of the origin aspect ratio cropped (w / h)
interpolation: Default: cv2.INTER_LINEAR:
"""
def __init__(self, size, scale=(0.08, 1.0), ratio=(3.0 / 4.0, 4.0 / 3.0), interpolation='linear'):
self.methods={
"area":cv2.INTER_AREA,
"nearest":cv2.INTER_NEAREST,
"linear" : cv2.INTER_LINEAR,
"cubic" : cv2.INTER_CUBIC,
"lanczos4" : cv2.INTER_LANCZOS4
}
self.size = (size, size)
self.interpolation = self.methods[interpolation]
self.scale = scale
self.ratio = ratio
def __call__(self, img):
h, w, _ = img.shape
area = w * h
for attempt in range(10):
target_area = random.uniform(*self.scale) * area
target_ratio = random.uniform(*self.ratio)
output_h = int(round(math.sqrt(target_area * target_ratio)))
output_w = int(round(math.sqrt(target_area / target_ratio)))
if random.random() < 0.5:
output_w, output_h = output_h, output_w
if output_w <= w and output_h <= h:
topleft_x = random.randint(0, w - output_w)
topleft_y = random.randint(0, h - output_h)
break
if output_w > w or output_h > h:
output_w = min(w, h)
output_h = output_w
topleft_x = random.randint(0, w - output_w)
topleft_y = random.randint(0, h - output_w)
cropped = img[topleft_y : topleft_y + output_h, topleft_x : topleft_x + output_w]
resized = cv2.resize(cropped, self.size, interpolation=self.interpolation)
return resized
def __repr__(self):
for name, inter in self.methods.items():
if inter == self.interpolation:
inter_name = name
interpolate_str = inter_name
format_str = self.__class__.__name__ + '(size={0}'.format(self.size)
format_str += ', scale={0}'.format(tuple(round(s, 4) for s in self.scale))
format_str += ', ratio={0}'.format(tuple(round(r, 4) for r in self.ratio))
format_str += ', interpolation={0})'.format(interpolate_str)
return format_str
class RandomHorizontalFlip:
"""Horizontally flip the given opencv image with given probability p.
Args:
p: probability of the image being flipped
"""
def __init__(self, p=0.5):
self.p = p
def __call__(self, img):
"""
Args:
the image to be flipped
Returns:
flipped image
"""
if random.random() < self.p:
img = cv2.flip(img, 1)
return img
class ColorJitter:
"""Randomly change the brightness, contrast and saturation of an image
Args:
brightness: (float or tuple of float(min, max)): how much to jitter
brightness, brightness_factor is choosen uniformly from[max(0, 1-brightness),
1 + brightness] or the given [min, max], Should be non negative numbe
contrast: same as brightness
saturation: same as birghtness
hue: same as brightness
"""
def __init__(self, brightness=0, contrast=0, saturation=0, hue=0):
self.brightness = self._check_input(brightness)
self.contrast = self._check_input(contrast)
self.saturation = self._check_input(saturation)
self.hue = self._check_input(hue)
def _check_input(self, value):
if isinstance(value, numbers.Number):
assert value >= 0, 'value should be non negative'
value = [max(0, 1 - value), 1 + value]
elif isinstance(value, (list, tuple)):
assert len(value) == 2, 'brightness should be a tuple/list with 2 elements'
assert 0 <= value[0] <= value[1], 'max should be larger than or equal to min,\
and both larger than 0'
else:
raise TypeError('need to pass int, float, list or tuple, instead got{}'.format(type(value).__name__))
return value
def __call__(self, img):
"""
Args:
img to be jittered
Returns:
jittered img
"""
img_dtype = img.dtype
h_factor = random.uniform(*self.hue)
b_factor = random.uniform(*self.brightness)
s_factor = random.uniform(*self.saturation)
c_factor = random.uniform(*self.contrast)
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
img = img.astype('float32')
#h
img[:, :, 0] *= h_factor
img[:, :, 0] = np.clip(img[:, :, 0], 0, 179)
#s
img[:, :, 1] *= s_factor
img[:, :, 1] = np.clip(img[:, :, 1], 0, 255)
#v
img[:, :, 2] *= b_factor
img[:, :, 2] = np.clip(img[:, :, 2], 0, 255)
img = img.astype(img_dtype)
img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
#c
img = img * c_factor
img = img.astype(img_dtype)
img = np.clip(img, 0, 255)
return img
class ToTensor:
"""convert an opencv image (h, w, c) ndarray range from 0 to 255 to a pytorch
float tensor (c, h, w) ranged from 0 to 1
"""
def __call__(self, img):
"""
Args:
a numpy array (h, w, c) range from [0, 255]
Returns:
a pytorch tensor
"""
#convert format H W C to C H W
img = img.transpose(2, 0, 1)
img = torch.from_numpy(img)
img = img.float() / 255.0
return img
class Normalize:
"""Normalize a torch tensor (H, W, BGR order) with mean and standard deviation
for each channel in torch tensor:
``input[channel] = (input[channel] - mean[channel]) / std[channel]``
Args:
mean: sequence of means for each channel
std: sequence of stds for each channel
"""
def __init__(self, mean, std, inplace=False):
self.mean = mean
self.std = std
self.inplace = inplace
def __call__(self, img):
"""
Args:
(H W C) format numpy array range from [0, 255]
Returns:
(H W C) format numpy array in float32 range from [0, 1]
"""
assert torch.is_tensor(img) and img.ndimension() == 3, 'not an image tensor'
if not self.inplace:
img = img.clone()
mean = torch.tensor(self.mean, dtype=torch.float32)
std = torch.tensor(self.std, dtype=torch.float32)
img.sub_(mean[:, None, None]).div_(std[:, None, None])
return img
class CenterCrop:
"""resize each image’s shorter edge to r pixels while keeping its aspect ratio.
Next, we crop out the cropped region in the center
Args:
resized: resize image' shorter edge to resized pixels while keeping the aspect ratio
cropped: output image size(h, w), if cropped is an int, then output cropped * cropped size
image
"""
def __init__(self, cropped, resized=256, interpolation='linear'):
methods = {
"area":cv2.INTER_AREA,
"nearest":cv2.INTER_NEAREST,
"linear" : cv2.INTER_LINEAR,
"cubic" : cv2.INTER_CUBIC,
"lanczos4" : cv2.INTER_LANCZOS4
}
self.interpolation = methods[interpolation]
self.resized = resized
if isinstance(cropped, numbers.Number):
cropped = (cropped, cropped)
self.cropped = cropped
def __call__(self, img):
shorter = min(*img.shape[:2])
scaler = float(self.resized) / shorter
img = cv2.resize(img, (0, 0), fx=scaler, fy=scaler, interpolation=self.interpolation)
h, w, _ = img.shape
topleft_x = int((w - self.cropped[1]) / 2)
topleft_y = int((h - self.cropped[0]) / 2)
center_cropped = img[topleft_y : topleft_y + self.cropped[0],
topleft_x : topleft_x + self.cropped[1]]
return center_cropped
class RandomErasing:
"""Random erasing the an rectangle region in Image.
Class that performs Random Erasing in Random Erasing Data Augmentation by Zhong et al.
Args:
sl: min erasing area region
sh: max erasing area region
r1: min aspect ratio range of earsing region
p: probability of performing random erasing
"""
def __init__(self, p=0.5, sl=0.02, sh=0.4, r1=0.3):
self.p = p
self.s = (sl, sh)
self.r = (r1, 1/r1)
def __call__(self, img):
"""
perform random erasing
Args:
img: opencv numpy array in form of [w, h, c] range
from [0, 255]
Returns:
erased img
"""
assert len(img.shape) == 3, 'image should be a 3 dimension numpy array'
if random.random() > self.p:
return img
else:
while True:
Se = random.uniform(*self.s) * img.shape[0] * img.shape[1]
re = random.uniform(*self.r)
He = int(round(math.sqrt(Se * re)))
We = int(round(math.sqrt(Se / re)))
xe = random.randint(0, img.shape[1])
ye = random.randint(0, img.shape[0])
if xe + We <= img.shape[1] and ye + He <= img.shape[0]:
img[ye : ye + He, xe : xe + We, :] = np.random.randint(low=0, high=255, size=(He, We, img.shape[2]))
return img
class CutOut:
"""Randomly mask out one or more patches from an image. An image
is a opencv format image (h,w,c numpy array)
Args:
n_holes (int): Number of patches to cut out of each image.
length (int): The length (in pixels) of each square patch.
"""
def __init__(self, length, n_holes=1):
self.n_holes = n_holes
self.length = length
def __call__(self, img):
while self.n_holes:
y = random.randint(0, img.shape[0] - 1)
x = random.randint(0, img.shape[1] - 1)
tl_x = int(max(0, x - self.length / 2))
tl_y = int(max(0, y - self.length / 2))
img[tl_y : tl_y + self.length, tl_x : tl_x + self.length, :] = 0
self.n_holes -= 1
return img