-
Notifications
You must be signed in to change notification settings - Fork 13
/
dataloader_cifar.py
491 lines (443 loc) · 16.8 KB
/
dataloader_cifar.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
import json
import os
import pickle
import random
import _pickle as cPickle
import numpy as np
import torch
import torchvision.transforms as transforms
from PIL import Image
from torch.utils.data import DataLoader, Dataset
from autoaugment import CIFAR10Policy
from randaugment import *
def unpickle(file):
with open(file, "rb") as fo:
return cPickle.load(fo, encoding="latin1")
transform_none_10_compose = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
]
)
transform_none_100_compose = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize((0.507, 0.487, 0.441), (0.267, 0.256, 0.276)),
]
)
transform_weak_10_compose = transforms.Compose(
[
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
]
)
transform_weak_100_compose = transforms.Compose(
[
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.507, 0.487, 0.441), (0.267, 0.256, 0.276)),
]
)
transform_strong_10_compose = transforms.Compose(
[
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
CIFAR10Policy(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
]
)
transform_strong_100_compose = transforms.Compose(
[
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
CIFAR10Policy(),
transforms.ToTensor(),
transforms.Normalize((0.507, 0.487, 0.441), (0.267, 0.256, 0.276)),
]
)
transform_strong_randaugment_10_compose = transforms.Compose(
[
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
# RandAugment(1, 6),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
]
)
transform_strong_randaugment_100_compose = transforms.Compose(
[
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
# RandAugment(1, 6),
transforms.ToTensor(),
transforms.Normalize((0.507, 0.487, 0.441), (0.267, 0.256, 0.276)),
]
)
class cifar_dataset(Dataset):
def __init__(
self,
dataset,
r,
noise_mode,
root_dir,
transform,
mode,
noise_file="",
preaug_file="",
pred=[],
probability=[],
):
self.r = r
self.transform = transform
self.mode = mode
self.preaug_file = preaug_file
self.transition = {
0: 0,
2: 0,
4: 7,
7: 7,
1: 1,
9: 1,
3: 5,
5: 3,
6: 6,
8: 8,
} # class transition for asymmetric noise
if self.mode == "test":
if dataset == "cifar10":
test_dic = unpickle("%s/test_batch" % root_dir)
self.test_data = test_dic["data"]
self.test_data = self.test_data.reshape((10000, 3, 32, 32))
self.test_data = self.test_data.transpose((0, 2, 3, 1))
self.test_label = test_dic["labels"]
elif dataset == "cifar100":
test_dic = unpickle("%s/test" % root_dir)
self.test_data = test_dic["data"]
self.test_data = self.test_data.reshape((10000, 3, 32, 32))
self.test_data = self.test_data.transpose((0, 2, 3, 1))
self.test_label = test_dic["fine_labels"]
else:
train_data = []
train_label = []
if dataset == "cifar10":
for n in range(1, 6):
dpath = "%s/data_batch_%d" % (root_dir, n)
data_dic = unpickle(dpath)
train_data.append(data_dic["data"])
train_label = train_label + data_dic["labels"]
train_data = np.concatenate(train_data)
elif dataset == "cifar100":
train_dic = unpickle("%s/train" % root_dir)
train_data = train_dic["data"]
train_label = train_dic["fine_labels"]
train_data = train_data.reshape((50000, 3, 32, 32))
train_data = train_data.transpose((0, 2, 3, 1))
self.train_label = train_label
if os.path.exists(noise_file):
noise_label = json.load(open(noise_file, "r"))
else: # inject noise
noise_label = []
idx = list(range(50000))
random.shuffle(idx)
num_noise = int(self.r * 50000)
noise_idx = idx[:num_noise]
for i in range(50000):
if i in noise_idx:
if noise_mode == "sym":
if dataset == "cifar10":
noiselabel = random.randint(0, 9)
elif dataset == "cifar100":
noiselabel = random.randint(0, 99)
noise_label.append(noiselabel)
elif noise_mode == "asym":
noiselabel = self.transition[train_label[i]]
noise_label.append(noiselabel)
else:
noise_label.append(train_label[i])
print(f"saving noisy labels to {noise_file}...")
json.dump(noise_label, open(noise_file, "w"), indent=4, sort_keys=True)
if self.preaug_file != "":
all_augmented = torch.load(self.preaug_file)
train_data = np.concatenate(
(
train_data,
np.array(all_augmented["samples"], dtype=np.uint8).transpose(
(0, 2, 3, 1)
),
)
)
noise_label = np.concatenate(
(
noise_label,
np.array(all_augmented["labels"]),
)
)
if self.mode == "all":
self.train_data = train_data
self.noise_label = noise_label
else:
if self.mode == "labeled":
pred_idx = pred.nonzero()[0]
self.probability = [probability[i] for i in pred_idx]
elif self.mode == "unlabeled":
pred_idx = (1 - pred).nonzero()[0]
self.train_data = train_data[pred_idx]
self.noise_label = [noise_label[i] for i in pred_idx]
print("%s data has a size of %d" % (self.mode, len(self.train_data)))
def __getitem__(self, index):
if self.mode == "labeled":
img, target, prob = (
self.train_data[index],
self.noise_label[index],
self.probability[index],
)
img = Image.fromarray(img)
img1 = self.transform[0](img)
img2 = self.transform[1](img)
if self.transform[2] == None:
img3 = img1
img4 = img2
else:
img3 = self.transform[2](img)
img4 = self.transform[3](img)
return img1, img2, img3, img4, target, prob
elif self.mode == "unlabeled":
img = self.train_data[index]
img = Image.fromarray(img)
img1 = self.transform[0](img)
img2 = self.transform[1](img)
if self.transform[2] == None:
img3 = img1
img4 = img2
else:
img3 = self.transform[2](img)
img4 = self.transform[3](img)
return img1, img2, img3, img4
elif self.mode == "all":
img, target = self.train_data[index], self.noise_label[index]
img = Image.fromarray(img)
img = self.transform(img)
return img, target, index
elif self.mode == "test":
img, target = self.test_data[index], self.test_label[index]
img = Image.fromarray(img)
img = self.transform(img)
return img, target
def __len__(self):
if self.mode != "test":
return len(self.train_data)
else:
return len(self.test_data)
class cifar_dataloader:
# workaround for windows because
# python can't pickle lambdas :(
def prob_transform_100(self, x):
if random.random() < self.warmup_aug_prob:
return transform_strong_100_compose(x)
else:
return transform_weak_100_compose(x)
def prob_transform_10(self, x):
if random.random() < self.warmup_aug_prob:
return transform_strong_10_compose(x)
else:
return transform_weak_10_compose(x)
def transform_strong_100(self, x):
return transform_strong_100_compose(x)
def transform_strong_10(self, x):
return transform_strong_10_compose(x)
def transform_weak_100(self, x):
return transform_weak_100_compose(x)
def transform_weak_10(self, x):
return transform_weak_10_compose(x)
def transform_strong_randaugment_10(self, x):
return transform_strong_randaugment_10_compose(x)
def transform_strong_randaugment_100(self, x):
return transform_strong_randaugment_100_compose(x)
def transform_none_10(self, x):
return transform_none_10_compose(x)
def transform_none_100(self, x):
return transform_none_100_compose(x)
def __init__(
self,
dataset,
r,
noise_mode,
batch_size,
warmup_batch_size,
num_workers,
root_dir,
noise_file="",
preaug_file="",
augmentation_strategy={},
):
self.dataset = dataset
self.r = r
self.noise_mode = noise_mode
self.batch_size = batch_size
self.warmup_batch_size = warmup_batch_size
self.num_workers = num_workers
self.root_dir = root_dir
self.noise_file = noise_file
self.preaug_file = preaug_file
self.warmup_aug_prob = augmentation_strategy.warmup_aug_probability
if "randaugment_params" in augmentation_strategy:
p = augmentation_strategy["randaugment_params"]
a = RandAugment(p["n"], p["m"])
transform_strong_randaugment_10_compose.transforms.insert(2, a)
transform_strong_randaugment_100_compose.transforms.insert(2, a)
self.transforms = {
"warmup": self.__getattribute__(augmentation_strategy.warmup_transform),
"unlabeled": [None for i in range(4)],
"labeled": [None for i in range(4)],
"test": None,
}
# workaround so it works on both windows and linux
for i in range(len(augmentation_strategy.unlabeled_transforms)):
self.transforms["unlabeled"][i] = self.__getattribute__(
augmentation_strategy.unlabeled_transforms[i]
)
for i in range(len(augmentation_strategy.labeled_transforms)):
self.transforms["labeled"][i] = self.__getattribute__(
augmentation_strategy.labeled_transforms[i]
)
if self.dataset == "cifar10":
self.transforms["test"] = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize(
(0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)
),
]
)
elif self.dataset == "cifar100":
self.transforms["test"] = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize((0.507, 0.487, 0.441), (0.267, 0.256, 0.276)),
]
)
if augmentation_strategy.preaugment and not os.path.exists(self.preaug_file):
print(f"Preaugmenting and saving to {self.preaug_file}...")
test_dataset = cifar_dataset(
dataset=self.dataset,
noise_mode=self.noise_mode,
noise_file=self.noise_file,
r=self.r,
root_dir=self.root_dir,
transform=self.__getattribute__(
augmentation_strategy.preaugment["transform"]
),
mode="all",
)
test_loader = DataLoader(
dataset=test_dataset,
batch_size=self.batch_size,
shuffle=False,
num_workers=self.num_workers,
)
all_augmented = {"samples": [], "labels": []}
for i in range(augmentation_strategy.preaugment["ratio"] - 1):
for img, target, index in test_loader:
for j in range(len(img)):
all_augmented["samples"].append(img[j].numpy())
all_augmented["labels"].append(target[j])
torch.save(all_augmented, self.preaug_file)
def run(self, mode, pred=[], prob=[]):
if mode == "warmup":
all_dataset = cifar_dataset(
dataset=self.dataset,
noise_mode=self.noise_mode,
r=self.r,
root_dir=self.root_dir,
transform=self.transforms["warmup"],
mode="all",
noise_file=self.noise_file,
preaug_file=self.preaug_file,
)
trainloader = DataLoader(
dataset=all_dataset,
batch_size=self.warmup_batch_size,
shuffle=True,
num_workers=self.num_workers,
)
return trainloader
elif mode == "train":
labeled_dataset = cifar_dataset(
dataset=self.dataset,
noise_mode=self.noise_mode,
r=self.r,
root_dir=self.root_dir,
transform=self.transforms["labeled"],
mode="labeled",
noise_file=self.noise_file,
pred=pred,
probability=prob,
preaug_file=self.preaug_file,
)
labeled_trainloader = DataLoader(
dataset=labeled_dataset,
batch_size=self.batch_size,
shuffle=True,
num_workers=self.num_workers,
)
unlabeled_dataset = cifar_dataset(
dataset=self.dataset,
noise_mode=self.noise_mode,
r=self.r,
root_dir=self.root_dir,
transform=self.transforms["unlabeled"],
mode="unlabeled",
noise_file=self.noise_file,
pred=pred,
preaug_file=self.preaug_file,
)
unlabeled_trainloader = DataLoader(
dataset=unlabeled_dataset,
batch_size=self.batch_size,
shuffle=True,
num_workers=self.num_workers,
)
return labeled_trainloader, unlabeled_trainloader
elif mode == "test":
test_dataset = cifar_dataset(
dataset=self.dataset,
noise_mode=self.noise_mode,
r=self.r,
root_dir=self.root_dir,
transform=self.transforms["test"],
mode="test",
)
test_loader = DataLoader(
dataset=test_dataset,
batch_size=self.batch_size,
shuffle=False,
num_workers=self.num_workers,
)
return test_loader
elif mode == "eval_train":
eval_dataset = cifar_dataset(
dataset=self.dataset,
noise_mode=self.noise_mode,
r=self.r,
root_dir=self.root_dir,
transform=self.transforms["test"],
# ^- this is a small mistake in our implementation!
# augmentations for eval_train should be weak, not none.
# although this has a neglibible effect on performance,
# we feel it is important to note for future readers of this code.
# see: https://github.com/KentoNishi/Augmentation-for-LNL/issues/4
mode="all",
noise_file=self.noise_file,
preaug_file=self.preaug_file,
)
eval_loader = DataLoader(
dataset=eval_dataset,
batch_size=self.batch_size,
shuffle=False,
num_workers=self.num_workers,
)
return eval_loader