-
Notifications
You must be signed in to change notification settings - Fork 62
/
augments.py
220 lines (171 loc) · 6.15 KB
/
augments.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
"""
CutBlur
Copyright 2020-present NAVER corp.
MIT license
"""
import numpy as np
import torch
import torch.nn.functional as F
def apply_augment(
im1, im2,
augs, probs, alphas,
aux_prob=None, aux_alpha=None,
mix_p=None
):
idx = np.random.choice(len(augs), p=mix_p)
aug = augs[idx]
prob = float(probs[idx])
alpha = float(alphas[idx])
mask = None
if aug == "none":
im1_aug, im2_aug = im1.clone(), im2.clone()
elif aug == "blend":
im1_aug, im2_aug = blend(
im1.clone(), im2.clone(),
prob=prob, alpha=alpha
)
elif aug == "mixup":
im1_aug, im2_aug, = mixup(
im1.clone(), im2.clone(),
prob=prob, alpha=alpha,
)
elif aug == "cutout":
im1_aug, im2_aug, mask, _ = cutout(
im1.clone(), im2.clone(),
prob=prob, alpha=alpha
)
elif aug == "cutmix":
im1_aug, im2_aug = cutmix(
im1.clone(), im2.clone(),
prob=prob, alpha=alpha,
)
elif aug == "cutmixup":
im1_aug, im2_aug = cutmixup(
im1.clone(), im2.clone(),
mixup_prob=aux_prob, mixup_alpha=aux_alpha,
cutmix_prob=prob, cutmix_alpha=alpha,
)
elif aug == "cutblur":
im1_aug, im2_aug = cutblur(
im1.clone(), im2.clone(),
prob=prob, alpha=alpha
)
elif aug == "rgb":
im1_aug, im2_aug = rgb(
im1.clone(), im2.clone(),
prob=prob
)
else:
raise ValueError("{} is not invalid.".format(aug))
return im1_aug, im2_aug, mask, aug
def blend(im1, im2, prob=1.0, alpha=0.6):
if alpha <= 0 or np.random.rand(1) >= prob:
return im1, im2
c = torch.empty((im2.size(0), 3, 1, 1), device=im2.device).uniform_(0, 255)
rim2 = c.repeat((1, 1, im2.size(2), im2.size(3)))
rim1 = c.repeat((1, 1, im1.size(2), im1.size(3)))
v = np.random.uniform(alpha, 1)
im1 = v * im1 + (1-v) * rim1
im2 = v * im2 + (1-v) * rim2
return im1, im2
def mixup(im1, im2, prob=1.0, alpha=1.2):
if alpha <= 0 or np.random.rand(1) >= prob:
return im1, im2
v = np.random.beta(alpha, alpha)
r_index = torch.randperm(im1.size(0)).to(im2.device)
im1 = v * im1 + (1-v) * im1[r_index, :]
im2 = v * im2 + (1-v) * im2[r_index, :]
return im1, im2
def _cutmix(im2, prob=1.0, alpha=1.0):
if alpha <= 0 or np.random.rand(1) >= prob:
return None
cut_ratio = np.random.randn() * 0.01 + alpha
h, w = im2.size(2), im2.size(3)
ch, cw = np.int(h*cut_ratio), np.int(w*cut_ratio)
fcy = np.random.randint(0, h-ch+1)
fcx = np.random.randint(0, w-cw+1)
tcy, tcx = fcy, fcx
rindex = torch.randperm(im2.size(0)).to(im2.device)
return {
"rindex": rindex, "ch": ch, "cw": cw,
"tcy": tcy, "tcx": tcx, "fcy": fcy, "fcx": fcx,
}
def cutmix(im1, im2, prob=1.0, alpha=1.0):
c = _cutmix(im2, prob, alpha)
if c is None:
return im1, im2
scale = im1.size(2) // im2.size(2)
rindex, ch, cw = c["rindex"], c["ch"], c["cw"]
tcy, tcx, fcy, fcx = c["tcy"], c["tcx"], c["fcy"], c["fcx"]
hch, hcw = ch*scale, cw*scale
hfcy, hfcx, htcy, htcx = fcy*scale, fcx*scale, tcy*scale, tcx*scale
im2[..., tcy:tcy+ch, tcx:tcx+cw] = im2[rindex, :, fcy:fcy+ch, fcx:fcx+cw]
im1[..., htcy:htcy+hch, htcx:htcx+hcw] = im1[rindex, :, hfcy:hfcy+hch, hfcx:hfcx+hcw]
return im1, im2
def cutmixup(
im1, im2,
mixup_prob=1.0, mixup_alpha=1.0,
cutmix_prob=1.0, cutmix_alpha=1.0
):
c = _cutmix(im2, cutmix_prob, cutmix_alpha)
if c is None:
return im1, im2
scale = im1.size(2) // im2.size(2)
rindex, ch, cw = c["rindex"], c["ch"], c["cw"]
tcy, tcx, fcy, fcx = c["tcy"], c["tcx"], c["fcy"], c["fcx"]
hch, hcw = ch*scale, cw*scale
hfcy, hfcx, htcy, htcx = fcy*scale, fcx*scale, tcy*scale, tcx*scale
v = np.random.beta(mixup_alpha, mixup_alpha)
if mixup_alpha <= 0 or np.random.rand(1) >= mixup_prob:
im2_aug = im2[rindex, :]
im1_aug = im1[rindex, :]
else:
im2_aug = v * im2 + (1-v) * im2[rindex, :]
im1_aug = v * im1 + (1-v) * im1[rindex, :]
# apply mixup to inside or outside
if np.random.random() > 0.5:
im2[..., tcy:tcy+ch, tcx:tcx+cw] = im2_aug[..., fcy:fcy+ch, fcx:fcx+cw]
im1[..., htcy:htcy+hch, htcx:htcx+hcw] = im1_aug[..., hfcy:hfcy+hch, hfcx:hfcx+hcw]
else:
im2_aug[..., tcy:tcy+ch, tcx:tcx+cw] = im2[..., fcy:fcy+ch, fcx:fcx+cw]
im1_aug[..., htcy:htcy+hch, htcx:htcx+hcw] = im1[..., hfcy:hfcy+hch, hfcx:hfcx+hcw]
im2, im1 = im2_aug, im1_aug
return im1, im2
def cutblur(im1, im2, prob=1.0, alpha=1.0):
if im1.size() != im2.size():
raise ValueError("im1 and im2 have to be the same resolution.")
if alpha <= 0 or np.random.rand(1) >= prob:
return im1, im2
cut_ratio = np.random.randn() * 0.01 + alpha
h, w = im2.size(2), im2.size(3)
ch, cw = np.int(h*cut_ratio), np.int(w*cut_ratio)
cy = np.random.randint(0, h-ch+1)
cx = np.random.randint(0, w-cw+1)
# apply CutBlur to inside or outside
if np.random.random() > 0.5:
im2[..., cy:cy+ch, cx:cx+cw] = im1[..., cy:cy+ch, cx:cx+cw]
else:
im2_aug = im1.clone()
im2_aug[..., cy:cy+ch, cx:cx+cw] = im2[..., cy:cy+ch, cx:cx+cw]
im2 = im2_aug
return im1, im2
def cutout(im1, im2, prob=1.0, alpha=0.1):
scale = im1.size(2) // im2.size(2)
fsize = (im2.size(0), 1)+im2.size()[2:]
if alpha <= 0 or np.random.rand(1) >= prob:
fim2 = np.ones(fsize)
fim2 = torch.tensor(fim2, dtype=torch.float, device=im2.device)
fim1 = F.interpolate(fim2, scale_factor=scale, mode="nearest")
return im1, im2, fim1, fim2
fim2 = np.random.choice([0.0, 1.0], size=fsize, p=[alpha, 1-alpha])
fim2 = torch.tensor(fim2, dtype=torch.float, device=im2.device)
fim1 = F.interpolate(fim2, scale_factor=scale, mode="nearest")
im2 *= fim2
return im1, im2, fim1, fim2
def rgb(im1, im2, prob=1.0):
if np.random.rand(1) >= prob:
return im1, im2
perm = np.random.permutation(3)
im1 = im1[:, perm]
im2 = im2[:, perm]
return im1, im2