-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataloader.py
387 lines (339 loc) · 11.4 KB
/
dataloader.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
import numpy as np
from torchvision import transforms
"""Base augmentations operators."""
from PIL import ImageOps, ImageEnhance, Image
import torch
# ImageNet code should change this value
IMAGE_SIZE = 224
def int_parameter(level, maxval):
"""Helper function to scale `val` between 0 and maxval .
Args:
level: Level of the operation that will be between [0, `PARAMETER_MAX`].
maxval: Maximum value that the operation can have. This will be scaled to
level/PARAMETER_MAX.
Returns:
An int that results from scaling `maxval` according to `level`.
"""
return int(level * maxval / 10)
def float_parameter(level, maxval):
"""Helper function to scale `val` between 0 and maxval.
Args:
level: Level of the operation that will be between [0, `PARAMETER_MAX`].
maxval: Maximum value that the operation can have. This will be scaled to
level/PARAMETER_MAX.
Returns:
A float that results from scaling `maxval` according to `level`.
"""
return float(level) * maxval / 10.
def sample_level(n):
return np.random.uniform(low=0.1, high=n)
def autocontrast(pil_img, _):
return ImageOps.autocontrast(pil_img)
def equalize(pil_img, _):
return ImageOps.equalize(pil_img)
def posterize(pil_img, level):
level = int_parameter(sample_level(level), 4)
return ImageOps.posterize(pil_img, 4 - level)
def rotate(pil_img, level):
degrees = int_parameter(sample_level(level), 30)
if np.random.uniform() > 0.5:
degrees = -degrees
return pil_img.rotate(degrees, resample=Image.BILINEAR)
def solarize(pil_img, level):
level = int_parameter(sample_level(level), 256)
return ImageOps.solarize(pil_img, 256 - level)
def shear_x(pil_img, level):
level = float_parameter(sample_level(level), 0.3)
if np.random.uniform() > 0.5:
level = -level
return pil_img.transform((IMAGE_SIZE, IMAGE_SIZE),
Image.AFFINE, (1, level, 0, 0, 1, 0),
resample=Image.BILINEAR)
def shear_y(pil_img, level):
level = float_parameter(sample_level(level), 0.3)
if np.random.uniform() > 0.5:
level = -level
return pil_img.transform((IMAGE_SIZE, IMAGE_SIZE),
Image.AFFINE, (1, 0, 0, level, 1, 0),
resample=Image.BILINEAR)
def translate_x(pil_img, level):
level = int_parameter(sample_level(level), IMAGE_SIZE / 3)
if np.random.random() > 0.5:
level = -level
return pil_img.transform((IMAGE_SIZE, IMAGE_SIZE),
Image.AFFINE, (1, 0, level, 0, 1, 0),
resample=Image.BILINEAR)
def translate_y(pil_img, level):
level = int_parameter(sample_level(level), IMAGE_SIZE / 3)
if np.random.random() > 0.5:
level = -level
return pil_img.transform((IMAGE_SIZE, IMAGE_SIZE),
Image.AFFINE, (1, 0, 0, 0, 1, level),
resample=Image.BILINEAR)
# operation that overlaps with ImageNet-C's test set
def color(pil_img, level):
level = float_parameter(sample_level(level), 1.8) + 0.1
return ImageEnhance.Color(pil_img).enhance(level)
# operation that overlaps with ImageNet-C's test set
def contrast(pil_img, level):
level = float_parameter(sample_level(level), 1.8) + 0.1
return ImageEnhance.Contrast(pil_img).enhance(level)
# operation that overlaps with ImageNet-C's test set
def brightness(pil_img, level):
level = float_parameter(sample_level(level), 1.8) + 0.1
return ImageEnhance.Brightness(pil_img).enhance(level)
# operation that overlaps with ImageNet-C's test set
def sharpness(pil_img, level):
level = float_parameter(sample_level(level), 1.8) + 0.1
return ImageEnhance.Sharpness(pil_img).enhance(level)
augmentations = [
autocontrast, equalize, posterize, rotate, solarize, shear_x, shear_y,
translate_x, translate_y
]
augmentations_all = [
autocontrast, equalize, posterize, rotate, solarize, shear_x, shear_y,
translate_x, translate_y, color, contrast, brightness, sharpness
]
try:
from torchvision.transforms import InterpolationMode
BICUBIC = InterpolationMode.BICUBIC
except ImportError:
BICUBIC = Image.BICUBIC
class AugMixAugmenter(object):
def __init__(self, base_transform, preprocess, n_views=2, augmix=False,
severity=1):
self.base_transform = base_transform
self.preprocess = preprocess
self.n_views = n_views
if augmix:
self.aug_list = augmentations.augmentations
else:
self.aug_list = []
self.severity = severity
def __call__(self, x):
image = self.preprocess(self.base_transform(x))
views = [augmix(x, self.preprocess, self.aug_list, self.severity) for _ in range(self.n_views)]
return [image] + views
def augmix(image, preprocess, aug_list, severity=1):
preaugment = get_preaugment()
x_orig = preaugment(image)
x_processed = preprocess(x_orig)
if len(aug_list) == 0:
return x_processed
w = np.float32(np.random.dirichlet([1.0, 1.0, 1.0]))
m = np.float32(np.random.beta(1.0, 1.0))
mix = torch.zeros_like(x_processed)
for i in range(3):
x_aug = x_orig.copy()
for _ in range(np.random.randint(1, 4)):
x_aug = np.random.choice(aug_list)(x_aug, severity)
mix += w[i] * preprocess(x_aug)
mix = m * x_processed + (1 - m) * mix
return mix
def get_preaugment():
return transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
])
wordnet_classes = {
"n01498041": "stingray",
"n01531178": "goldfinch",
"n01534433": "junco",
"n01558993": "American robin",
"n01580077": "jay",
"n01614925": "bald eagle",
"n01616318": "vulture",
"n01631663": "newt",
"n01641577": "American bullfrog",
"n01669191": "box turtle",
"n01677366": "green iguana",
"n01687978": "agama",
"n01694178": "chameleon",
"n01698640": "American alligator",
"n01735189": "garter snake",
"n01770081": "harvestman",
"n01770393": "scorpion",
"n01774750": "tarantula",
"n01784675": "centipede",
"n01819313": "sulphur-crested cockatoo",
"n01820546": "lorikeet",
"n01833805": "hummingbird",
"n01843383": "toucan",
"n01847000": "duck",
"n01855672": "goose",
"n01882714": "koala",
"n01910747": "jellyfish",
"n01914609": "sea anemone",
"n01924916": "flatworm",
"n01944390": "snail",
"n01985128": "crayfish",
"n01986214": "hermit crab",
"n02007558": "flamingo",
"n02009912": "great egret",
"n02037110": "oystercatcher",
"n02051845": "pelican",
"n02077923": "sea lion",
"n02085620": "Chihuahua",
"n02099601": "Golden Retriever",
"n02106550": "Rottweiler",
"n02106662": "German Shepherd Dog",
"n02110958": "pug",
"n02119022": "red fox",
"n02123394": "Persian cat",
"n02127052": "lynx",
"n02129165": "lion",
"n02133161": "American black bear",
"n02137549": "mongoose",
"n02165456": "ladybug",
"n02174001": "rhinoceros beetle",
"n02177972": "weevil",
"n02190166": "fly",
"n02206856": "bee",
"n02219486": "ant",
"n02226429": "grasshopper",
"n02231487": "stick insect",
"n02233338": "cockroach",
"n02236044": "mantis",
"n02259212": "leafhopper",
"n02268443": "dragonfly",
"n02279972": "monarch butterfly",
"n02280649": "small white",
"n02281787": "gossamer-winged butterfly",
"n02317335": "starfish",
"n02325366": "cottontail rabbit",
"n02346627": "porcupine",
"n02356798": "fox squirrel",
"n02361337": "marmot",
"n02410509": "bison",
"n02445715": "skunk",
"n02454379": "armadillo",
"n02486410": "baboon",
"n02492035": "white-headed capuchin",
"n02504458": "African bush elephant",
"n02655020": "pufferfish",
"n02669723": "academic gown",
"n02672831": "accordion",
"n02676566": "acoustic guitar",
"n02690373": "airliner",
"n02701002": "ambulance",
"n02730930": "apron",
"n02777292": "balance beam",
"n02782093": "balloon",
"n02787622": "banjo",
"n02793495": "barn",
"n02797295": "wheelbarrow",
"n02802426": "basketball",
"n02814860": "lighthouse",
"n02815834": "beaker",
"n02837789": "bikini",
"n02879718": "bow",
"n02883205": "bow tie",
"n02895154": "breastplate",
"n02906734": "broom",
"n02948072": "candle",
"n02951358": "canoe",
"n02980441": "castle",
"n02992211": "cello",
"n02999410": "chain",
"n03014705": "chest",
"n03026506": "Christmas stocking",
"n03124043": "cowboy boot",
"n03125729": "cradle",
"n03187595": "rotary dial telephone",
"n03196217": "digital clock",
"n03223299": "doormat",
"n03250847": "drumstick",
"n03255030": "dumbbell",
"n03291819": "envelope",
"n03325584": "feather boa",
"n03355925": "flagpole",
"n03384352": "forklift",
"n03388043": "fountain",
"n03417042": "garbage truck",
"n03443371": "goblet",
"n03444034": "go-kart",
"n03445924": "golf cart",
"n03452741": "grand piano",
"n03483316": "hair dryer",
"n03584829": "clothes iron",
"n03590841": "jack-o'-lantern",
"n03594945": "jeep",
"n03617480": "kimono",
"n03666591": "lighter",
"n03670208": "limousine",
"n03717622": "manhole cover",
"n03720891": "maraca",
"n03721384": "marimba",
"n03724870": "mask",
"n03775071": "mitten",
"n03788195": "mosque",
"n03804744": "nail",
"n03837869": "obelisk",
"n03840681": "ocarina",
"n03854065": "organ",
"n03888257": "parachute",
"n03891332": "parking meter",
"n03935335": "piggy bank",
"n03982430": "billiard table",
"n04019541": "hockey puck",
"n04033901": "quill",
"n04039381": "racket",
"n04067472": "reel",
"n04086273": "revolver",
"n04099969": "rocking chair",
"n04118538": "rugby ball",
"n04131690": "salt shaker",
"n04133789": "sandal",
"n04141076": "saxophone",
"n04146614": "school bus",
"n04147183": "schooner",
"n04179913": "sewing machine",
"n04208210": "shovel",
"n04235860": "sleeping bag",
"n04252077": "snowmobile",
"n04252225": "snowplow",
"n04254120": "soap dispenser",
"n04270147": "spatula",
"n04275548": "spider web",
"n04310018": "steam locomotive",
"n04317175": "stethoscope",
"n04344873": "couch",
"n04347754": "submarine",
"n04355338": "sundial",
"n04366367": "suspension bridge",
"n04376876": "syringe",
"n04389033": "tank",
"n04399382": "teddy bear",
"n04442312": "toaster",
"n04456115": "torch",
"n04482393": "tricycle",
"n04507155": "umbrella",
"n04509417": "unicycle",
"n04532670": "viaduct",
"n04540053": "volleyball",
"n04554684": "washing machine",
"n04562935": "water tower",
"n04591713": "wine bottle",
"n04606251": "shipwreck",
"n07583066": "guacamole",
"n07695742": "pretzel",
"n07697313": "cheeseburger",
"n07697537": "hot dog",
"n07714990": "broccoli",
"n07718472": "cucumber",
"n07720875": "bell pepper",
"n07734744": "mushroom",
"n07749582": "lemon",
"n07753592": "banana",
"n07760859": "custard apple",
"n07768694": "pomegranate",
"n07831146": "carbonara",
"n09229709": "bubble",
"n09246464": "cliff",
"n09472597": "volcano",
"n09835506": "baseball player",
"n11879895": "rapeseed",
"n12057211": "yellow lady's slipper",
"n12144580": "corn",
"n12267677": "acorn"
}