-
Notifications
You must be signed in to change notification settings - Fork 26
/
datasets.py
350 lines (293 loc) · 13.1 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
# Copyright 2017-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import collections
import torchvision.transforms as transforms
import os
import json
try:
from IPython import embed
except:
pass
_DATASETS = {}
Dataset = collections.namedtuple(
'Dataset', ['trainset', 'testset'])
def _add_dataset(dataset_fn):
_DATASETS[dataset_fn.__name__] = dataset_fn
return dataset_fn
def _get_transforms(augment=True, normalize=None):
if normalize is None:
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
basic_transform = [transforms.ToTensor(), normalize]
transform_train = []
if augment:
transform_train += [
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
]
else:
transform_train += [
transforms.Resize(256),
transforms.CenterCrop(224),
]
transform_train += basic_transform
transform_train = transforms.Compose(transform_train)
transform_test = [
transforms.Resize(256),
transforms.CenterCrop(224),
]
transform_test += basic_transform
transform_test = transforms.Compose(transform_test)
return transform_train, transform_test
def _get_mnist_transforms(augment=True, invert=False, transpose=False):
transform = [
transforms.ToTensor(),
]
if invert:
transform += [transforms.Lambda(lambda x: 1. - x)]
if transpose:
transform += [transforms.Lambda(lambda x: x.transpose(2, 1))]
transform += [
transforms.Normalize((.5,), (.5,)),
transforms.Lambda(lambda x: x.expand(3, 32, 32))
]
transform_train = []
transform_train += [transforms.Pad(padding=2)]
if augment:
transform_train += [transforms.RandomCrop(32, padding=4)]
transform_train += transform
transform_train = transforms.Compose(transform_train)
transform_test = []
transform_test += [transforms.Pad(padding=2)]
transform_test += transform
transform_test = transforms.Compose(transform_test)
return transform_train, transform_test
def _get_cifar_transforms(augment=True):
transform = [
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
]
transform_train = []
if augment:
transform_train += [
transforms.Pad(padding=4, fill=(125, 123, 113)),
transforms.RandomCrop(32, padding=0),
transforms.RandomHorizontalFlip()]
transform_train += transform
transform_train = transforms.Compose(transform_train)
transform_test = []
transform_test += transform
transform_test = transforms.Compose(transform_test)
return transform_train, transform_test
def set_metadata(trainset, testset, config, dataset_name):
trainset.metadata = {
'dataset': dataset_name,
'task_id': config.task_id,
'task_name': trainset.task_name,
}
testset.metadata = {
'dataset': dataset_name,
'task_id': config.task_id,
'task_name': testset.task_name,
}
return trainset, testset
@_add_dataset
def inat2018(root, config):
from dataset.inat import iNat2018Dataset
transform_train, transform_test = _get_transforms()
trainset = iNat2018Dataset(root, split='train', transform=transform_train, task_id=config.task_id)
testset = iNat2018Dataset(root, split='val', transform=transform_test, task_id=config.task_id)
trainset, testset = set_metadata(trainset, testset, config, 'inat2018')
return trainset, testset
def load_tasks_map(tasks_map_file):
assert os.path.exists(tasks_map_file), tasks_map_file
with open(tasks_map_file, 'r') as f:
tasks_map = json.load(f)
tasks_map = {int(k): int(v) for k, v in tasks_map.items()}
return tasks_map
@_add_dataset
def cub_inat2018(root, config):
"""This meta-task is the concatenation of CUB-200 (first 25 tasks) and iNat (last 207 tasks).
- The first 10 tasks are classification of the animal species inside one of 10 orders of birds in CUB-200
(considering all orders except passeriformes).
- The next 15 tasks are classification of species inside the 15 families of the order of passerifomes
- The remaining 207 tasks are classification of the species inside each of 207 families in iNat
As noted above, for CUB-200 10 taks are classification of species inside an order, rather than inside of a family
as done in the iNat (recall order > family > species). This is done because CUB-200 has very few images
in each family of bird (expect for the families of passeriformes). Hence, we go up step in the taxonomy and
consider classification inside a orders and not families.
"""
NUM_CUB = 25
NUM_CUB_ORDERS = 10
NUM_INAT = 207
assert 0 <= config.task_id < NUM_CUB + NUM_INAT
transform_train, transform_test = _get_transforms()
if 0 <= config.task_id < NUM_CUB:
# CUB
from dataset.cub import CUBTasks, CUBDataset
tasks_map_file = os.path.join(root, 'cub/CUB_200_2011', 'final_tasks_map.json')
tasks_map = load_tasks_map(tasks_map_file)
task_id = tasks_map[config.task_id]
if config.task_id < NUM_CUB_ORDERS:
# CUB orders
train_tasks = CUBTasks(CUBDataset(root, split='train'))
trainset = train_tasks.generate(task_id=task_id,
use_species_names=True,
transform=transform_train)
test_tasks = CUBTasks(CUBDataset(root, split='test'))
testset = test_tasks.generate(task_id=task_id,
use_species_names=True,
transform=transform_test)
else:
# CUB passeriformes families
train_tasks = CUBTasks(CUBDataset(root, split='train'))
trainset = train_tasks.generate(task_id=task_id,
task='family',
taxonomy_file='passeriformes.txt',
use_species_names=True,
transform=transform_train)
test_tasks = CUBTasks(CUBDataset(root, split='test'))
testset = test_tasks.generate(task_id=task_id,
task='family',
taxonomy_file='passeriformes.txt',
use_species_names=True,
transform=transform_test)
else:
# iNat2018
from dataset.inat import iNat2018Dataset
tasks_map_file = os.path.join(root, 'inat2018', 'final_tasks_map.json')
tasks_map = load_tasks_map(tasks_map_file)
task_id = tasks_map[config.task_id - NUM_CUB]
trainset = iNat2018Dataset(root, split='train', transform=transform_train, task_id=task_id)
testset = iNat2018Dataset(root, split='val', transform=transform_test, task_id=task_id)
trainset, testset = set_metadata(trainset, testset, config, 'cub_inat2018')
return trainset, testset
@_add_dataset
def imat2018fashion(root, config):
NUM_IMAT = 228
assert 0 <= config.task_id < NUM_IMAT
from dataset.imat import iMat2018FashionDataset, iMat2018FashionTasks
transform_train, transform_test = _get_transforms()
train_tasks = iMat2018FashionTasks(iMat2018FashionDataset(root, split='train'))
trainset = train_tasks.generate(task_id=config.task_id,
transform=transform_train)
test_tasks = iMat2018FashionTasks(iMat2018FashionDataset(root, split='validation'))
testset = test_tasks.generate(task_id=config.task_id,
transform=transform_test)
trainset, testset = set_metadata(trainset, testset, config, 'imat2018fashion')
return trainset, testset
@_add_dataset
def split_mnist(root, config):
assert isinstance(config.task_id, tuple)
from dataset.mnist import MNISTDataset, SplitMNISTTask
transform_train, transform_test = _get_mnist_transforms()
train_tasks = SplitMNISTTask(MNISTDataset(root, train=True))
trainset = train_tasks.generate(classes=config.task_id, transform=transform_train)
test_tasks = SplitMNISTTask(MNISTDataset(root, train=False))
testset = test_tasks.generate(classes=config.task_id, transform=transform_test)
trainset, testset = set_metadata(trainset, testset, config, 'split_mnist')
return trainset, testset
@_add_dataset
def split_cifar(root, config):
assert 0 <= config.task_id < 11
from dataset.cifar import CIFAR10Dataset, CIFAR100Dataset, SplitCIFARTask
transform_train, transform_test = _get_cifar_transforms()
train_tasks = SplitCIFARTask(CIFAR10Dataset(root, train=True), CIFAR100Dataset(root, train=True))
trainset = train_tasks.generate(task_id=config.task_id, transform=transform_train)
test_tasks = SplitCIFARTask(CIFAR10Dataset(root, train=False), CIFAR100Dataset(root, train=False))
testset = test_tasks.generate(task_id=config.task_id, transform=transform_test)
trainset, testset = set_metadata(trainset, testset, config, 'split_cifar')
return trainset, testset
@_add_dataset
def cifar10_mnist(root, config):
from dataset.cifar import CIFAR10Dataset
from dataset.mnist import MNISTDataset
from dataset.expansion import UnionClassificationTaskExpander
transform_train, transform_test = _get_cifar_transforms()
trainset = UnionClassificationTaskExpander(merge_duplicate_images=False)(
[CIFAR10Dataset(root, train=True), MNISTDataset(root, train=True, expand=True)], transform=transform_train)
testset = UnionClassificationTaskExpander(merge_duplicate_images=False)(
[CIFAR10Dataset(root, train=False), MNISTDataset(root, train=False, expand=True)], transform=transform_test)
return trainset, testset
@_add_dataset
def cifar10(root):
from torchvision.datasets import CIFAR10
transform = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
])
trainset = CIFAR10(root, train=True, transform=transform, download=True)
testset = CIFAR10(root, train=False, transform=transform)
return trainset, testset
@_add_dataset
def cifar100(root):
from torchvision.datasets import CIFAR100
transform = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
])
trainset = CIFAR100(root, train=True, transform=transform, download=True)
testset = CIFAR100(root, train=False, transform=transform)
return trainset, testset
@_add_dataset
def mnist(root):
from torchvision.datasets import MNIST
transform = transforms.Compose([
lambda x: x.convert("RGB"),
transforms.Resize(224),
transforms.ToTensor(),
# transforms.Normalize((0.5, 0.5, 0.5), (1., 1., 1.)),
])
trainset = MNIST(root, train=True, transform=transform, download=True)
testset = MNIST(root, train=False, transform=transform)
return trainset, testset
@_add_dataset
def letters(root):
from torchvision.datasets import EMNIST
transform = transforms.Compose([
lambda x: x.convert("RGB"),
transforms.Resize(224),
transforms.ToTensor(),
# transforms.Normalize((0.5, 0.5, 0.5), (1., 1., 1.)),
])
trainset = EMNIST(root, train=True, split='letters', transform=transform, download=True)
testset = EMNIST(root, train=False, split='letters', transform=transform)
return trainset, testset
@_add_dataset
def kmnist(root):
from torchvision.datasets import KMNIST
transform = transforms.Compose([
lambda x: x.convert("RGB"),
transforms.Resize(224),
transforms.ToTensor(),
])
trainset = KMNIST(root, train=True, transform=transform, download=True)
testset = KMNIST(root, train=False, transform=transform)
return trainset, testset
@_add_dataset
def stl10(root):
from torchvision.datasets import STL10
transform = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
])
trainset = STL10(root, split='train', transform=transform, download=True)
testset = STL10(root, split='test', transform=transform)
trainset.targets = trainset.labels
testset.targets = testset.labels
return trainset, testset
def get_dataset(root, config=None):
return _DATASETS[config.name](os.path.expanduser(root), config)