-
Notifications
You must be signed in to change notification settings - Fork 75
/
p_data_augmentation.py
200 lines (150 loc) · 5.6 KB
/
p_data_augmentation.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
## Additional transforms for PyTorch data augmentation
## It is very recommended to use Pillow-SIMD for speed gain in the 5x range.
## https://python-pillow.org/pillow-perf/
## OpenCV built with IPP and TBB is also fast but inaccurate
import torch
import random
import PIL.ImageEnhance as ie
import PIL.Image as im
class Lighting(object):
"""Lighting noise(AlexNet - style PCA - based noise)"""
def __init__(self, alphastd, eigval, eigvec):
self.alphastd = alphastd
self.eigval = eigval
self.eigvec = eigvec
def __call__(self, img):
if self.alphastd == 0:
return img
alpha = img.new().resize_(3).normal_(0, self.alphastd)
rgb = self.eigvec.type_as(img).clone()\
.mul(alpha.view(1, 3).expand(3, 3))\
.mul(self.eigval.view(1, 3).expand(3, 3))\
.sum(1).squeeze()
return img.add(rgb.view(3, 1, 1).expand_as(img))
class Grayscale(object):
def __call__(self, img):
gs = img.clone()
gs[0].mul_(0.299).add_(0.587, gs[1]).add_(0.114, gs[2])
gs[1].copy_(gs[0])
gs[2].copy_(gs[0])
return gs
class Saturation(object):
def __init__(self, var):
self.var = var
def __call__(self, img):
gs = Grayscale()(img)
alpha = random.uniform(0, self.var)
return img.lerp(gs, alpha)
class Brightness(object):
def __init__(self, var):
self.var = var
def __call__(self, img):
gs = img.new().resize_as_(img).zero_()
alpha = random.uniform(0, self.var)
return img.lerp(gs, alpha)
class Contrast(object):
def __init__(self, var):
self.var = var
def __call__(self, img):
gs = Grayscale()(img)
gs.fill_(gs.mean())
alpha = random.uniform(0, self.var)
return img.lerp(gs, alpha)
class RandomOrder(object):
""" Composes several transforms together in random order.
"""
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, img):
if self.transforms is None:
return img
order = torch.randperm(len(self.transforms))
for i in order:
img = self.transforms[i](img)
return img
class ColorJitter(RandomOrder):
def __init__(self, brightness=0.4, contrast=0.4, saturation=0.4):
self.transforms = []
if brightness != 0:
self.transforms.append(Brightness(brightness))
if contrast != 0:
self.transforms.append(Contrast(contrast))
if saturation != 0:
self.transforms.append(Saturation(saturation))
class RandomFlip(object):
"""Randomly flips the given PIL.Image with a probability of 0.25 horizontal,
0.25 vertical,
0.5 as is
"""
def __call__(self, img):
dispatcher = {
0: img,
1: img,
2: img.transpose(im.FLIP_LEFT_RIGHT),
3: img.transpose(im.FLIP_TOP_BOTTOM)
}
return dispatcher[random.randint(0,3)] #randint is inclusive
class RandomRotate(object):
"""Randomly rotate the given PIL.Image with a probability of 1/6 90°,
1/6 180°,
1/6 270°,
1/2 as is
"""
def __call__(self, img):
dispatcher = {
0: img,
1: img,
2: img,
3: img.transpose(im.ROTATE_90),
4: img.transpose(im.ROTATE_180),
5: img.transpose(im.ROTATE_270)
}
return dispatcher[random.randint(0,5)] #randint is inclusive
class PILColorBalance(object):
def __init__(self, var):
self.var = var
def __call__(self, img):
alpha = random.uniform(1 - self.var, 1 + self.var)
return ie.Color(img).enhance(alpha)
class PILContrast(object):
def __init__(self, var):
self.var = var
def __call__(self, img):
alpha = random.uniform(1 - self.var, 1 + self.var)
return ie.Contrast(img).enhance(alpha)
class PILBrightness(object):
def __init__(self, var):
self.var = var
def __call__(self, img):
alpha = random.uniform(1 - self.var, 1 + self.var)
return ie.Brightness(img).enhance(alpha)
class PILSharpness(object):
def __init__(self, var):
self.var = var
def __call__(self, img):
alpha = random.uniform(1 - self.var, 1 + self.var)
return ie.Sharpness(img).enhance(alpha)
# Check ImageEnhancer effect: https://www.youtube.com/watch?v=_7iDTpTop04
# Not documented but all enhancements can go beyond 1.0 to 2
# Image must be RGB
# Use Pillow-SIMD because Pillow is too slow
class PowerPIL(RandomOrder):
def __init__(self, rotate=True,
flip=True,
colorbalance=0.4,
contrast=0.4,
brightness=0.4,
sharpness=0.4):
self.transforms = []
if rotate:
self.transforms.append(RandomRotate())
if flip:
self.transforms.append(RandomFlip())
if brightness != 0:
self.transforms.append(PILBrightness(brightness))
if contrast != 0:
self.transforms.append(PILContrast(contrast))
if colorbalance != 0:
self.transforms.append(PILColorBalance(colorbalance))
if sharpness != 0:
self.transforms.append(PILSharpness(sharpness))