-
Notifications
You must be signed in to change notification settings - Fork 15
/
datasets.py
542 lines (444 loc) · 20.9 KB
/
datasets.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
import os
import numpy as np
import pandas as pd
import torch
from PIL import Image, ImageFile
from torchvision import transforms
import torchvision.datasets.folder
from torch.utils.data import TensorDataset
from torchvision.datasets import MNIST, SVHN, VisionDataset
from torchvision.transforms.functional import rotate
from mdlt.utils.misc import save_image
ImageFile.LOAD_TRUNCATED_IMAGES = True
DATASETS = [
# Debug
"Debug28",
"Debug224",
# Small MDLT datasets
"ImbalancedColoredMNIST",
"ImbalancedRotatedMNIST",
"ImbalancedDigits",
# Big MDLT datasets
"VLCS",
"PACS",
"OfficeHome",
"TerraIncognita",
"DomainNet"
]
def get_dataset_class(dataset_name):
"""Return the dataset class with the given name."""
if dataset_name not in globals():
raise NotImplementedError(f"Dataset not found: {dataset_name}")
return globals()[dataset_name]
def num_environments(dataset_name):
return len(get_dataset_class(dataset_name).ENVIRONMENTS)
class MultipleDomainDataset:
N_STEPS = 5001 # Default, subclasses may override
CHECKPOINT_FREQ = 100 # Default, subclasses may override
N_WORKERS = 8 # Default, subclasses may override
MANY_SHOT_THRES = 100 # Default, subclasses may override
FEW_SHOT_THRES = 20 # Default, subclasses may override
ENVIRONMENTS = None # Subclasses should override
INPUT_SHAPE = None # Subclasses should override
def __getitem__(self, index):
return self.datasets[index]
def __len__(self):
return len(self.datasets)
class Debug(MultipleDomainDataset):
def __init__(self, root, hparams):
super().__init__()
self.input_shape = self.INPUT_SHAPE
self.num_classes = 2
self.datasets = []
for _ in [0, 1, 2]:
self.datasets.append(
TensorDataset(
torch.randn(16, *self.INPUT_SHAPE),
torch.randint(0, self.num_classes, (16,))
)
)
class Debug28(Debug):
INPUT_SHAPE = (3, 28, 28)
ENVIRONMENTS = ['0', '1', '2']
class Debug224(Debug):
INPUT_SHAPE = (3, 224, 224)
ENVIRONMENTS = ['0', '1', '2']
class MNISTM(VisionDataset):
resources = [
('https://github.com/liyxi/mnist-m/releases/download/data/mnist_m_train.pt.tar.gz',
'191ed53db9933bd85cc9700558847391'),
('https://github.com/liyxi/mnist-m/releases/download/data/mnist_m_test.pt.tar.gz',
'e11cb4d7fff76d7ec588b1134907db59')
]
training_file = "mnist_m_train.pt"
test_file = "mnist_m_test.pt"
def __init__(self, root, train=True, transform=None, target_transform=None, download=False):
super(MNISTM, self).__init__(root, transform=transform, target_transform=target_transform)
self.train = train
if not self._check_exists():
raise RuntimeError("Dataset not found.")
data_file = self.training_file if self.train else self.test_file
self.data, self.targets = torch.load(os.path.join(self.processed_folder, data_file))
def __getitem__(self, index):
img, target = self.data[index], int(self.targets[index])
img = Image.fromarray(img.squeeze().numpy(), mode="RGB")
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self):
return len(self.data)
@property
def raw_folder(self):
return os.path.join(self.root, self.__class__.__name__, 'raw')
@property
def processed_folder(self):
return os.path.join(self.root, self.__class__.__name__, 'processed')
def _check_exists(self):
return (os.path.exists(os.path.join(self.processed_folder, self.training_file)) and
os.path.exists(os.path.join(self.processed_folder, self.test_file)))
class ImbalancedMultipleEnvironmentMNIST(MultipleDomainDataset):
def __init__(self, root, split, environments, dataset_transform, input_shape, num_classes,
imb_type_per_env, imb_factor=0.1, rand_seed=0):
super().__init__()
np.random.seed(rand_seed)
torch.manual_seed(rand_seed)
if root is None:
raise ValueError('Data directory not specified!')
original_dataset_tr = MNIST(root, train=True, download=True)
original_dataset_te = MNIST(root, train=False, download=True)
original_images_tr = original_dataset_tr.data
original_images_te = original_dataset_te.data
original_labels_tr = original_dataset_tr.targets
original_labels_te = original_dataset_te.targets
shuffle_tr = torch.randperm(len(original_images_tr))
original_images_tr = original_images_tr[shuffle_tr]
original_labels_tr = original_labels_tr[shuffle_tr]
# split original train into train & val set
val_size = len(original_images_tr) // 3
original_images_va = original_images_tr[:val_size]
original_labels_va = original_labels_tr[:val_size]
original_images_tr = original_images_tr[val_size:]
original_labels_tr = original_labels_tr[val_size:]
shuffle_te = torch.randperm(len(original_images_te))
original_images_te = original_images_te[shuffle_te]
original_labels_te = original_labels_te[shuffle_te]
if split == 'train':
original_images = original_images_tr
original_labels = original_labels_tr
elif split == 'val':
original_images = original_images_va
original_labels = original_labels_va
else:
original_images = original_images_te
original_labels = original_labels_te
self.input_shape = input_shape
self.num_classes = num_classes
self.datasets = []
for i in range(len(environments)):
images = original_images[i::len(environments)]
labels = original_labels[i::len(environments)]
if split == 'train':
img_max = 1000
img_num_list = self.get_img_num_per_cls(num_classes, img_max, imb_type_per_env[i], imb_factor)
else:
img_max = 500
img_num_list = self.get_img_num_per_cls(num_classes, img_max, 'balanced', 1.)
images, labels = self.gen_imbalanced_data(images, labels, img_num_list)
self.datasets.append(dataset_transform(images, labels, environments[i]))
@staticmethod
def get_img_num_per_cls(cls_num, img_max, imb_type, imb_factor):
img_num_per_cls = []
if 'exp' in imb_type:
for cls_idx in range(cls_num):
num = img_max * (imb_factor**(cls_idx / (cls_num - 1.)))
img_num_per_cls.append(int(num))
elif 'step' in imb_type:
for cls_idx in range(cls_num // 2):
img_num_per_cls.append(int(img_max))
for cls_idx in range(cls_num // 2):
img_num_per_cls.append(int(img_max * imb_factor))
else:
img_num_per_cls.extend([int(img_max)] * cls_num)
if 'inv' in imb_type:
img_num_per_cls = img_num_per_cls[::-1]
return img_num_per_cls
def gen_imbalanced_data(self, images, labels, img_num_per_cls):
new_images, new_labels = [], []
labels_np = np.array(labels, dtype=np.int64)
classes = np.unique(labels_np)
self.num_per_cls_dict = dict()
for the_class, the_img_num in zip(classes, img_num_per_cls):
self.num_per_cls_dict[the_class] = the_img_num
idx = np.where(labels_np == the_class)[0]
np.random.shuffle(idx)
select_idx = idx[:the_img_num]
new_images.append(images[select_idx, ...])
new_labels.append(labels[select_idx])
new_images = torch.vstack(new_images)
new_labels = torch.hstack(new_labels)
return new_images, new_labels
def get_cls_num_list(self):
cls_num_list = []
for i in range(self.num_classes):
cls_num_list.append(self.num_per_cls_dict[i])
return cls_num_list
def collect_vis_data(self, images, labels, num_per_cls=2):
new_images = []
labels_np = np.array(labels, dtype=np.int64)
for class_idx in range(self.num_classes):
idx = np.where(labels_np == class_idx)[0]
new_images.append(images[idx[:num_per_cls], ...])
new_images = torch.vstack(new_images)
return new_images
class ImbalancedColoredMNIST(ImbalancedMultipleEnvironmentMNIST):
ENVIRONMENTS = ['blue', 'gray', 'green', 'pink']
COLORS = torch.tensor(
[[65., 105., 225.], [180., 180., 180.], [20., 180., 20.], [255., 20., 147.],
[255., 215., 0.], [255., 0., 0.], [0., 255., 0.], [0., 225., 225.], [0., 0., 255.], [188., 143., 143.]])
def __init__(self, root, split, hparams, vis=False):
self.vis = vis
super(ImbalancedColoredMNIST, self).__init__(
root, split, [0, 1, 2, 3], self.color_dataset, (3, 28, 28,), 10,
hparams['imb_type_per_env'], hparams['imb_factor'], hparams['rand_seed'])
def color_dataset(self, images, labels, environment):
images = torch.stack([images, images, images], dim=1)
images = (images > 0).float()
images *= self.COLORS[environment].view(1, -1, 1, 1)
x = images.float().div_(255.)
y = labels.view(-1).long()
if self.vis:
os.makedirs('vis_data', exist_ok=True)
save_image(self.collect_vis_data(images, labels),
f"vis_data/colormnist_env_{self.ENVIRONMENTS[environment]}.png", nrow=10)
return TensorDataset(x, y)
@staticmethod
def torch_bernoulli_(p, size):
return (torch.rand(size) < p).float()
@staticmethod
def torch_xor_(a, b):
return (a - b).abs()
class ImbalancedRotatedMNIST(ImbalancedMultipleEnvironmentMNIST):
# ENVIRONMENTS = ['0', '15', '30', '45', '60', '75']
ENVIRONMENTS = ['0', '30', '60']
def __init__(self, root, split, hparams, vis=False):
self.vis = vis
super(ImbalancedRotatedMNIST, self).__init__(
root, split, [0, 30, 60], self.rotate_dataset, (1, 28, 28,), 10,
hparams['imb_type_per_env'], hparams['imb_factor'], hparams['rand_seed'])
def rotate_dataset(self, images, labels, angle):
rotation = transforms.Compose([
transforms.ToPILImage(),
transforms.Lambda(lambda x: rotate(x, angle, fill=(0,),
interpolation=torchvision.transforms.InterpolationMode.BILINEAR)),
transforms.ToTensor()])
x = torch.zeros(len(images), 1, 28, 28)
for i in range(len(images)):
x[i] = rotation(images[i])
y = labels.view(-1).long()
if self.vis:
os.makedirs('vis_data', exist_ok=True)
save_image(self.collect_vis_data(x * 255., labels), f"vis_data/rotmnist_env_{angle}.png", nrow=10)
return TensorDataset(x, y)
class ImbalancedDigits(MultipleDomainDataset):
ENVIRONMENTS = ['MNIST', 'MNIST-M', 'SVHN']
DATASET_MAPPINGS = {'MNIST': MNIST, 'MNIST-M': MNISTM, 'SVHN': SVHN}
def __init__(self, root, split, hparams, vis=False, input_shape=(3, 28, 28,), num_classes=10):
super().__init__()
np.random.seed(hparams['rand_seed'])
torch.manual_seed(hparams['rand_seed'])
if root is None:
raise ValueError('Data directory not specified!')
self.input_shape = input_shape
self.num_classes = num_classes
self.vis = vis
self.datasets = []
for i, env in enumerate(self.ENVIRONMENTS):
if env != 'SVHN':
original_dataset_tr = self.DATASET_MAPPINGS[env](root, train=True, download=True)
original_dataset_te = self.DATASET_MAPPINGS[env](root, train=False, download=True)
images_tr, images_te = original_dataset_tr.data, original_dataset_te.data
labels_tr, labels_te = original_dataset_tr.targets, original_dataset_te.targets
else:
original_dataset_tr = self.DATASET_MAPPINGS[env](os.path.join(root, env), split='train', download=True)
original_dataset_te = self.DATASET_MAPPINGS[env](os.path.join(root, env), split='test', download=True)
images_tr, images_te = torch.from_numpy(original_dataset_tr.data), torch.from_numpy(original_dataset_te.data)
labels_tr, labels_te = torch.from_numpy(original_dataset_tr.labels), torch.from_numpy(original_dataset_te.labels)
shuffle = torch.randperm(len(images_tr))
images_tr, labels_tr = images_tr[shuffle], labels_tr[shuffle]
# split original train into train & val set
val_size = len(images_tr) // 3
images_va, labels_va = images_tr[:val_size], labels_tr[:val_size]
images_tr, labels_tr = images_tr[val_size:], labels_tr[val_size:]
if split == 'train':
images, labels = images_tr, labels_tr
img_max = 1000
img_num_list = self.get_img_num_per_cls(
num_classes, img_max, hparams['imb_type_per_env'][i], hparams['imb_factor'])
elif split == 'val':
images, labels = images_va, labels_va
img_max = 800
img_num_list = self.get_img_num_per_cls(num_classes, img_max, 'balanced', 1.)
else:
images, labels = images_te, labels_te
img_max = 800
img_num_list = self.get_img_num_per_cls(num_classes, img_max, 'balanced', 1.)
images, labels = self.gen_imbalanced_data(images, labels, img_num_list, env == 'SVHN')
self.datasets.append(self.digit_transform(images, labels, env))
def digit_transform(self, images, labels, dataset):
if dataset == 'MNIST':
images = torch.stack([images, images, images], dim=1)
elif dataset == 'MNIST-M':
images = images.permute(0, 3, 1, 2)
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((self.input_shape[1], self.input_shape[2])),
transforms.ToTensor()]) if dataset == 'SVHN' else \
transforms.Compose([transforms.ToPILImage(), transforms.ToTensor()])
x = torch.zeros(len(images), *self.input_shape)
for i in range(len(images)):
x[i] = transform(images[i])
y = labels.view(-1).long()
if self.vis:
os.makedirs('vis_data', exist_ok=True)
save_image(self.collect_vis_data(x, labels), f"vis_data/digit_{dataset}.png", nrow=8)
return TensorDataset(x, y)
@staticmethod
def get_img_num_per_cls(cls_num, img_max, imb_type, imb_factor):
img_num_per_cls = []
if 'exp' in imb_type:
for cls_idx in range(cls_num):
num = img_max * (imb_factor**(cls_idx / (cls_num - 1.)))
img_num_per_cls.append(int(num))
elif 'step' in imb_type:
for cls_idx in range(cls_num // 2):
img_num_per_cls.append(int(img_max))
for cls_idx in range(cls_num // 2):
img_num_per_cls.append(int(img_max * imb_factor))
else:
img_num_per_cls.extend([int(img_max)] * cls_num)
if 'inv' in imb_type:
img_num_per_cls = img_num_per_cls[::-1]
return img_num_per_cls
def gen_imbalanced_data(self, images, labels, img_num_per_cls, shift_class=False):
new_images, new_labels = [], []
labels_np = np.array(labels, dtype=np.int64)
classes = np.unique(labels_np)
# if shift_class:
# classes = np.concatenate([classes[1:], classes[:1]], axis=0)
# np.random.shuffle(classes)
self.num_per_cls_dict = dict()
for the_class, the_img_num in zip(classes, img_num_per_cls):
self.num_per_cls_dict[the_class] = the_img_num
idx = np.where(labels_np == the_class)[0]
np.random.shuffle(idx)
select_idx = idx[:the_img_num]
new_images.append(images[select_idx, ...])
new_labels.append(labels[select_idx])
new_images = torch.vstack(new_images)
new_labels = torch.hstack(new_labels)
return new_images, new_labels
def get_cls_num_list(self):
cls_num_list = []
for i in range(self.num_classes):
cls_num_list.append(self.num_per_cls_dict[i])
return cls_num_list
def collect_vis_data(self, images, labels, num_per_cls=4):
new_images = []
labels_np = np.array(labels, dtype=np.int64)
for class_idx in range(self.num_classes):
idx = np.where(labels_np == class_idx)[0]
new_images.append(images[idx[:num_per_cls], ...])
new_images = torch.vstack(new_images)
return new_images
class SplitImageFolder(torch.utils.data.Dataset):
def __init__(self, path, df, augment, split='train'):
self.df = df[df['split'] == split]
self.img_dir = path
self.split = split
self.augment = augment
self.transform = self.get_transform()
self.targets = self.df.label
self.classes = sorted(list(set([x.split('/')[1] for x in df.path])))
def __len__(self):
return len(self.df)
def __getitem__(self, index):
index = index % len(self.df)
row = self.df.iloc[index]
img = Image.open(os.path.join(self.img_dir, row['path'])).convert('RGB')
img = self.transform(img)
y = row['label'].astype('int')
return img, y
def get_transform(self):
if self.augment and self.split == 'train':
transform = transforms.Compose([
# transforms.Resize((224, 224)),
transforms.RandomResizedCrop(224, scale=(0.7, 1.0)),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(0.3, 0.3, 0.3, 0.3),
transforms.RandomGrayscale(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
else:
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
return transform
class MultipleEnvironmentImageFolder(MultipleDomainDataset):
def __init__(self, root, df, split, augment, hparams):
super().__init__()
environments = [f.name for f in os.scandir(root) if f.is_dir()]
environments = sorted(environments)
self.datasets = []
for i, environment in enumerate(environments):
df_env = df[df['env'] == environment]
env_dataset = SplitImageFolder(root, df_env, augment=augment, split=split)
self.datasets.append(env_dataset)
self.input_shape = (3, 224, 224,)
self.num_classes = len(self.datasets[-1].classes)
class VLCS(MultipleEnvironmentImageFolder):
ENVIRONMENTS = ["C", "L", "S", "V"]
MANY_SHOT_THRES = 100
FEW_SHOT_THRES = 20
def __init__(self, root, split, hparams):
self.dir = os.path.join(root, "VLCS")
self.df = pd.read_csv(os.path.join(self.dir, "VLCS.csv"))
super().__init__(self.dir, self.df, split, hparams['data_augmentation'], hparams)
class PACS(MultipleEnvironmentImageFolder):
ENVIRONMENTS = ["A", "C", "P", "S"]
MANY_SHOT_THRES = 100
FEW_SHOT_THRES = 20
def __init__(self, root, split, hparams):
self.dir = os.path.join(root, "PACS")
self.df = pd.read_csv(os.path.join(self.dir, "PACS.csv"))
super().__init__(self.dir, self.df, split, hparams['data_augmentation'], hparams)
class DomainNet(MultipleEnvironmentImageFolder):
N_STEPS = 15001
CHECKPOINT_FREQ = 300
ENVIRONMENTS = ["clip", "info", "paint", "quick", "real", "sketch"]
MANY_SHOT_THRES = 100
FEW_SHOT_THRES = 20
def __init__(self, root, split, hparams):
self.dir = os.path.join(root, "domain_net")
self.df = pd.read_csv(os.path.join(self.dir, "DomainNet.csv"))
super().__init__(self.dir, self.df, split, hparams['data_augmentation'], hparams)
class OfficeHome(MultipleEnvironmentImageFolder):
ENVIRONMENTS = ["A", "C", "P", "R"]
MANY_SHOT_THRES = 60
FEW_SHOT_THRES = 20
def __init__(self, root, split, hparams):
self.dir = os.path.join(root, "office_home")
self.df = pd.read_csv(os.path.join(self.dir, "OfficeHome.csv"))
super().__init__(self.dir, self.df, split, hparams['data_augmentation'], hparams)
class TerraIncognita(MultipleEnvironmentImageFolder):
ENVIRONMENTS = ["L100", "L38", "L43", "L46"]
MANY_SHOT_THRES = 100
FEW_SHOT_THRES = 25
def __init__(self, root, split, hparams):
self.dir = os.path.join(root, "terra_incognita")
self.df = pd.read_csv(os.path.join(self.dir, "TerraIncognita.csv"))
super().__init__(self.dir, self.df, split, hparams['data_augmentation'], hparams)