-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandaugment.py
140 lines (98 loc) · 3.04 KB
/
randaugment.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
# copyright: https://github.com/ildoonet/pytorch-randaugment
# code in this file is adpated from rpmcruz/autoaugment
# https://github.com/rpmcruz/autoaugment/blob/master/transformations.py
# This code is modified version of one of ildoonet, for randaugmentation of fixmatch.
import random
import PIL, PIL.ImageOps, PIL.ImageEnhance, PIL.ImageDraw
import numpy as np
from PIL import Image
def AutoContrast(img, _):
return PIL.ImageOps.autocontrast(img)
def Brightness(img, v):
assert v >= 0.0
return PIL.ImageEnhance.Brightness(img).enhance(v)
def Color(img, v):
assert v >= 0.0
return PIL.ImageEnhance.Color(img).enhance(v)
def Contrast(img, v):
assert v >= 0.0
return PIL.ImageEnhance.Contrast(img).enhance(v)
def Equalize(img, _):
return PIL.ImageOps.equalize(img)
def Identity(img, v):
return img
def Posterize(img, v):
v = int(v)
v = max(1, v)
return PIL.ImageOps.posterize(img, v)
def Rotate(img, v):
return img.rotate(v)
def Sharpness(img, v):
assert v >= 0.0
return PIL.ImageEnhance.Sharpness(img).enhance(v)
def ShearX(img, v):
return img.transform(img.size, PIL.Image.AFFINE, (1, v, 0, 0, 1, 0))
def ShearY(img, v):
return img.transform(img.size, PIL.Image.AFFINE, (1, 0, 0, v, 1, 0))
def TranslateX(img, v):
v = v * img.size[0]
return img.transform(img.size, PIL.Image.AFFINE, (1, 0, v, 0, 1, 0))
def TranslateY(img, v):
v = v * img.size[1]
return img.transform(img.size, PIL.Image.AFFINE, (1, 0, 0, 0, 1, v))
def Solarize(img, v):
assert 0 <= v <= 256
return PIL.ImageOps.solarize(img, v)
def Cutout(img, v):
assert 0.0 <= v <= 0.5
if v <= 0.:
return img
v = v * img.size[0]
return CutoutAbs(img, v)
def CutoutAbs(img, v):
if v < 0:
return img
w, h = img.size
x0 = np.random.uniform(w)
y0 = np.random.uniform(h)
x0 = int(max(0, x0 - v / 2.))
y0 = int(max(0, y0 - v / 2.))
x1 = min(w, x0 + v)
y1 = min(h, y0 + v)
xy = (x0, y0, x1, y1)
color = (125, 123, 114)
img = img.copy()
PIL.ImageDraw.Draw(img).rectangle(xy, color)
return img
def augment_list():
l = [
(AutoContrast, 0, 1),
(Brightness, 0.05, 0.95),
(Color, 0.05, 0.95),
(Contrast, 0.05, 0.95),
(Equalize, 0, 1),
(Identity, 0, 1),
(Posterize, 4, 8),
(Rotate, -30, 30),
(Sharpness, 0.05, 0.95),
(ShearX, -0.3, 0.3),
(ShearY, -0.3, 0.3),
(Solarize, 0, 256),
(TranslateX, -0.3, 0.3),
(TranslateY, -0.3, 0.3)
]
return l
class RandAugment:
def __init__(self, n, m):
self.n = n
self.m = m
self.augment_list = augment_list()
def __call__(self, img):
ops = random.choices(self.augment_list, k=self.n)
for op, min_val, max_val in ops:
val = min_val + float(max_val - min_val) * random.random()
img = op(img, val)
cutout_val = random.random() * 0.5
# cutout for fixmatch
img = Cutout(img, cutout_val)
return img