forked from JeongsooP/Community-Forensics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_transforms.py
More file actions
1078 lines (958 loc) · 40.7 KB
/
custom_transforms.py
File metadata and controls
1078 lines (958 loc) · 40.7 KB
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import math
from typing import List, Tuple, Optional, Dict
import os
import torch
from torch import Tensor
import numpy as np
import PIL.Image
import random
from io import BytesIO
import cv2
import numpy as np
from torchvision.transforms import functional as F, InterpolationMode
import torchvision.transforms as T
__all__ = ["AutoAugmentPolicy", "AutoAugment", "RandAugment", "TrivialAugmentWide", "AugMix"]
def get_dimensions(img):
height, width = F.get_image_size(img)
channels = F.get_image_num_channels(img)
return channels, height, width
def cutout(img, pad_size, replace=0):
"""Apply cutout (https://arxiv.org/abs/1708.04552) to image.
### (PyTorch implementation of Google's big_vision cutout) ###
This operation applies a (2*pad_size x 2*pad_size) mask of zeros to
a random location within `img`. The pixel values filled in will be of the
value `replace`. The located where the mask will be applied is randomly
chosen uniformly over the whole image.
Args:
image: A PIL image
pad_size: Specifies how big the zero mask that will be generated is that
is applied to the image. The mask will be of size
(2*pad_size x 2*pad_size).
replace: What pixel value to fill in the image in the area that has
the cutout mask applied to it.
Returns:
A PIL image of type uint8.
"""
convert_back=False
if F._is_pil_image(img):
img = F.pil_to_tensor(img) # convert to tensor for pytorch operations
convert_back=True
assert img.dtype == torch.uint8, "PIL to tensor image is expected to have torch.unit8 as dtype."
channels, height, width = get_dimensions(img)
cutout_center_height = torch.randint(low=0, high=height, size=(1,)).item()
cutout_center_width = torch.randint(low=0, high=width, size=(1,)).item()
lower_pad = max(0, cutout_center_height - pad_size)
upper_pad = max(0, height - cutout_center_height - pad_size)
left_pad = max(0, cutout_center_width - pad_size)
right_pad = max(0, width - cutout_center_width - pad_size)
cutout_shape = (height - (lower_pad + upper_pad),
width - (left_pad + right_pad)) # cutout this shape
padding_dims = (left_pad, right_pad, upper_pad, lower_pad)
cutout_mask = torch.nn.functional.pad(
torch.zeros(cutout_shape, dtype=img.dtype, device=img.device),
padding_dims, value=1
)
cutout_mask = cutout_mask.unsqueeze(dim=0)
cutout_mask = torch.tile(cutout_mask, (channels,1,1))
#replacement = torch.ones_like(img, dtype=torch.float32) * replace[0]
#replacement = replacement.to(torch.uint8)
img = torch.where(
cutout_mask==0, # condition.
torch.ones_like(img, dtype=img.dtype, device=img.device) * replace, # If true
#replacement,
img # If condition is false
)
if convert_back:
return F.to_pil_image(img)
else:
return img
def solarize_add(img, addition=0, threshold=128):
"""
For each pixel in the image less than threshold
we add 'addition' amount to it and then clip the
pixel value to be between 0 and 255. The value
of 'addition' is between -128 and 128.
### Re-implementation of Google's big_vision in PyTorch ###
"""
convert_back=False
if F._is_pil_image(img):
img = F.pil_to_tensor(img) # convert to tensor for pytorch operations
convert_back=True
assert img.dtype == torch.uint8, "PIL to tensor image is expected to have torch.unit8 as dtype."
added_img = img.to(torch.int) + addition
added_img = torch.clamp(added_img, min=0,max=255)
added_img = added_img.to(img.dtype)
img = torch.where(
img < threshold, # condition
added_img, # if true
img # if false
)
if convert_back:
return F.to_pil_image(img)
else:
return img
def chroma_drop(img):
img = img.convert("YCbCr")
Y, Cb, Cr = img.split()
if torch.rand(1).item() > 0.5:
Cr = Cr.point(lambda i: 128)
else:
Cb = Cb.point(lambda i: 128)
img = PIL.Image.merge("YCbCr", (Y, Cb, Cr))
return img.convert("RGB")
def auto_saturation_separate(img):
img = img.convert("YCbCr")
Y, Cb, Cr = img.split()
Cbmin, Cbmax = Cb.getextrema()
Crmin, Crmax = Cr.getextrema()
Cmin = min(Cbmin, Crmin)
Cmax = max(Cbmax, Crmax)
Cb = Cb.point(lambda i: ((i-128) / (Cmax - 128) * 127 + 128 if Cmax > 128 else i) if i>127 \
else ((i - Cmin) / (127 - Cmin) * 127) if Cmin<127 else i) # scale >127 and else separately (they represent different hue)
#Cb = Cb.point(lambda i: (i-Cbmin) / (Cbmax - Cbmin) * 255)
Cr = Cr.point(lambda i: ((i-128) / (Cmax - 128) * 127 + 128 if Cmax > 128 else i) if i>127 \
else ((i - Cmin) / (127 - Cmin) * 127) if Cmin<127 else i)
#Cr = Cr.point(lambda i: (i-Crmin) / (Crmax - Crmin) * 255)
img = PIL.Image.merge("YCbCr", (Y, Cb, Cr))
return img.convert("RGB")
def auto_saturation(img):
img = img.convert("YCbCr")
Y, Cb, Cr = img.split()
Cbmin, Cbmax = Cb.getextrema()
Crmin, Crmax = Cr.getextrema()
Cmin = min(Cbmin, Crmin)
Cmax = max(Cbmax, Crmax)
Cb = Cb.point(lambda i: (i-Cmin) / (Cmax - Cmin) * 255 if (Cmax - Cmin) != 0 else i)
Cr = Cr.point(lambda i: (i-Cmin) / (Cmax - Cmin) * 255 if (Cmax - Cmin) != 0 else i)
img = PIL.Image.merge("YCbCr", (Y, Cb, Cr))
return img.convert("RGB")
def _apply_op(
img: Tensor, op_name: str, magnitude: float, interpolation: InterpolationMode, fill: Optional[List[float]]
):
if op_name == "ShearX":
# magnitude should be arctan(magnitude)
# official autoaug: (1, level, 0, 0, 1, 0)
# https://github.com/tensorflow/models/blob/dd02069717128186b88afa8d857ce57d17957f03/research/autoaugment/augmentation_transforms.py#L290
# compared to
# torchvision: (1, tan(level), 0, 0, 1, 0)
# https://github.com/pytorch/vision/blob/0c2373d0bba3499e95776e7936e207d8a1676e65/torchvision/transforms/functional.py#L976
img = F.affine(
img,
angle=0.0,
translate=[0, 0],
scale=1.0,
shear=[math.degrees(math.atan(magnitude)), 0.0],
interpolation=interpolation,
fill=fill,
center=[0, 0],
)
elif op_name == "ShearY":
# magnitude should be arctan(magnitude)
# See above
img = F.affine(
img,
angle=0.0,
translate=[0, 0],
scale=1.0,
shear=[0.0, math.degrees(math.atan(magnitude))],
interpolation=interpolation,
fill=fill,
center=[0, 0],
)
elif op_name == "TranslateX":
img = F.affine(
img,
angle=0.0,
translate=[int(magnitude), 0],
scale=1.0,
interpolation=interpolation,
shear=[0.0, 0.0],
fill=fill,
)
elif op_name == "TranslateY":
img = F.affine(
img,
angle=0.0,
translate=[0, int(magnitude)],
scale=1.0,
interpolation=interpolation,
shear=[0.0, 0.0],
fill=fill,
)
elif op_name == "Rotate":
img = F.rotate(img, magnitude, interpolation=interpolation, fill=fill)
elif op_name == "Brightness":
img = F.adjust_brightness(img, 1.0 + magnitude)
elif op_name == "Color":
img = F.adjust_saturation(img, 1.0 + magnitude)
elif op_name == "Contrast":
img = F.adjust_contrast(img, 1.0 + magnitude)
elif op_name == "Sharpness":
img = F.adjust_sharpness(img, 1.0 + magnitude)
elif op_name == "Posterize":
img = F.posterize(img, int(magnitude))
elif op_name == "Solarize":
img = F.solarize(img, magnitude)
elif op_name == "AutoContrast":
img = F.autocontrast(img)
elif op_name == "Equalize":
img = F.equalize(img)
elif op_name == "Invert":
img = F.invert(img)
elif op_name == "Identity":
pass
elif op_name == 'Cutout': # added
img = cutout(img, int(magnitude), replace=fill)
elif op_name == "SolarizeAdd": # added
img = solarize_add(img, int(magnitude))
elif op_name == "Grayscale": # added v2
img = F.to_grayscale(img, num_output_channels=3)
elif op_name == "ChromaDrop": #
img = chroma_drop(img)
elif op_name == "AutoSaturation":
#img = auto_saturation(img)
img = auto_saturation(img) # dct-equivalent
elif op_name == "AutoSaturation_old": # for compatibility purposes
img = auto_saturation(img)
elif op_name == "Rotate90": # magnitude is +- 90
img = F.rotate(img, magnitude, interpolation=interpolation, fill=fill)
else:
raise ValueError(f"The provided operator {op_name} is not recognized.")
return img
class RandAugment_bv(torch.nn.Module):
r"""RandAugment data augmentation method based on
`"RandAugment: Practical automated data augmentation with a reduced search space"
<https://arxiv.org/abs/1909.13719>`_.
### Re-implementation of Google's Big Vision randaugment in PyTorch ###
If the image is torch Tensor, it should be of type torch.uint8, and it is expected
to have [..., 1 or 3, H, W] shape, where ... means an arbitrary number of leading dimensions.
If img is PIL Image, it is expected to be in mode "L" or "RGB".
Args:
num_ops (int): Number of augmentation transformations to apply sequentially.
magnitude (int): Magnitude for all the transformations.
num_magnitude_bins (int): The number of different magnitude values.
interpolation (InterpolationMode): Desired interpolation enum defined by
:class:`torchvision.transforms.InterpolationMode`. Default is ``InterpolationMode.NEAREST``.
If input is Tensor, only ``InterpolationMode.NEAREST``, ``InterpolationMode.BILINEAR`` are supported.
fill (sequence or number, optional): Pixel fill value for the area outside the transformed
image. If given a number, the value is used for all bands respectively.
"""
def __init__(
self,
num_ops: int = 2,
magnitude: int = 10,
num_magnitude_bins: int = 11,
interpolation: InterpolationMode = InterpolationMode.NEAREST,
fill: Optional[List[float]] = None,
ops_list = ["AutoContrast", "Equalize", "Invert", "Rotate", "Posterize", "Solarize", "SolarizeAdd", "Color", "Contrast", "Brightness",
"Sharpness", "ShearX", "ShearY", "Cutout", "TranslateX", "TranslateY"]
) -> None:
super().__init__()
self.num_ops = num_ops
self.magnitude = magnitude
self.num_magnitude_bins = num_magnitude_bins
self.interpolation = interpolation
self.fill = fill
if ops_list==None:
self.ops_list = ["AutoContrast", "Equalize", "Invert", "Rotate", "Posterize", "Solarize", "SolarizeAdd", "Color", "Contrast", "Brightness",
"Sharpness", "ShearX", "ShearY", "Cutout", "TranslateX", "TranslateY"]
else:
self.ops_list = ops_list
def _augmentation_space(self, num_bins: int, image_size: Tuple[int, int]) -> Dict[str, Tuple[Tensor, bool]]:
return {
# op_name: (magnitudes, signed)
#"Identity": (torch.tensor(0.0), False), not needed
"AutoContrast": (torch.tensor(0.0), False),
"Equalize": (torch.tensor(0.0), False),
"Invert": (torch.tensor(0.0), False), # added
"Rotate": (torch.linspace(0.0, 30.0, num_bins), True),
"Posterize": (8 - (torch.arange(num_bins) / ((num_bins - 1) / 4)).round().int(), False),
"Solarize": (torch.linspace(255.0, 0.0, num_bins), False),
"SolarizeAdd": (torch.linspace(0, 110, num_bins), False), # added
"Color": (torch.linspace(0.0, 0.9, num_bins), True),
"Contrast": (torch.linspace(0.0, 0.9, num_bins), True),
"Brightness": (torch.linspace(0.0, 0.9, num_bins), True),
"Sharpness": (torch.linspace(0.0, 0.9, num_bins), True),
"ShearX": (torch.linspace(0.0, 0.3, num_bins), True),
"ShearY": (torch.linspace(0.0, 0.3, num_bins), True),
"Cutout": (torch.linspace(0, 40, num_bins), False), #added
"TranslateX": (torch.linspace(0.0, 150.0 / 336.0 * image_size[1], num_bins), True),
"TranslateY": (torch.linspace(0.0, 150.0 / 336.0 * image_size[0], num_bins), True),
"Grayscale": (torch.tensor(0.0), False),
"ChromaDrop": (torch.tensor(0.0), False),
"AutoSaturation": (torch.tensor(0.0), False),
"AutoSaturation_old": (torch.tensor(0.0), False),
"Rotate90": (torch.tensor(90.0), True),
}
def forward(self, img: Tensor) -> Tensor:
"""
img (PIL Image or Tensor): Image to be transformed.
Returns:
PIL Image or Tensor: Transformed image.
"""
fill = self.fill
channels, height, width = get_dimensions(img)
#if isinstance(img, Tensor):
# if isinstance(fill, (int, float)):
# fill = [float(fill)] * channels
# elif fill is not None:
# fill = [float(f) for f in fill]
op_meta = self._augmentation_space(self.num_magnitude_bins, (height, width))
for _ in range(self.num_ops):
op_index = int(torch.randint(len(self.ops_list), (1,)).item())
op_name = list(self.ops_list)[op_index]
magnitudes, signed = op_meta[op_name]
magnitude = float(magnitudes[self.magnitude].item()) if magnitudes.ndim > 0 else 0.0
if signed and torch.randint(2, (1,)):
magnitude *= -1.0
img = _apply_op(img, op_name, magnitude, interpolation=self.interpolation, fill=fill)
return img
def __repr__(self) -> str:
s = (
f"{self.__class__.__name__}("
f"num_ops={self.num_ops}"
f", magnitude={self.magnitude}"
f", num_magnitude_bins={self.num_magnitude_bins}"
f", interpolation={self.interpolation}"
f", fill={self.fill}"
f")"
)
return s
class ToTensor_range(torch.nn.Module):
r"""
Converts PIL image to Tensor into a specified range
Args:
val_min = minimum value after convert
val_max = maximum value after convert
dtype = dtype after convert (default=torch.float32)
Returns:
Converted Torch Tensor
"""
def __init__(
self,
val_min: float = -1.,
val_max: float = 1.,
dtype = torch.float32,
) -> Tensor:
super().__init__()
self.val_min = val_min
self.val_max = val_max
self.dtype = dtype
def forward(self, img) -> Tensor:
"""
img (PIL Image): Image to be transformed.
Returns:
Tensor: Converted Image
"""
#assert F._is_pil_image(img), "Input should be a PIL image (ToTensor_range transform)"
if F._is_pil_image(img):
img = F.to_tensor(img) # to_tensor normalizes data to (0,1)
img = img.to(self.dtype) # convert dtype
img = self.val_min + (img * (self.val_max - self.val_min)) # scale to val_min to val_max
return img
def __repr__(self) -> str:
s = (
f"{self.__class__.__name__}("
f"val_min={self.val_min}"
f", val_max={self.val_max}"
f", dtype={self.dtype}"
f")"
)
return s
def apply_PILJPEG(img, quality):
buffer = BytesIO()
img.save(buffer, format="JPEG", quality=quality)
buffer.seek(0) # move pointer to 0 so we can read them
img = PIL.Image.open(buffer).convert("RGB")
return img
def apply_cv2JPEG(img, quality):
# convert PIL image to cv2 image
img_cv2 = np.array(img)
img_cv2 = img_cv2[:,:,::-1]
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), quality]
result, encimg = cv2.imencode('.jpg', img_cv2, encode_param)
decimg = cv2.imdecode(encimg, 1)
return PIL.Image.fromarray(decimg[:,:,::-1])
def apply_randomJPEG(img, quality):
if random.random() < 0.5:
img = apply_PILJPEG(img, quality) # randomly apply PIL or CV2
else:
img = apply_cv2JPEG(img, quality)
return img
def resize_with_random_intpl(img, size):
"""
Perform resizing with random interpolation
"""
#intp_list = [InterpolationMode.NEAREST, InterpolationMode.BILINEAR, InterpolationMode.BICUBIC, InterpolationMode.LANCZOS, InterpolationMode.HAMMING, InterpolationMode.BOX]
intp_list = [InterpolationMode.BILINEAR, InterpolationMode.BICUBIC]
#interp_idx = random.randint(0, len(intp_list)-1)
interp = random.choice(intp_list)
# random interpolation somehow doesn't work
img = F.resize(img, size, interpolation=interp)
return img
class RandomResizeWithRandomIntpl(torch.nn.Module):
r"""
Reads PIL Image. Resizes with random interpolation. Returns torch tensor.
"""
def __init__(
self,
size_range: int=(112,448),
) -> Tensor:
super().__init__()
self.size_range = size_range
def forward(self, img) -> Tensor:
"""
Args:
img: PIL image to be transformed.
Returns:
Tensor: Converted Image
"""
assert F._is_pil_image(img), "Input should be a PIL image (RandomResizeWithRandomIntpl transform)"
# add resize
img = resize_with_random_intpl(img, random.randint(self.size_range[0], self.size_range[1]))
return img
def __repr__(self) -> str:
s = (
f"{self.__class__.__name__}()"
f" size_range={self.size_range}"
f")"
)
class ResizeWithRandomIntpl(torch.nn.Module):
r"""
Reads PIL Image. Resizes with random interpolation. Returns torch tensor.
"""
def __init__(
self,
size: int,
) -> Tensor:
super().__init__()
self.size = size
def forward(self, img) -> Tensor:
"""
Args:
img: PIL image to be transformed.
Returns:
Tensor: Converted Image
"""
assert F._is_pil_image(img), "Input should be a PIL image (ResizeWithRandomIntpl transform)"
# add resize
img = resize_with_random_intpl(img, self.size)
return img
def __repr__(self) -> str:
s = (
f"{self.__class__.__name__}("
f" size={self.size}"
f")"
)
return s
class RRCWithRandomIntpl(T.RandomResizedCrop):
r"""
Reads PIL Image. Randomly resized crop with random interpolation. Returns torch tensor.
"""
def __init__(
self,
size: int,
scale: Tuple[float, float] = (0.08, 1.0),
ratio: Tuple[float, float] = (3./4., 4./3.),
) -> Tensor:
super().__init__(size=size, scale=scale, ratio=ratio)
self.size = size
self.scale = scale
self.ratio = ratio
self.intp_list=[InterpolationMode.NEAREST, InterpolationMode.BILINEAR, InterpolationMode.BICUBIC, InterpolationMode.LANCZOS, InterpolationMode.HAMMING, InterpolationMode.BOX]
def forward(self, img) -> Tensor:
"""
Args:
img: PIL image to be transformed.
Returns:
Tensor: Converted Image
"""
assert F._is_pil_image(img), "Input should be a PIL image (RRCWithRandomIntpl transform)"
# add resize
i, j, h, w = self.get_params(img, self.scale, self.ratio)
#interp_idx = random.randint(0, len(self.intp_list)-1)
interp = random.choice(self.intp_list) # somehow doesn't work. Gives me error: TypeError: resized_crop() got multiple values for argument 'interpolation'
return F.resized_crop(img, i, j, h, w, self.size, interpolation=interp)
def __repr__(self) -> str:
s = (
f"{self.__class__.__name__}("
f" size={self.size}"
f", scale={self.scale}"
f", ratio={self.ratio}"
f")"
)
return s
class JPEGinMemory(torch.nn.Module):
r"""
Reads PIL Image. Compress JPEG in memory. Returns PIL Image.
"""
def __init__(
self,
quality_range = (30, 100),
method: str = "cv,pil",
dtype = torch.float32,
) -> Tensor:
super().__init__()
self.quality_range = quality_range
self.method = method.lower().split(',')
self.dtype = dtype
def forward(self, img) -> Tensor:
"""
Args:
img: PIL image to be transformed.jdt
Returns:
Tensor: Converted Image
"""
assert F._is_pil_image(img), "Input should be a PIL image (ResizeAndJPEGinMemory transform)"
if "cv" in self.method and "pil" in self.method:
img = apply_randomJPEG(img, random.randint(self.quality_range[0], self.quality_range[1]))
elif "cv" in self.method:
img = apply_cv2JPEG(img, random.randint(self.quality_range[0], self.quality_range[1]))
elif "pil" in self.method:
img = apply_PILJPEG(img, random.randint(self.quality_range[0], self.quality_range[1]))
return img
def __repr__(self) -> str:
s = (
f"{self.__class__.__name__}("
f", quality_range={self.quality_range}"
f", dtype={self.dtype}"
f")"
)
return s
class ResizeAndJPEGinMemory(torch.nn.Module):
r"""
Reads PIL Image. Resizes and compresses to JPEG in memory. Returns torch tensor.
"""
def __init__(
self,
size: int,
quality: int = 95,
method: str = "cv,pil",
dtype = torch.float32,
) -> Tensor:
super().__init__()
self.size = size
self.quality = quality
self.method = method.lower().split(',')
self.dtype = dtype
def forward(self, img) -> Tensor:
"""
Args:
img: PIL image to be transformed.
Returns:
Tensor: Converted Image
"""
assert F._is_pil_image(img), "Input should be a PIL image (ResizeAndJPEGinMemory transform)"
# add resize
img = F.resize(img, self.size, interpolation=InterpolationMode.BILINEAR) # this is the right way to resize! If torchvision updates, make sure that this resizes the smaller side to the specified size and keeps the aspect ratio
if "cv" in self.method and "pil" in self.method:
img = apply_randomJPEG(img, self.quality)
elif "cv" in self.method:
img = apply_cv2JPEG(img, self.quality)
elif "pil" in self.method:
img = apply_PILJPEG(img, self.quality)
return img
def __repr__(self) -> str:
s = (
f"{self.__class__.__name__}("
f" size={self.size}"
f", quality={self.quality}"
f", dtype={self.dtype}"
f")"
)
return s
class StochasticJPEG(torch.nn.Module):
r"""
Stochastically applies multiple JPEG compression and resizing to an image.
"""
def __init__(
self,
size: int, # final output size
quality: Tuple[int, int] = (50, 100), # quality range
num_jpeg: Tuple[int, int] = (1, 5), # number of jpegs to apply
jpeg_p: float = 0.5, # probability of applying JPEG compression
rrc_p: float = 0.5, # probability of applying random resized crop
rrc_scale: Tuple[float, float] = (0.75, 1.0), # random resize crop scale
rrc_ratio: Tuple[float, float] = (3./4., 4./3.), # random resize crop ratio
no_rrc: bool = False, # if True, no random resized crop is applied
dtype: type = torch.float32,
) -> Tensor:
"""
Initialize the CustomTransforms class.
Args:
size (int): The final output size.
quality (Tuple[int, int]): The quality range as a tuple of two integers.
num_jpeg (Tuple[int, int]): The number of jpegs to apply as a tuple of two integers.
p (float): The probability of applying the transform.
rrc_scale (Tuple[float, float]): The random resize crop scale as a tuple of two floats.
rrc_ratio (Tuple[float, float]): The random resize crop ratio as a tuple of two floats.
no_rrc (bool): If True, no random resized crop is applied.
dtype (type): The data type of the tensor.
Returns:
Tensor: The initialized CustomTransforms object.
"""
super().__init__()
self.size = size
self.quality = quality
self.num_jpeg = num_jpeg
self.jpeg_p = jpeg_p
self.rrc_p = rrc_p
self.rrc = torch.nn.Identity() if no_rrc else T.RandomResizedCrop(size=size, scale=rrc_scale, ratio=rrc_ratio, interpolation=InterpolationMode.BILINEAR)
self.dtype = dtype
def forward(self, img) -> Tensor:
"""
Args:
img: PIL image to be transformed.
Returns:
Tensor: Converted Image
"""
assert F._is_pil_image(img), "Input should be a PIL image (StochasticJPEG transform)"
# randomly sample p
count = self.num_jpeg[0]
for _ in range(self.num_jpeg[0]): # apply min number of jpegs and RRC first
img = self.rrc(img)
img = apply_randomJPEG(img, random.randint(self.quality[0], self.quality[1]))
while count < self.num_jpeg[1]:
if random.random() < self.p: # apply more jpegs with set probability.
img = self.rrc(img)
img = apply_randomJPEG(img, random.randint(self.quality[0], self.quality[1]))
count += 1
else:
break
return img
class RandomJPEG(torch.nn.Module):
"""
Randomly applies JPEG
Args:
quality: tuple of quality value range for JPEG
p: probability of applying JPEG
"""
def __init__(
self,
quality_list: tuple = (30, 100),
p: float = 0.5,
):
super().__init__()
self.quality_list = quality_list
self.p = p
def forward(self, img):
if random.random() < self.p:
img = apply_randomJPEG(img, random.randint(self.quality_list[0], self.quality_list[1]))
return img
class RandomGaussianBlur(torch.nn.Module):
"""
Randomly applies Gaussian Blur
Args:
p: probability of applying JPEG
sigma: tuple of sigma values for Gaussian Blur
"""
def __init__(
self,
p: float = 0.5,
sigma: Tuple[float, float] = (0.0, 3.0),
):
super().__init__()
self.p = p
self.sigma = sigma
def forward(self, img):
if random.random() < self.p:
sigma=random.uniform(self.sigma[0], self.sigma[1])
kernel_size=1+2*round(sigma*4.0) # default sigma used in scipy (https://github.com/scipy/scipy/blob/v1.13.1/scipy/ndimage/_filters.py#L286-L390)
img = F.gaussian_blur(img, kernel_size=kernel_size, sigma=sigma)
return img
class RandomPaddingAndResize(torch.nn.Module):
r"""
Reads PIL Image. Randomly applies padding, and resize it back to original resolution.
"""
def __init__(
self,
pad_percentage_range = (0.1, 0.1), # random padding percentage for x (width) and y (height)
padding_value_range = (0, 255), # random padding value range
) -> Tensor:
super().__init__()
self.pad_percentage_range = pad_percentage_range
self.padding_value_range = padding_value_range
def forward(self, img) -> Tensor:
"""
Args:
img: PIL image to be transformed.jdt
Returns:
Tensor: Converted Image
"""
assert F._is_pil_image(img), "Input should be a PIL image (ResizeAndJPEGinMemory transform)"
original_size = img.size
pad_x_l = random.uniform(0, self.pad_percentage_range[0]/2) # x-axis random padding ratio (left)
pad_x_r = random.uniform(0, self.pad_percentage_range[0]/2) # x-axis random padding ratio (right)
pad_y_l = random.uniform(0, self.pad_percentage_range[1]/2) # y-axis random padding ratio (left)
pad_y_r = random.uniform(0, self.pad_percentage_range[1]/2) # y-axis random padding ratio (right)
pad_fill = random.randint(int(self.padding_value_range[0]), int(self.padding_value_range[1])) # random padding fill value
img = F.pad(img, (int(pad_x_l*img.size[0]), int(pad_y_l*img.size[1]), int(pad_x_r*img.size[0]), int(pad_y_r*img.size[1])), fill=pad_fill, padding_mode='constant')
img = F.resize(img, original_size, interpolation=InterpolationMode.BILINEAR)
return img
def __repr__(self) -> str:
s = (
f"{self.__class__.__name__}("
f", pad_percentage_range={self.pad_percentage_range}"
f", padding_value_range={self.padding_value_range}"
f")"
)
return s
class RandomCutout(T.RandomErasing):
r"""
Random cutout with random numbers
"""
def __init__(
self,
p=0.5,
scale=(0.02, 0.33),
ratio=(0.3, 3.3),
value_range=(0, 255),
):
super().__init__(p=p, scale=scale, ratio=ratio)
self.value_range = value_range
def forward(self, img):
convert_to_pil=False
if F._is_pil_image(img):
img = F.pil_to_tensor(img)
convert_to_pil=True
if torch.rand(1) < self.p:
rand_value = random.randint(self.value_range[0], self.value_range[1])
# cast self.value to script acceptable type
if isinstance(rand_value, (int, float)):
rand_value = [float(rand_value)]
elif isinstance(rand_value, str):
rand_value = None
elif isinstance(rand_value, (list, tuple)):
rand_value = [float(v) for v in rand_value]
else:
rand_value = rand_value
if rand_value is not None and not (len(rand_value) in (1, img.shape[-3])):
raise ValueError(
"If value is a sequence, it should have either a single value or "
f"{img.shape[-3]} (number of input channels)"
)
x, y, h, w, v = self.get_params(img, self.scale, self.ratio, rand_value)
img = F.erase(img, x, y, h, w, v)
if convert_to_pil:
img = F.to_pil_image(img)
return img
class RandomVisualization(torch.nn.Module):
r"""
Randomly visualizes the fully augmented images by saving them at a specified directory.
"""
def __init__(
self,
save_dir: str = "/nfs/turbo/coe-ahowens-nobackup/jespark/visualizations/fake_img",
save_p: float = 0.01,
max_imgs: int = 500,
overwrite: bool = False,
) -> None:
super().__init__()
self.save_dir = save_dir
self.save_p = save_p
self.max_imgs = max_imgs
self.overwrite = overwrite
self.skip_namecheck=False
def next_available_filename(self, save_dir, max_imgs):
# Returns next available filename
# image format = visualization_{03d}_{i}.png, i=[0, max_imgs)
# let's not make it overwrite
imgs = os.listdir(save_dir)
imgs_list = [int(img.split("_")[-1].split(".")[0]) for img in imgs]
random_int = random.randint(0, 999)
if len(imgs_list) >= max_imgs:
if self.overwrite:
return random.choice(imgs) # overwrite random file from imgs
else:
self.skip_namecheck=True
return False
elif len(imgs_list) > 0:
next_int = max(imgs_list) + 1
return f"visualization_{next_int}_{random_int:03d}.png"
elif len(imgs_list) == 0:
return f"visualization_0_{random_int:03d}.png"
else: # uncaught, unexpected situation.
raise ValueError("Error in next_available_filename")
def forward(self, img) -> Tensor:
"""
Args:
img: PIL image to be transformed.
Returns:
Tensor: Converted Image
"""
if not self.skip_namecheck:
if random.random() < self.save_p:
os.makedirs(self.save_dir, exist_ok=True)
filename = self.next_available_filename(self.save_dir, self.max_imgs)
if filename:
img.save(os.path.join(self.save_dir, filename))
return img
class RandomStateAugmentation(torch.nn.Module):
r"""
Randomly applies augmentations given in the input
"""
def __init__(
self,
resize_size=256,
crop_size=224,
auglist="JPEGinMemory,RandomResizeWithRandomIntpl,RandomCrop,RandomHorizontalFlip,RandomVerticalFlip,RRCWithRandomIntpl,RandomRotation,RandomTranslate,RandomShear,RandomPadding",
min_augs='0',
max_augs='5',
):
"""
auglist: augmentation lists to apply. Input comma-separated string of augmentations.
min_augs: minimum number of augmentations to apply. (can be comma-separated string to denote per-augmentation minimum)
max_augs: maximum number of augmentations to apply. (can be comma-separated string to denote per-augmentation maximum)
"""
super().__init__()
self.resize_size=resize_size
self.crop_size=crop_size
self.auglist = self.parse_auglist(auglist)
# convert min_augs and max_augs to appropriate format
min_augs = self.parse_augnums(min_augs)
max_augs = self.parse_augnums(max_augs)
if type(min_augs) == list:
assert type(max_augs) == list, "max_augs should be list if min_augs is list."
assert len(min_augs) == len(auglist), "min_augs length should be equal to auglist length."
assert len(max_augs) == len(auglist), "max_augs length should be equal to auglist length."
# convert min_augs and max_augs to list if they are not
self.min_augs = [min_augs] * len(self.auglist) if type(min_augs) != list else min_augs
self.max_augs = [max_augs] * len(self.auglist) if type(max_augs) != list else max_augs
def parse_augnums(self, augsnum):
# parse min_augs or max_augs. They are expected to be a string of integers, optinally separated by commas.
augsnum_list = augsnum.split(",")
if len(augsnum_list) == 1:
return int(augsnum_list[0])
else:
return [int(aug) for aug in augsnum_list]
def parse_auglist(self, auglist):
# parse str-comma-separated auglist to list of augmentations
# default augmentation thoughts: "JPEGinMemory,RandomResizeWithRandomIntpl,RandomCrop,RandomHorizontalFlip,RandomVerticalFlip,RRCWithRandomIntpl,RandomRotation,RandomTranslate,RandomShear,RandomPadding"
auglist_list = auglist.split(",")
parsed_list = torch.nn.ModuleList()
for aug_name in auglist_list:
if aug_name=='singleJPEG':
parsed_list.append(ResizeAndJPEGinMemory(size=self.crop_size, quality=95, dtype=torch.float32))
if aug_name=='StochasticJPEG':
parsed_list.append(StochasticJPEG(size=self.crop_size, quality=(75, 100), num_jpeg=(1, 5), jpeg_p=0.5, rrc_p=0.5, rrc_scale=(0.75, 1.0), rrc_ratio=(3./4., 4./3.), no_rrc=False, dtype=torch.float32))
if aug_name=='JPEGinMemory':
parsed_list.append(JPEGinMemory(quality_range=(75, 100), dtype=torch.float32))
if aug_name=='RandomResizeWithRandomIntpl':
parsed_list.append(RandomResizeWithRandomIntpl(size_range=(self.crop_size+1,round(self.crop_size*1.228)))) # should not be smaller; causes issues with Random Crop.
if aug_name=='RandomCrop':
parsed_list.append(T.RandomCrop(self.crop_size))
if aug_name=='RandomHorizontalFlip':
parsed_list.append(T.RandomHorizontalFlip())
if aug_name=='RandomVerticalFlip':
parsed_list.append(T.RandomVerticalFlip())
if aug_name=='RRCWithRandomIntpl':
parsed_list.append(RRCWithRandomIntpl(size=self.crop_size, scale=(0.9, 1.0), ratio=(3./4., 4./3.)))
if aug_name=='RandomRotation':
parsed_list.append(T.RandomRotation(15, interpolation=InterpolationMode.BILINEAR))
if aug_name=='RandomTranslate':
parsed_list.append(T.RandomAffine(degrees=0, translate=(0.1, 0.1), scale=None, shear=None, interpolation=InterpolationMode.BILINEAR))
if aug_name=='RandomShear':
parsed_list.append(T.RandomAffine(degrees=0, translate=None, scale=None, shear=(-15, 15, -15, 15), interpolation=InterpolationMode.BILINEAR))
if aug_name=='RandomPadding' or aug_name=='RandomPaddingAndResize':
parsed_list.append(RandomPaddingAndResize(pad_percentage_range=(0.1, 0.1), padding_value_range=(0, 255)))
if aug_name=='RandomCutout':
parsed_list.append(RandomCutout(p=0.5, scale=(0.02, 0.06), ratio=(0.3, 3.3), value_range=(0, 255)))
return parsed_list
def generate_randAug_counts(self):
# Generates random required counts per augmentation
per_aug_counts = [0] * len(self.auglist)
for i in range(len(per_aug_counts)):
per_aug_counts[i] = random.randint(self.min_augs[i], self.max_augs[i])
return per_aug_counts
def convert_aug_counts_to_idxList(self, per_aug_counts):
# convert per augmentation count to list of indices. For example, [1,3,2] = [0,1,1,1,2,2]
idxList = []
for i in range(len(per_aug_counts)):
idxList += [i] * per_aug_counts[i]
return idxList
def check_if_complete(self, count, min_augs):
# not needed
if type(min_augs) == list:
min_augs_list = min_augs
else:
min_augs_list = [min_augs] * len(self.auglist)
for i in range(len(min_augs_list)):
if count[i] < min_augs_list[i]:
return False
return True
def forward(self, img) -> Tensor:
"""
Args:
img: PIL image to be transformed.
Returns:
Tensor: Converted Image
"""
assert F._is_pil_image(img), "Input should be a PIL image (RandomStateAugmentation transform)"
# randomly applies augmentation. Randomly walks through the list of augmentations and applies them. They should be applied at least "min_augs" number of times.
#count = [0] * len(self.auglist)