-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdataset.py
executable file
·546 lines (455 loc) · 22 KB
/
dataset.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
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
from PIL import Image
from tqdm import tqdm
from numpy.testing import assert_array_almost_equal
import numpy as np
import os
import torch
import random
from data.mnist import MNIST
from data.cifar import CIFAR10, CIFAR100
def build_for_cifar100(size, noise):
""" random flip between two random classes.
"""
assert(noise >= 0.) and (noise <= 1.)
P = (1. - noise) * np.eye(size)
for i in np.arange(size - 1):
P[i, i+1] = noise
# adjust last row
P[size-1, 0] = noise
assert_array_almost_equal(P.sum(axis=1), 1, 1)
return P
def multiclass_noisify(y, P, random_state=0):
""" Flip classes according to transition probability matrix T.
It expects a number between 0 and the number of classes - 1.
"""
assert P.shape[0] == P.shape[1]
assert np.max(y) < P.shape[0]
# row stochastic matrix
assert_array_almost_equal(P.sum(axis=1), np.ones(P.shape[1]))
assert (P >= 0.0).all()
m = y.shape[0]
new_y = y.copy()
flipper = np.random.RandomState(random_state)
for idx in np.arange(m):
i = y[idx]
# draw a vector with only an 1
flipped = flipper.multinomial(1, P[i, :], 1)[0]
new_y[idx] = np.where(flipped == 1)[0]
return new_y
def other_class(n_classes, current_class):
"""
Returns a list of class indices excluding the class indexed by class_ind
:param nb_classes: number of classes in the task
:param class_ind: the class index to be omitted
:return: one random class that != class_ind
"""
if current_class < 0 or current_class >= n_classes:
error_str = "class_ind must be within the range (0, nb_classes - 1)"
raise ValueError(error_str)
other_class_list = list(range(n_classes))
other_class_list.remove(current_class)
other_class = np.random.choice(other_class_list)
return other_class
class MNISTNoisy(datasets.MNIST):
def __init__(self, root, train=True, transform=None, target_transform=None, download=True, nosiy_rate=0.0, asym=False, seed=0):
super(MNISTNoisy, self).__init__(root, transform=transform, target_transform=target_transform, download=download)
self.targets = self.targets.numpy()
if asym:
P = np.eye(10)
n = nosiy_rate
P[7, 7], P[7, 1] = 1. - n, n
# 2 -> 7
P[2, 2], P[2, 7] = 1. - n, n
# 5 <-> 6
P[5, 5], P[5, 6] = 1. - n, n
P[6, 6], P[6, 5] = 1. - n, n
# 3 -> 8
P[3, 3], P[3, 8] = 1. - n, n
y_train_noisy = multiclass_noisify(self.targets, P=P, random_state=seed)
actual_noise = (y_train_noisy != self.targets).mean()
assert actual_noise > 0.0
print('Actual noise %.2f' % actual_noise)
self.targets = y_train_noisy
else:
n_samples = len(self.targets)
n_noisy = int(nosiy_rate * n_samples)
print("%d Noisy samples" % (n_noisy))
class_index = [np.where(np.array(self.targets) == i)[0] for i in range(10)]
class_noisy = int(n_noisy / 10)
noisy_idx = []
for d in range(10):
noisy_class_index = np.random.choice(class_index[d], class_noisy, replace=False)
noisy_idx.extend(noisy_class_index)
print("Class %d, number of noisy % d" % (d, len(noisy_class_index)))
for i in noisy_idx:
self.targets[i] = other_class(n_classes=10, current_class=self.targets[i])
print(len(noisy_idx))
print("Print noisy label generation statistics:")
for i in range(10):
n_noisy = np.sum(np.array(self.targets) == i)
print("Noisy class %s, has %s samples." % (i, n_noisy))
return
class cifar10Nosiy(datasets.CIFAR10):
def __init__(self, root, train=True, transform=None, target_transform=None, download=True, nosiy_rate=0.0, asym=False):
super(cifar10Nosiy, self).__init__(root, download=download, transform=transform, target_transform=target_transform)
self.download = download
if asym:
# automobile < - truck, bird -> airplane, cat <-> dog, deer -> horse
source_class = [9, 2, 3, 5, 4]
target_class = [1, 0, 5, 3, 7]
for s, t in zip(source_class, target_class):
cls_idx = np.where(np.array(self.targets) == s)[0]
n_noisy = int(nosiy_rate * cls_idx.shape[0])
noisy_sample_index = np.random.choice(cls_idx, n_noisy, replace=False)
for idx in noisy_sample_index:
self.targets[idx] = t
return
elif nosiy_rate > 0:
n_samples = len(self.targets)
n_noisy = int(nosiy_rate * n_samples)
print("%d Noisy samples" % (n_noisy))
class_index = [np.where(np.array(self.targets) == i)[0] for i in range(10)]
class_noisy = int(n_noisy / 10)
noisy_idx = []
for d in range(10):
noisy_class_index = np.random.choice(class_index[d], class_noisy, replace=False)
noisy_idx.extend(noisy_class_index)
print("Class %d, number of noisy % d" % (d, len(noisy_class_index)))
for i in noisy_idx:
self.targets[i] = other_class(n_classes=10, current_class=self.targets[i])
print(len(noisy_idx))
print("Print noisy label generation statistics:")
for i in range(10):
n_noisy = np.sum(np.array(self.targets) == i)
print("Noisy class %s, has %s samples." % (i, n_noisy))
return
class cifar100Nosiy(datasets.CIFAR100):
def __init__(self, root, train=True, transform=None, target_transform=None, download=True, nosiy_rate=0.0, asym=False, seed=0):
super(cifar100Nosiy, self).__init__(root, download=download, transform=transform, target_transform=target_transform)
self.download = download
if asym:
"""mistakes are inside the same superclass of 10 classes, e.g. 'fish'
"""
nb_classes = 100
P = np.eye(nb_classes)
n = nosiy_rate
nb_superclasses = 20
nb_subclasses = 5
if n > 0.0:
for i in np.arange(nb_superclasses):
init, end = i * nb_subclasses, (i+1) * nb_subclasses
P[init:end, init:end] = build_for_cifar100(nb_subclasses, n)
y_train_noisy = multiclass_noisify(np.array(self.targets), P=P, random_state=seed)
actual_noise = (y_train_noisy != np.array(self.targets)).mean()
assert actual_noise > 0.0
print('Actual noise %.2f' % actual_noise)
self.targets = y_train_noisy.tolist()
return
elif nosiy_rate > 0:
n_samples = len(self.targets)
n_noisy = int(nosiy_rate * n_samples)
print("%d Noisy samples" % (n_noisy))
class_index = [np.where(np.array(self.targets) == i)[0] for i in range(100)]
class_noisy = int(n_noisy / 100)
noisy_idx = []
for d in range(100):
noisy_class_index = np.random.choice(class_index[d], class_noisy, replace=False)
noisy_idx.extend(noisy_class_index)
print("Class %d, number of noisy % d" % (d, len(noisy_class_index)))
for i in noisy_idx:
self.targets[i] = other_class(n_classes=100, current_class=self.targets[i])
print(len(noisy_idx))
print("Print noisy label generation statistics:")
for i in range(100):
n_noisy = np.sum(np.array(self.targets) == i)
print("Noisy class %s, has %s samples." % (i, n_noisy))
return
class DatasetGenerator():
def __init__(self,
train_batch_size=128,
eval_batch_size=256,
data_path='data/',
seed=123,
num_of_workers=4,
asym=False,
dataset_type='CIFAR10',
is_cifar100=False,
cutout_length=16,
noise_rate=0.4):
self.seed = seed
np.random.seed(seed)
self.train_batch_size = train_batch_size
self.eval_batch_size = eval_batch_size
self.data_path = data_path
self.num_of_workers = num_of_workers
self.cutout_length = cutout_length
self.noise_rate = noise_rate
self.dataset_type = dataset_type
self.asym = asym
if self.asym:
self.noise_type = 'asymmetric'
else:
self.noise_type = 'symmetric'
if self.noise_rate == 0:
self.noise_type = 'clean'
self.data_loaders = self.loadData()
return
def getDataLoader(self):
return self.data_loaders
def loadData(self):
if self.dataset_type == 'MNIST':
MEAN = [0.1307]
STD = [0.3081]
train_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(MEAN, STD)])
test_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(MEAN, STD)])
train_dataset = MNIST(root=self.data_path+'/MNIST',
train=True,
transform=train_transform,
download=False,
noise_type=self.noise_type,
noise_rate=self.noise_rate,
)
test_dataset = MNIST(
root=self.data_path+'/MNIST',
train=False,
transform=test_transform,
download=False,
noise_type=self.noise_type,
noise_rate=self.noise_rate,
)
# train_dataset = MNISTNoisy(root=self.data_path,
# train=True,
# transform=train_transform,
# download=False,
# asym=self.asym,
# seed=self.seed,
# nosiy_rate=self.noise_rate)
#
# test_dataset = datasets.MNIST(root=self.data_path,
# train=False,
# transform=test_transform,
# download=False)
elif self.dataset_type == 'CIFAR100':
CIFAR_MEAN = [0.5071, 0.4865, 0.4409]
CIFAR_STD = [0.2673, 0.2564, 0.2762]
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(20),
transforms.ToTensor(),
transforms.Normalize(CIFAR_MEAN, CIFAR_STD)])
test_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(CIFAR_MEAN, CIFAR_STD)])
train_dataset = cifar100Nosiy(root=self.data_path,
train=True,
transform=train_transform,
download=True,
asym=self.asym,
seed=self.seed,
nosiy_rate=self.noise_rate)
test_dataset = datasets.CIFAR100(root=self.data_path,
train=False,
transform=test_transform,
download=True)
elif self.dataset_type == 'CIFAR10':
CIFAR_MEAN = [0.49139968, 0.48215827, 0.44653124]
CIFAR_STD = [0.24703233, 0.24348505, 0.26158768]
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(CIFAR_MEAN, CIFAR_STD)])
test_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(CIFAR_MEAN, CIFAR_STD)])
train_dataset = cifar10Nosiy(root=self.data_path,
train=True,
transform=train_transform,
download=True,
asym=self.asym,
nosiy_rate=self.noise_rate)
test_dataset = datasets.CIFAR10(root=self.data_path,
train=False,
transform=test_transform,
download=True)
else:
raise("Unknown Dataset")
data_loaders = {}
data_loaders['train_dataset'] = DataLoader(dataset=train_dataset,
batch_size=self.train_batch_size,
shuffle=True,
pin_memory=True,
num_workers=self.num_of_workers)
data_loaders['test_dataset'] = DataLoader(dataset=test_dataset,
batch_size=self.eval_batch_size,
shuffle=False,
pin_memory=True,
num_workers=self.num_of_workers)
print("Num of train %d" % (len(train_dataset)))
print("Num of test %d" % (len(test_dataset)))
return data_loaders
class WebVisionDataset:
def __init__(self, path, file_name='webvision_mini_train', transform=None, target_transform=None):
self.target_list = []
self.path = path
self.load_file(os.path.join(path, file_name))
self.transform = transform
self.target_transform = target_transform
return
def load_file(self, filename):
f = open(filename, "r")
for line in f:
train_file, label = line.split()
self.target_list.append((train_file, int(label)))
f.close()
return
def __len__(self):
return len(self.target_list)
def __getitem__(self, index):
impath, target = self.target_list[index]
img = Image.open(os.path.join(self.path, impath)).convert("RGB")
if self.transform is not None:
img = self.transform(img)
return img, target
class ImageNetMiniVal:
def __init__(self, path, file_name='ILSVRC2012_mini_val.txt', transform=None, target_transform=None):
self.target_list = []
self.path = path
self.load_file(os.path.join(path, file_name))
self.transform= transform
self.target_transform = target_transform
return
def load_file(self, filename):
f = open(filename, "r")
for line in f:
train_file, label = line.split()
self.target_list.append((train_file, int(label)))
f.close()
return
def __len__(self):
return len(self.target_list)
def __getitem__(self, index):
impath, target = self.target_list[index]
img = Image.open(os.path.join(self.path, impath)).convert("RGB")
if self.transform is not None:
img = self.transform(img)
return img, target
class WebVisionDatasetLoader:
def __init__(self, setting='mini', train_batch_size=128, eval_batch_size=256, train_data_path='data/', valid_data_path='data/', num_of_workers=4):
self.train_batch_size = train_batch_size
self.eval_batch_size = eval_batch_size
self.train_data_path = train_data_path
self.valid_data_path = valid_data_path
self.num_of_workers = num_of_workers
self.setting = setting
self.data_loaders = self.loadData()
def getDataLoader(self):
return self.data_loaders
def loadData(self):
IMAGENET_MEAN = [0.485, 0.456, 0.406]
IMAGENET_STD = [0.229, 0.224, 0.225]
train_transform = transforms.Compose([transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(brightness=0.4,
contrast=0.4,
saturation=0.4,
hue=0.2),
transforms.ToTensor(),
transforms.Normalize(IMAGENET_MEAN, IMAGENET_STD)])
test_transform = transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(IMAGENET_MEAN, IMAGENET_STD)])
if self.setting == 'mini':
train_dataset = WebVisionDataset(path=self.train_data_path,
file_name='webvision_mini_train.txt',
transform=train_transform)
test_dataset = WebVisionDataset(path=self.valid_data_path,
file_name='webvision_mini_val.txt',
transform=test_transform)
elif self.setting == 'full':
train_dataset = WebVisionDataset(path=self.train_data_path,
file_name='train_filelist_google.txt',
transform=train_transform)
test_dataset = WebVisionDataset(path=self.valid_data_path,
file_name='val_filelist.txt',
transform=test_transform)
elif self.setting == 'full_imagenet':
train_dataset = WebVisionDataset(path=self.train_data_path,
file_name='train_filelist_google',
transform=train_transform)
test_dataset = datasets.ImageNet(root=self.valid_data_path,
split='val',
transform=test_transform)
else:
raise(NotImplementedError)
data_loaders = {}
print('Training Set Size %d' % (len(train_dataset)))
print('Test Set Size %d' % (len(test_dataset)))
data_loaders['train_dataset'] = DataLoader(dataset=train_dataset,
batch_size=self.train_batch_size,
shuffle=True,
pin_memory=True,
num_workers=self.num_of_workers)
data_loaders['test_dataset'] = DataLoader(dataset=test_dataset,
batch_size=self.eval_batch_size,
shuffle=False,
pin_memory=True,
num_workers=self.num_of_workers)
return data_loaders
def online_mean_and_sd(loader):
"""Compute the mean and sd in an online fashion
Var[x] = E[X^2] - E^2[X]
"""
cnt = 0
fst_moment = torch.empty(3)
snd_moment = torch.empty(3)
for data, _ in tqdm(loader):
b, c, h, w = data.shape
nb_pixels = b * h * w
sum_ = torch.sum(data, dim=[0, 2, 3])
sum_of_square = torch.sum(data ** 2, dim=[0, 2, 3])
fst_moment = (cnt * fst_moment + sum_) / (cnt + nb_pixels)
snd_moment = (cnt * snd_moment + sum_of_square) / (cnt + nb_pixels)
cnt += nb_pixels
return fst_moment, torch.sqrt(snd_moment - fst_moment ** 2)
class Cutout(object):
def __init__(self, length):
self.length = length
def __call__(self, img):
h, w = img.size(1), img.size(2)
mask = np.ones((h, w), np.float32)
y = np.random.randint(h)
x = np.random.randint(w)
y1 = np.clip(y - self.length // 2, 0, h)
y2 = np.clip(y + self.length // 2, 0, h)
x1 = np.clip(x - self.length // 2, 0, w)
x2 = np.clip(x + self.length // 2, 0, w)
mask[y1: y2, x1: x2] = 0.
mask = torch.from_numpy(mask)
mask = mask.expand_as(img)
img *= mask
return img
if __name__ == '__main__':
IMAGENET_MEAN = [0.485, 0.456, 0.406]
IMAGENET_STD = [0.229, 0.224, 0.225]
train_transform = transforms.Compose([transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(brightness=0.4,
contrast=0.4,
saturation=0.4,
hue=0.2),
transforms.ToTensor(),
transforms.Normalize(IMAGENET_MEAN, IMAGENET_STD)])
test_transform = transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(IMAGENET_MEAN, IMAGENET_STD)])
a = WebVisionDataset(path='../database/WebVision', file_name='webvision_mini_val.txt',transform=test_transform)