-
Notifications
You must be signed in to change notification settings - Fork 3
/
augment.py
287 lines (251 loc) · 8.71 KB
/
augment.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
"""
3Augment implementation
Data-augmentation (DA) based on dino DA (https://github.com/facebookresearch/dino)
and timm DA(https://github.com/rwightman/pytorch-image-models)
"""
import torch
import random
import numpy as np
import torchvision.transforms.functional as TF
from torchvision import transforms
from torchvision import datasets, transforms
from PIL import ImageFilter, ImageOps, Image
from timm.data.transforms import RandomResizedCropAndInterpolation, ToNumpy, ToTensor
class GaussianBlur(object):
"""
Apply Gaussian Blur to the PIL image.
"""
def __init__(self, p=0.1, radius_min=0.1, radius_max=2.0):
self.prob = p
self.radius_min = radius_min
self.radius_max = radius_max
def __call__(self, img):
do_it = random.random() <= self.prob
if not do_it:
return img
img = img.filter(
ImageFilter.GaussianBlur(
radius=random.uniform(self.radius_min, self.radius_max)
)
)
return img
class Solarization(object):
"""
Apply Solarization to the PIL image.
"""
def __init__(self, p=0.2):
self.p = p
def __call__(self, img):
if random.random() < self.p:
return ImageOps.solarize(img)
else:
return img
class gray_scale(object):
"""
Apply Solarization to the PIL image.
"""
def __init__(self, p=0.2):
self.p = p
self.transf = transforms.Grayscale(3)
def __call__(self, img):
if random.random() < self.p:
return self.transf(img)
else:
return img
class MultiCrop(object):
def __init__(
self, global_crops_scale, local_crops_scale, local_crops_number, rand_aug=False
):
rand_aug = transforms.RandAugment(num_ops=3)
self.local_crops_number = local_crops_number
color_jitter = transforms.RandomApply(
[
transforms.ColorJitter(
brightness=0.4, contrast=0.4, saturation=0.2, hue=0.1
)
],
p=0.8,
)
normalize = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
]
)
# self.local_transforms = transforms.Compose(
# [
# transforms.RandomResizedCrop(
# 96, scale=local_crops_scale, interpolation=Image.BICUBIC
# ),
# transforms.RandomHorizontalFlip(p=0.5),
# transforms.RandAugment(num_ops=3),
# GaussianBlur(1.0),
# normalize,
# ]
# )
# self.global_transforms1 = transforms.Compose(
# [
# transforms.RandomResizedCrop(
# 224, scale=local_crops_scale, interpolation=Image.BICUBIC
# ),
# transforms.Resize(224, interpolation=3),
# transforms.RandomHorizontalFlip(p=0.5),
# transforms.RandAugment(num_ops=2),
# GaussianBlur(1.0),
# normalize,
# ]
# )
# self.global_transforms2 = transforms.Compose(
# [
# transforms.RandomResizedCrop(
# 224, scale=local_crops_scale, interpolation=Image.BICUBIC
# ),
# transforms.Resize(224, interpolation=3),
# transforms.RandomHorizontalFlip(p=0.5),
# transforms.RandAugment(num_ops=3),
# GaussianBlur(1.0),
# normalize,
# ]
# )
if rand_aug:
print("==== Using Rand-Augment")
flip_and_color_jitter = transforms.RandAugment(num_ops=3)
else:
print("==== Using Normal flip and color jitter for multi-crop")
flip_and_color_jitter = transforms.Compose(
[
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomApply(
[
transforms.ColorJitter(
brightness=0.4, contrast=0.4, saturation=0.2, hue=0.1
)
],
p=0.8,
),
transforms.RandomGrayscale(p=0.2),
]
)
# first global crop
self.global_transforms1 = transforms.Compose(
[
transforms.RandomResizedCrop(
224, scale=global_crops_scale, interpolation=Image.BICUBIC
),
transforms.Resize(224, interpolation=3),
flip_and_color_jitter,
GaussianBlur(1.0),
normalize,
]
)
# second global crop
self.global_transforms2 = transforms.Compose(
[
transforms.RandomResizedCrop(
224, scale=global_crops_scale, interpolation=Image.BICUBIC
),
transforms.Resize(224, interpolation=3),
flip_and_color_jitter,
GaussianBlur(0.1),
Solarization(0.2),
normalize,
]
)
# transformation for the local small crops
self.local_transforms = transforms.Compose(
[
transforms.RandomResizedCrop(
96, scale=local_crops_scale, interpolation=Image.BICUBIC
),
flip_and_color_jitter,
GaussianBlur(p=0.5),
normalize,
]
)
def __call__(self, image):
crops = []
# crops.append(self.global_transfo1(image))
# crops.append(self.global_transfo2(image))
crops.append(self.global_transforms1(image))
crops.append(self.global_transforms2(image))
for _ in range(self.local_crops_number):
crops.append(self.local_transforms(image))
return crops
class horizontal_flip(object):
"""
Apply Solarization to the PIL image.
"""
def __init__(self, p=0.2, activate_pred=False):
self.p = p
self.transf = transforms.RandomHorizontalFlip(p=1.0)
def __call__(self, img):
if random.random() < self.p:
return self.transf(img)
else:
return img
def three_augment(args=None):
"""
* 3-Augment from DeiT-III
* (https://arxiv.org/pdf/2204.07118.pdf)
"""
img_size = args.input_size
remove_random_resized_crop = args.src
mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
secondary_tfl = [
transforms.Resize(img_size, interpolation=3),
transforms.RandomCrop(img_size, padding=4, padding_mode="reflect"),
transforms.RandomHorizontalFlip(),
transforms.RandomChoice(
[gray_scale(p=1.0), Solarization(p=1.0), GaussianBlur(p=1.0)]
),
]
if args.color_jitter is not None and not args.color_jitter == 0:
secondary_tfl.append(
transforms.ColorJitter(
args.color_jitter, args.color_jitter, args.color_jitter
)
)
final_tfl = [
transforms.ToTensor(),
transforms.Normalize(mean=torch.tensor(mean), std=torch.tensor(std)),
]
return transforms.Compose(secondary_tfl + final_tfl)
def new_data_aug_generator(args=None):
img_size = args.input_size
remove_random_resized_crop = args.src
named_loss = args.named_loss
mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
primary_tfl = []
scale = (0.08, 1.0)
interpolation = "bicubic"
if remove_random_resized_crop:
primary_tfl = [
transforms.Resize(img_size, interpolation=3),
transforms.RandomCrop(img_size, padding=4, padding_mode="reflect"),
transforms.RandomHorizontalFlip(),
]
else:
primary_tfl = [
RandomResizedCropAndInterpolation(
img_size, scale=scale, interpolation=interpolation
),
transforms.RandomHorizontalFlip(),
]
secondary_tfl = [
transforms.RandomChoice(
[gray_scale(p=1.0), Solarization(p=1.0), GaussianBlur(p=1.0)]
)
]
if args.color_jitter is not None and not args.color_jitter == 0:
secondary_tfl.append(
transforms.ColorJitter(
args.color_jitter, args.color_jitter, args.color_jitter
)
)
final_tfl = [
transforms.ToTensor(),
transforms.Normalize(mean=torch.tensor(mean), std=torch.tensor(std)),
]
return transforms.Compose(primary_tfl + secondary_tfl + final_tfl)