This repository has been archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
augmentation.py
601 lines (509 loc) · 19.5 KB
/
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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
"""
Coior Augmentation: Autocontrast, Brightness, Color, Contrast, Equalize, Posterize, Sharpness, Solarize, SolarizeAdd
Geometric Augmentation: Rotate_BBox, ShearX_BBox, ShearY_BBox, TranslateX_BBox, TranslateY_BBox
Mask Augmentation: Cutout
Color Augmentation based on BBoxes: Equalize_Only_BBoxes, Solarize_Only_BBoxes
Geometric Augmentation based on BBoxes: Rotate_Only_BBoxes, ShearX_Only_BBoxes, ShearY_Only_BBoxes,
TranslateX_Only_BBoxes, TranslateY_Only_BBoxes, Flip_Only_BBoxes
Mask Augmentation based on BBoxes: BBox_Cutout, Cutout_Only_BBoxes
"""
import torch, torchvision, functional
import torchvision.transforms.functional as F
from PIL import Image, ImageOps
### Basic Augmentation
class Compose:
"""
Composes several transforms together.
"""
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, image, bboxs):
for t in self.transforms:
image, bboxs = t(image, bboxs)
return image, bboxs
class ToTensor:
"""
Converts a PIL Image or numpy.ndarray (H x W x C) to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0].
Only applied to image, not bboxes.
"""
def __call__(self, image, bboxs):
return F.to_tensor(image), bboxs
class Normalize(torch.nn.Module):
"""
Normalize a tensor image with mean and standard deviation.
Only applied to image, not bboxes.
"""
def __init__(self, mean, std, inplace=False):
super().__init__()
self.mean = mean
self.std = std
self.inplace = inplace
def forward(self, image, bboxs):
return F.normalize(image, self.mean, self.std, self.inplace), bboxs
### Coior Augmentation
class AutoContrast(torch.nn.Module):
"""
Autocontrast the pixels of the given image.
Only applied to image, not bboxes.
"""
def __init__(self, p):
super().__init__()
self.p = p
def forward(self, image, bboxs):
if torch.rand(1) < self.p:
autocontrast_image = ImageOps.autocontrast(image)
return autocontrast_image, bboxs
else:
return image, bboxs
class Brightness(torch.nn.Module):
"""
Adjust image brightness using magnitude.
Only applied to image, not bboxes.
"""
def __init__(self, p, magnitude, minus=True):
super().__init__()
self.p = p
self.magnitude = magnitude
self.minus = minus
def forward(self, image, bboxs):
if self.minus and (torch.rand(1) < 0.5): self.magnitude *= -1
if torch.rand(1) < self.p:
brightness_image = functional.brightness(image, 1+self.magnitude)
return brightness_image, bboxs
else:
return image, bboxs
class Color(torch.nn.Module):
"""
Adjust image color balance using magnitude.
Only applied to image, not bboxes.
"""
def __init__(self, p, magnitude, minus=True):
super().__init__()
self.p = p
self.magnitude = magnitude
self.minus = minus
def forward(self, image, bboxs):
if self.minus and (torch.rand(1) < 0.5): self.magnitude *= -1
if torch.rand(1) < self.p:
color_image = functional.color(image, 1+self.magnitude)
return color_image, bboxs
else:
return image, bboxs
class Contrast(torch.nn.Module):
"""
Adjust image contrast using magnitude.
Only applied to image, not bboxes.
"""
def __init__(self, p, magnitude, minus=True):
super().__init__()
self.p = p
self.magnitude = magnitude
self.minus = minus
def forward(self, image, bboxs):
if self.minus and (torch.rand(1) < 0.5): self.magnitude *= -1
if torch.rand(1) < self.p:
contrast_image = functional.contrast(image, 1+self.magnitude)
return contrast_image, bboxs
else:
return image, bboxs
class Equalize(torch.nn.Module):
"""
Equalize the histogram of the given image.
Only applied to image, not bboxes.
"""
def __init__(self, p):
super().__init__()
self.p = p
def forward(self, image, bboxs):
if torch.rand(1) < self.p:
equalize_image = ImageOps.equalize(image)
return equalize_image, bboxs
else:
return image, bboxs
class Posterize(torch.nn.Module):
"""
Posterize the image by reducing the number of bits for each color channel.
Only applied to image, not bboxes.
"""
def __init__(self, p, bits):
super().__init__()
self.p = p
self.bits = int(bits)
def forward(self, image, bboxs):
if torch.rand(1) < self.p:
posterize_image = ImageOps.posterize(image, self.bits)
return posterize_image, bboxs
else:
return image, bboxs
class Sharpness(torch.nn.Module):
"""
Adjust image sharpness using magnitude.
Only applied to image, not bboxes.
"""
def __init__(self, p, magnitude, minus=True):
super().__init__()
self.p = p
self.magnitude = magnitude
self.minus = minus
def forward(self, image, bboxs):
if self.minus and (torch.rand(1) < 0.5): self.magnitude *= -1
if torch.rand(1) < self.p:
sharpness_image = functional.sharpness(image, 1+self.magnitude)
return sharpness_image, bboxs
else:
return image, bboxs
class Solarize(torch.nn.Module):
"""
Solarize the image by inverting all pixel values above a threshold.
Only applied to image, not bboxes.
"""
def __init__(self, p, threshold):
super().__init__()
self.p = p
self.threshold = int(threshold)
def forward(self, image, bboxs):
if torch.rand(1) < self.p:
solarize_image = ImageOps.solarize(image, self.threshold)
return solarize_image, bboxs
else:
return image, bboxs
class SolarizeAdd(torch.nn.Module):
"""
Solarize the image by added image below a threshold.
Add addition amount to image and then clip the pixel value to 0~255 or 0~1.
Parameter addition must be integer.
Only applied to image, not bboxes.
"""
def __init__(self, p, addition, threshold=128, minus=True):
super().__init__()
self.p = p
self.addition = int(addition)
self.threshold = int(threshold)
self.minus = minus
def forward(self, image, bboxs):
if self.minus and (torch.rand(1) < 0.5): self.addition *= -1
if torch.rand(1) < self.p:
solarize_add_image = functional.solarize_add(image, self.addition, self.threshold)
return solarize_add_image, bboxs
else:
return image, bboxs
### Geometric Augmentation
class Rotate_BBox(torch.nn.Module):
"""
Rotate image by degrees and change bboxes according to rotated image.
The pixel values filled in will be of the value replace.
Assume the coords are given min_x, min_y, max_x, max_y.
Both applied to image and bboxes.
"""
def __init__(self, p, degrees, replace=128, minus=True):
super().__init__()
self.p = p
self.degrees = degrees
self.replace = replace
self.minus = minus
def forward(self, image, bboxs):
if self.minus and (torch.rand(1) < 0.5): self.degrees *= -1
if torch.rand(1) < self.p:
rotate_image = image.rotate(self.degrees, fillcolor=(self.replace, self.replace, self.replace))
if bboxs == None:
return rotate_image, bboxs
else:
rotate_bbox = functional._rotate_bbox(image, bboxs, self.degrees)
return rotate_image, rotate_bbox
else:
return image, bboxs
class ShearX_BBox(torch.nn.Module):
"""
Shear image and change bboxes on X-axis.
The pixel values filled in will be of the value replace.
Level is usually between -0.3~0.3.
Assume the coords are given min_x, min_y, max_x, max_y.
Both applied to image and bboxes.
"""
def __init__(self, p, level, replace=128, minus=True):
super().__init__()
self.p = p
self.level = level
self.replace = replace
self.minus = minus
def forward(self, image, bboxs):
if self.minus and (torch.rand(1) < 0.5): self.level *= -1
if torch.rand(1) < self.p:
shear_image = image.transform(image.size, Image.AFFINE, (1, self.level, 0, 0, 1, 0), fillcolor=(self.replace, self.replace, self.replace))
if bboxs == None:
return shear_image, bboxs
else:
shear_bbox = functional.shear_with_bboxes(image, bboxs, self.level, self.replace, shift_horizontal=True)
return shear_image, shear_bbox
else:
return image, bboxs
class ShearY_BBox(torch.nn.Module):
"""
Shear image and change bboxes on Y-axis.
The pixel values filled in will be of the value replace.
Level is usually between -0.3~0.3.
Assume the coords are given min_x, min_y, max_x, max_y.
Both applied to image and bboxes.
"""
def __init__(self, p, level, replace=128, minus=True):
super().__init__()
self.p = p
self.level = level
self.replace = replace
self.minus = minus
def forward(self, image, bboxs):
if self.minus and (torch.rand(1) < 0.5): self.level *= -1
if torch.rand(1) < self.p:
shear_image = image.transform(image.size, Image.AFFINE, (1, 0, 0, self.level, 1, 0), fillcolor=(self.replace, self.replace, self.replace))
if bboxs == None:
return shear_image, bboxs
else:
shear_bbox = functional.shear_with_bboxes(image, bboxs, self.level, self.replace, shift_horizontal=False)
return shear_image, shear_bbox
else:
return image, bboxs
class TranslateX_BBox(torch.nn.Module):
"""
Translate image and bboxes on X-axis.
The pixel values filled in will be of the value replace.
Assume the coords are given min_x, min_y, max_x, max_y.
Both applied to image and bboxes.
"""
def __init__(self, p, pixels, replace=128, minus=True):
super().__init__()
self.p = p
self.pixels = int(pixels)
self.replace = replace
self.minus = minus
def forward(self, image, bboxs):
if self.minus and (torch.rand(1) < 0.5): self.pixels *= -1
if torch.rand(1) < self.p:
translate_image = image.transform(image.size, Image.AFFINE, (1, 0, -self.pixels, 0, 1, 0), fillcolor=(self.replace, self.replace, self.replace))
if bboxs == None:
return translate_image, bboxs
else:
translate_bbox = functional.translate_bbox(image, bboxs, self.pixels, self.replace, shift_horizontal=True)
return translate_image, translate_bbox
else:
return image, bboxs
class TranslateY_BBox(torch.nn.Module):
"""
Translate image and bboxes on Y-axis.
The pixel values filled in will be of the value replace.
Assume the coords are given min_x, min_y, max_x, max_y.
Both applied to image and bboxes.
"""
def __init__(self, p, pixels, replace=128, minus=True):
super().__init__()
self.p = p
self.pixels = int(pixels)
self.replace = replace
self.minus = minus
def forward(self, image, bboxs):
if self.minus and (torch.rand(1) < 0.5): self.pixels *= -1
if torch.rand(1) < self.p:
translate_image = image.transform(image.size, Image.AFFINE, (1, 0, 0, 0, 1, -self.pixels), fillcolor=(self.replace, self.replace, self.replace))
if bboxs == None:
return translate_image, bboxs
else:
translate_bbox = functional.translate_bbox(image, bboxs, self.pixels, self.replace, shift_horizontal=False)
return translate_image, translate_bbox
else:
return image, bboxs
### Mask Augmentation
class Cutout(torch.nn.Module):
"""
Apply cutout (https://arxiv.org/abs/1708.04552) to the image.
This operation applies a (2*pad_size, 2*pad_size) mask of zeros to a random location within image.
The pixel values filled in will be of the value replace.
Only applied to image, not bboxes.
"""
def __init__(self, p, pad_size, replace=128):
super().__init__()
self.p = p
self.pad_size = int(pad_size)
self.replace = replace
def forward(self, image, bboxs):
if torch.rand(1) < self.p:
cutout_image = functional.cutout(image, self.pad_size, self.replace)
return cutout_image, bboxs
else:
return image, bboxs
### Color Augmentation based on BBoxes
class Equalize_Only_BBoxes(torch.nn.Module):
"""
Apply equalize to each bboxes in the image with probability.
Assume the coords are given min_x, min_y, max_x, max_y.
Only applied to image not bboxes.
"""
def __init__(self, p):
super().__init__()
self.p = p/3
def forward(self, image, bboxs):
if bboxs == None:
return image, bboxs
else:
equalize_image = functional.equalize_only_bboxes(image, bboxs, self.p)
return equalize_image, bboxs
class Solarize_Only_BBoxes(torch.nn.Module):
"""
Apply solarize to each bboxes in the image with probability.
Assume the coords are given min_x, min_y, max_x, max_y.
Only applied to image not bboxes.
"""
def __init__(self, p, threshold):
super().__init__()
self.p = p/3
self.threshold = int(threshold)
def forward(self, image, bboxs):
if bboxs == None:
return image, bboxs
else:
solarize_image = functional.solarize_only_bboxes(image, bboxs, self.p, self.threshold)
return solarize_image, bboxs
### Geometric Augmentation based on BBoxes
class Rotate_Only_BBoxes(torch.nn.Module):
"""
Apply rotation to each bboxes in the image with probability.
Assume the coords are given min_x, min_y, max_x, max_y.
Only applied to image not bboxes.
"""
def __init__(self, p, degrees, replace=128, minus=True):
super().__init__()
self.p = p/3
self.degrees = degrees
self.replace = replace
self.minus = minus
def forward(self, image, bboxs):
if self.minus and (torch.rand(1) < 0.5): self.degrees *= -1
if bboxs == None:
return image, bboxs
else:
rotate_image = functional.rotate_only_bboxes(image, bboxs, self.p, self.degrees, self.replace)
return rotate_image, bboxs
class ShearX_Only_BBoxes(torch.nn.Module):
"""
Apply shear to each bboxes in the image with probability only on X-axis.
Assume the coords are given min_x, min_y, max_x, max_y.
Only applied to image not bboxes.
"""
def __init__(self, p, level, replace=128, minus=True):
super().__init__()
self.p = p/3
self.level = level
self.replace = replace
self.minus = minus
def forward(self, image, bboxs):
if self.minus and (torch.rand(1) < 0.5): self.level *= -1
if bboxs == None:
return image, bboxs
else:
shear_image = functional.shear_only_bboxes(image, bboxs, self.p, self.level, self.replace, shift_horizontal=True)
return shear_image, bboxs
class ShearY_Only_BBoxes(torch.nn.Module):
"""
Apply shear to each bboxes in the image with probability only on Y-axis.
Assume the coords are given min_x, min_y, max_x, max_y.
Only applied to image not bboxes.
"""
def __init__(self, p, level, replace=128, minus=True):
super().__init__()
self.p = p/3
self.level = level
self.replace = replace
self.minus = minus
def forward(self, image, bboxs):
if self.minus and (torch.rand(1) < 0.5): self.level *= -1
if bboxs == None:
return image, bboxs
else:
shear_image = functional.shear_only_bboxes(image, bboxs, self.p, self.level, self.replace, shift_horizontal=False)
return shear_image, bboxs
class TranslateX_Only_BBoxes(torch.nn.Module):
"""
Apply translation to each bboxes in the image with probability only on X-axis.
Assume the coords are given min_x, min_y, max_x, max_y.
Only applied to image not bboxes.
"""
def __init__(self, p, pixels, replace=128, minus=True):
super().__init__()
self.p = p/3
self.pixels = int(pixels)
self.replace = replace
self.minus = minus
def forward(self, image, bboxs):
if self.minus and (torch.rand(1) < 0.5): self.pixels *= -1
if bboxs == None:
return image, bboxs
else:
translate_image = functional.translate_only_bboxes(image, bboxs, self.p, self.pixels, self.replace, shift_horizontal=True)
return translate_image, bboxs
class TranslateY_Only_BBoxes(torch.nn.Module):
"""
Apply transloation to each bboxes in the image with probability only on Y-axis.
Assume the coords are given min_x, min_y, max_x, max_y.
Only applied to image not bboxes.
"""
def __init__(self, p, pixels, replace=128, minus=True):
super().__init__()
self.p = p/3
self.pixels = int(pixels)
self.replace = replace
self.minus = minus
def forward(self, image, bboxs):
if self.minus and (torch.rand(1) < 0.5): self.pixels *= -1
if bboxs == None:
return image, bboxs
else:
translate_image = functional.translate_only_bboxes(image, bboxs, self.p, self.pixels, self.replace, shift_horizontal=False)
return translate_image, bboxs
class Flip_Only_BBoxes(torch.nn.Module):
"""
Apply horizontal flip to each bboxes in the image with probability.
Assume the coords are given min_x, min_y, max_x, max_y.
Only applied to image not bboxes.
"""
def __init__(self, p):
super().__init__()
self.p = p/3
def forward(self, image, bboxs):
if bboxs == None:
return image, bboxs
else:
flip_image = functional.flip_only_bboxes(image, bboxs, self.p)
return flip_image, bboxs
### Mask Augmentation based on BBoxes
class BBox_Cutout(torch.nn.Module):
"""
Apply cutout to the image according to bbox information.
Assume the coords are given min_x, min_y, max_x, max_y.
Only applied to image, not bboxes.
"""
def __init__(self, p, pad_fraction, replace_with_mean=False):
super().__init__()
self.p = p
self.pad_fraction = pad_fraction
self.replace_with_mean = replace_with_mean
def forward(self, image, bboxs):
if (torch.rand(1) < self.p) and (bboxs != None):
cutout_image = functional.bbox_cutout(image, bboxs, self.pad_fraction, self.replace_with_mean)
return cutout_image, bboxs
else:
return image, bboxs
class Cutout_Only_BBoxes(torch.nn.Module):
"""
Apply cutout to each bboxes in the image with probability.
Assume the coords are given min_x, min_y, max_x, max_y.
Only applied to image not bboxes.
"""
def __init__(self, p, pad_size, replace=128):
super().__init__()
self.p = p/3
self.pad_size = int(pad_size)
self.replace = replace
def forward(self, image, bboxs):
if bboxs == None:
return image, bboxs
else:
cutout_image = functional.cutout_only_bboxes(image, bboxs, self.p, self.pad_size, self.replace)
return cutout_image, bboxs