-
Notifications
You must be signed in to change notification settings - Fork 0
/
incremental_dataloader.py
545 lines (435 loc) · 19.7 KB
/
incremental_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
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
'''
TaICML incremental learning
Copyright (c) Jathushan Rajasegaran, 2019
'''
import random
import numpy as np
import torch
from PIL import Image
from torch.utils.data import DataLoader
from torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.data import Sampler
from torchvision import datasets, transforms
from idatasets.CUB200 import Cub2011
from idatasets.omniglot import Omniglot
from idatasets.celeb_1m import MS1M
import collections
from src.utils.augmentations import CIFAR10Policy
import torchvision
import pickle
from utils.download import download, generate_data
import shutil
def get_train_test_target_lists(train_dataset, test_dataset):
train_targets = []
test_targets = []
for idx in range(len(train_dataset)):
train_targets.append(train_dataset[idx][1])
for idx in range(len(test_dataset)):
test_targets.append(test_dataset[idx][1])
return train_targets, test_targets
class SubsetRandomSampler(Sampler):
r"""Samples elements randomly from a given list of indices, without replacement.
Arguments:
indices (sequence): a sequence of indices
"""
def __init__(self, indices, shuffle):
self.indices = indices
self.shuffle = shuffle
def __iter__(self):
if(self.shuffle):
return (self.indices[i] for i in torch.randperm(len(self.indices)))
else:
return (self.indices[i] for i in range(len(self.indices)))
def __len__(self):
return len(self.indices)
class IncrementalDataset:
def __init__(
self,
dataset_name,
args,
random_order=False,
shuffle=True,
workers=16,
batch_size=128,
seed=1,
initial_classes=40,
increment=10,
validation_split=0.
):
self.dataset_name = dataset_name.lower().strip()
datasets = _get_datasets(dataset_name)
self.train_transforms = datasets[0].train_transforms
self.common_transforms = datasets[0].common_transforms
try:
self.meta_transforms = datasets[0].meta_transforms
except:
self.meta_transforms = datasets[0].train_transforms
self.args = args
self._setup_data(
datasets,
args.data_path,
random_order=random_order,
seed=seed,
initial_classes=initial_classes,
increment=increment,
validation_split=validation_split
)
self._current_task = 0
self._batch_size = batch_size
self._workers = workers
self._shuffle = shuffle
self.sample_per_task_testing = {}
@property
def n_tasks(self):
return len(self.increments)
def get_same_index(self, target, label, mode="train", memory=None):
label_indices = []
label_targets = []
for i in range(len(target)):
if int(target[i]) in label:
label_indices.append(i)
label_targets.append(target[i])
for_memory = (label_indices.copy(),label_targets.copy())
if(self.args.overflow and not(mode=="test")):
memory_indices, memory_targets = memory
return memory_indices, memory
if memory is not None:
memory_indices, memory_targets = memory
memory_indices2 = np.tile(memory_indices, (self.args.mu,))
all_indices = np.concatenate([memory_indices2,label_indices])
else:
all_indices = label_indices
return all_indices, for_memory
def get_same_index_test_chunk(self, target, label, mode="test", memory=None):
label_indices = []
label_targets = []
np_target = np.array(target, dtype="uint32")
np_indices = np.array(list(range(len(target))), dtype="uint32")
for t in range(len(label)//self.args.class_per_task):
task_idx = []
for class_id in label[t*self.args.class_per_task: (t+1)*self.args.class_per_task]:
idx = np.where(np_target==class_id)[0]
task_idx.extend(list(idx.ravel()))
task_idx = np.array(task_idx, dtype="uint32")
task_idx.ravel()
random.shuffle(task_idx)
label_indices.extend(list(np_indices[task_idx]))
label_targets.extend(list(np_target[task_idx]))
if(t not in self.sample_per_task_testing.keys()):
self.sample_per_task_testing[t] = len(task_idx)
label_indices = np.array(label_indices, dtype="uint32")
label_indices.ravel()
return list(label_indices), label_targets
def new_task(self, memory=None):
print(self._current_task)
print(self.increments)
min_class = sum(self.increments[:self._current_task])
max_class = sum(self.increments[:self._current_task + 1])
if(self.args.overflow):
min_class = 0
max_class = sum(self.increments)
train_indices, for_memory = self.get_same_index(self.train_dataset.targets, list(range(min_class, max_class)), mode="train", memory=memory)
test_indices, _ = self.get_same_index(self.test_dataset.targets, list(range(min_class,max_class)), mode="test")
self.train_data_loader = torch.utils.data.DataLoader(self.train_dataset, batch_size=self._batch_size,shuffle=False,num_workers=self._workers, sampler=SubsetRandomSampler(train_indices, True))
self.test_data_loader = torch.utils.data.DataLoader(self.test_dataset, batch_size=self.args.test_batch,shuffle=False,num_workers=self._workers, sampler=SubsetRandomSampler(test_indices, False))
task_info = {
"min_class": min_class,
"max_class": max_class,
"task": self._current_task,
"max_task": len(self.increments),
"n_train_data": len(train_indices),
"n_test_data": len(test_indices)
}
self._current_task += 1
return task_info, self.train_data_loader, self.test_data_loader, self.test_data_loader
# for verification
def get_galary(self, task, batch_size=10):
indexes = []
dict_ind = {}
seen_classes = []
for i, t in enumerate(self.train_dataset.targets):
if not(t in seen_classes) and (t< (task+1)*self.args.class_per_task and (t>= (task)*self.args.class_per_task)):
seen_classes.append(t)
dict_ind[t] = i
od = collections.OrderedDict(sorted(dict_ind.items()))
for k, v in od.items():
indexes.append(v)
data_loader = torch.utils.data.DataLoader(self.train_dataset, batch_size=batch_size, shuffle=False, num_workers=self._workers, sampler=SubsetRandomSampler(indexes, False))
return data_loader
def get_custom_loader_idx(self, indexes, mode="train", batch_size=10, shuffle=True):
if(mode=="train"):
data_loader = torch.utils.data.DataLoader(self.train_dataset, batch_size=batch_size, shuffle=False, num_workers=self._workers, sampler=SubsetRandomSampler(indexes, True))
else:
data_loader = torch.utils.data.DataLoader(self.test_dataset, batch_size=batch_size, shuffle=False, num_workers=self._workers, sampler=SubsetRandomSampler(indexes, False))
return data_loader
def get_custom_loader_class(self, class_id, mode="train", batch_size=10, shuffle=False):
if(mode=="train"):
train_indices, for_memory = self.get_same_index(self.train_dataset.targets, class_id, mode="train", memory=None)
data_loader = torch.utils.data.DataLoader(self.train_dataset, batch_size=batch_size, shuffle=False, num_workers=self._workers, sampler=SubsetRandomSampler(train_indices, True))
else:
test_indices, _ = self.get_same_index(self.test_dataset.targets, class_id, mode="test")
data_loader = torch.utils.data.DataLoader(self.test_dataset, batch_size=batch_size, shuffle=False, num_workers=self._workers, sampler=SubsetRandomSampler(test_indices, False))
return data_loader
def _setup_data(self, datasets, path, random_order=False, seed=1, initial_classes=50, increment=10, validation_split=0.):
self.increments = []
self.class_order = []
trsf_train = transforms.Compose(self.train_transforms)
try:
trsf_mata = transforms.Compose(self.meta_transforms)
except:
trsf_mata = transforms.Compose(self.train_transforms)
trsf_test = transforms.Compose(self.common_transforms)
current_class_idx = 0 # When using multiple datasets
for dataset in datasets:
if(self.dataset_name=="tinyimagenet" or self.dataset_name=="tinyimagenet56"):
train_dataset = torchvision.datasets.ImageFolder(root=path + 'train',transform=trsf_train)
test_dataset = torchvision.datasets.ImageFolder(root=path + 'val', transform=trsf_test)
train_dataset.targets, test_dataset.targets = get_train_test_target_lists(train_dataset, test_dataset)
elif (self.dataset_name=='imagenet100'):
im100path = path
train_dataset = torchvision.datasets.ImageFolder(root=im100path+'/train', transform=trsf_train)
test_dataset = torchvision.datasets.ImageFolder(root=im100path+'/val/', transform=trsf_test)
elif (self.dataset_name=='imagenet'):
im100path = path
train_dataset = torchvision.datasets.ImageFolder(root=im100path+'/train', transform=trsf_train)
test_dataset = torchvision.datasets.ImageFolder(root=im100path+'/val', transform=trsf_test)
elif(self.dataset_name=="cub200" or self.dataset_name=="cifar100" or self.dataset_name=="mnist" or self.dataset_name=="caltech101" or self.dataset_name=="omniglot" or self.dataset_name=="celeb"):
train_dataset = dataset.base_dataset(root=path, train=True, download=True, transform=trsf_train)
test_dataset = dataset.base_dataset(root=path, train=False, download=True, transform=trsf_test)
elif(self.dataset_name=="svhn"):
train_dataset = dataset.base_dataset(root=path, split='train', download=True, transform=trsf_train)
test_dataset = dataset.base_dataset(root=path, split='test', download=True, transform=trsf_test)
train_dataset.targets = train_dataset.labels
test_dataset.targets = test_dataset.labels
order = [i for i in range(self.args.num_classes)]
if random_order:
random.seed(seed)
random.shuffle(order)
elif dataset.class_order is not None:
order = dataset.class_order
for i,t in enumerate(train_dataset.targets):
train_dataset.targets[i] = order[t]
for i,t in enumerate(test_dataset.targets):
test_dataset.targets[i] = order[t]
self.class_order.append(order)
self.increments = [increment for _ in range(len(order) // increment)]
self.train_dataset = train_dataset
self.test_dataset = test_dataset
@staticmethod
def _map_new_class_index(y, order):
"""Transforms targets for new class order."""
return np.array(list(map(lambda x: order.index(x), y)))
def get_memory(self, memory, for_memory, seed=1):
random.seed(seed)
memory_per_task = self.args.memory // ((self.args.sess+1)*self.args.class_per_task)
self._data_memory, self._targets_memory = np.array([]), np.array([])
mu = 1
#update old memory
if(memory is not None):
data_memory, targets_memory = memory
data_memory = np.array(data_memory, dtype="int32")
targets_memory = np.array(targets_memory, dtype="int32")
for class_idx in range(self.args.class_per_task*(self.args.sess)):
idx = np.where(targets_memory==class_idx)[0][:memory_per_task]
self._data_memory = np.concatenate([self._data_memory, np.tile(data_memory[idx], (mu,)) ])
self._targets_memory = np.concatenate([self._targets_memory, np.tile(targets_memory[idx], (mu,)) ])
#add new classes to the memory
new_indices, new_targets = for_memory
new_indices = np.array(new_indices, dtype="int32")
new_targets = np.array(new_targets, dtype="int32")
for class_idx in range(self.args.class_per_task*(self.args.sess),self.args.class_per_task*(1+self.args.sess)):
idx = np.where(new_targets==class_idx)[0][:memory_per_task]
self._data_memory = np.concatenate([self._data_memory, np.tile(new_indices[idx],(mu,)) ])
self._targets_memory = np.concatenate([self._targets_memory, np.tile(new_targets[idx],(mu,)) ])
print(len(self._data_memory))
return list(self._data_memory.astype("int32")), list(self._targets_memory.astype("int32"))
def _get_datasets(dataset_names):
return [_get_dataset(dataset_name) for dataset_name in dataset_names.split("-")]
def _get_dataset(dataset_name):
dataset_name = dataset_name.lower().strip()
if dataset_name == "cifar10":
return iCIFAR10
elif dataset_name == "cifar100":
return iCIFAR100
elif dataset_name == "imagenet":
return iIMAGENET
elif dataset_name == "imagenet100":
return iIMAGENET100
elif dataset_name == "tinyimagenet":
return iTINYIMAGENET
elif dataset_name == "tinyimagenet56":
return iTINYIMAGENET56
elif dataset_name == "cub200":
return iCUB200
elif dataset_name == "mnist":
return iMNIST
elif dataset_name == "caltech101":
return iCALTECH101
elif dataset_name == "celeb":
return iCELEB
elif dataset_name == "svhn":
return iSVHN
elif dataset_name == "omniglot":
return iOMNIGLOT
else:
raise NotImplementedError("Unknown dataset {}.".format(dataset_name))
class DataHandler:
base_dataset = None
train_transforms = []
mata_transforms = [transforms.ToTensor()]
common_transforms = [transforms.ToTensor()]
class_order = None
class iCIFAR10(DataHandler):
base_dataset = datasets.cifar.CIFAR10
train_transforms = [
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
transforms.ColorJitter(brightness=63 / 255),
transforms.ToTensor(),
]
common_transforms = [
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
]
class iCIFAR100(DataHandler):
base_dataset = datasets.cifar.CIFAR100
train_transforms = [
transforms.Resize((32,32)),
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
CIFAR10Policy(random=True),
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
]
common_transforms = [
transforms.ToTensor(),
]
class iCALTECH101(DataHandler):
base_dataset = datasets.Caltech101
train_transforms = [
transforms.Resize(136),
transforms.RandomCrop(128, padding=4),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
]
common_transforms = [
transforms.Resize(130),
transforms.CenterCrop(128),
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
]
class iCELEB(DataHandler):
base_dataset = MS1M
train_transforms = [
transforms.RandomCrop(112, padding=4),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
]
common_transforms = [
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
]
class iIMAGENET(DataHandler):
base_dataset = datasets.ImageNet
train_transforms = [
transforms.Resize(256),
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
]
common_transforms = [
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
# transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
]
class iIMAGENET100(DataHandler):
base_dataset = datasets.ImageNet
train_transforms = [
transforms.Resize(256),
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
CIFAR10Policy(random=True),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
]
common_transforms = [
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
]
class iTINYIMAGENET(DataHandler):
base_dataset = datasets.ImageNet
train_transforms = [
transforms.Resize(70),
transforms.RandomResizedCrop(64),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
CIFAR10Policy(random=True),
transforms.ToTensor(),
transforms.Normalize((0.480, 0.448, 0.397), (0.277, 0.270, 0.282)),
]
common_transforms = [
transforms.Resize(68),
transforms.CenterCrop(64),
transforms.ToTensor(),
]
class iTINYIMAGENET56(DataHandler):
train_transforms = [
transforms.Resize(60),
transforms.RandomResizedCrop(56),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
transforms.ToTensor(),
transforms.Normalize((0.480, 0.448, 0.397), (0.277, 0.270, 0.282)),
]
common_transforms = [
transforms.Resize(60),
transforms.CenterCrop(56),
transforms.ToTensor(),
]
class iCUB200(DataHandler):
base_dataset = Cub2011
train_transforms = [
transforms.Resize(230),
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
transforms.ColorJitter(brightness=63 / 255),
transforms.ToTensor(),
]
common_transforms = [
transforms.Resize(230),
transforms.CenterCrop(224),
transforms.ToTensor(),
]
class iMNIST(DataHandler):
base_dataset = datasets.MNIST
train_transforms = [ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) ]
common_transforms = [transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]
class iSVHN(DataHandler):
base_dataset = datasets.SVHN
train_transforms = [
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
]
common_transforms = [
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
]
class iOMNIGLOT(DataHandler):
base_dataset = datasets.Omniglot
train_transforms = [ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) ]
common_transforms = [transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]