-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathdatasets.py
424 lines (338 loc) · 15.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
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
import subprocess
import os
import abc
import hashlib
import zipfile
import glob
import logging
import tarfile
from skimage.io import imread
from PIL import Image
from tqdm import tqdm
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, datasets
DIR = os.path.abspath(os.path.dirname(__file__))
COLOUR_BLACK = 0
COLOUR_WHITE = 1
DATASETS_DICT = {"mnist": "MNIST",
"fashion": "FashionMNIST",
"dsprites": "DSprites",
"celeba": "CelebA",
"chairs": "Chairs"}
DATASETS = list(DATASETS_DICT.keys())
def get_dataset(dataset):
"""Return the correct dataset."""
dataset = dataset.lower()
try:
# eval because stores name as string in order to put it at top of file
return eval(DATASETS_DICT[dataset])
except KeyError:
raise ValueError("Unkown dataset: {}".format(dataset))
def get_img_size(dataset):
"""Return the correct image size."""
return get_dataset(dataset).img_size
def get_background(dataset):
"""Return the image background color."""
return get_dataset(dataset).background_color
def get_dataloaders(dataset, root=None, shuffle=True, pin_memory=True,
batch_size=128, logger=logging.getLogger(__name__), **kwargs):
"""A generic data loader
Parameters
----------
dataset : {"mnist", "fashion", "dsprites", "celeba", "chairs"}
Name of the dataset to load
root : str
Path to the dataset root. If `None` uses the default one.
kwargs :
Additional arguments to `DataLoader`. Default values are modified.
"""
pin_memory = pin_memory and torch.cuda.is_available # only pin if GPU available
Dataset = get_dataset(dataset)
dataset = Dataset(logger=logger) if root is None else Dataset(root=root, logger=logger)
return DataLoader(dataset,
batch_size=batch_size,
shuffle=shuffle,
pin_memory=pin_memory,
**kwargs)
class DisentangledDataset(Dataset, abc.ABC):
"""Base Class for disentangled VAE datasets.
Parameters
----------
root : string
Root directory of dataset.
transforms_list : list
List of `torch.vision.transforms` to apply to the data when loading it.
"""
def __init__(self, root, transforms_list=[], logger=logging.getLogger(__name__)):
self.root = root
self.train_data = os.path.join(root, type(self).files["train"])
self.transforms = transforms.Compose(transforms_list)
self.logger = logger
if not os.path.isdir(root):
self.logger.info("Downloading {} ...".format(str(type(self))))
self.download()
self.logger.info("Finished Downloading.")
def __len__(self):
return len(self.imgs)
@abc.abstractmethod
def __getitem__(self, idx):
"""Get the image of `idx`.
Return
------
sample : torch.Tensor
Tensor in [0.,1.] of shape `img_size`.
"""
pass
@abc.abstractmethod
def download(self):
"""Download the dataset. """
pass
class DSprites(DisentangledDataset):
"""DSprites Dataset from [1].
Disentanglement test Sprites dataset.Procedurally generated 2D shapes, from 6
disentangled latent factors. This dataset uses 6 latents, controlling the color,
shape, scale, rotation and position of a sprite. All possible variations of
the latents are present. Ordering along dimension 1 is fixed and can be mapped
back to the exact latent values that generated that image. Pixel outputs are
different. No noise added.
Notes
-----
- Link : https://github.com/deepmind/dsprites-dataset/
- hard coded metadata because issue with python 3 loading of python 2
Parameters
----------
root : string
Root directory of dataset.
References
----------
[1] Higgins, I., Matthey, L., Pal, A., Burgess, C., Glorot, X., Botvinick,
M., ... & Lerchner, A. (2017). beta-vae: Learning basic visual concepts
with a constrained variational framework. In International Conference
on Learning Representations.
"""
urls = {"train": "https://github.com/deepmind/dsprites-dataset/blob/master/dsprites_ndarray_co1sh3sc6or40x32y32_64x64.npz?raw=true"}
files = {"train": "dsprite_train.npz"}
lat_names = ('shape', 'scale', 'orientation', 'posX', 'posY')
lat_sizes = np.array([3, 6, 40, 32, 32])
img_size = (1, 64, 64)
background_color = COLOUR_BLACK
lat_values = {'posX': np.array([0., 0.03225806, 0.06451613, 0.09677419, 0.12903226,
0.16129032, 0.19354839, 0.22580645, 0.25806452,
0.29032258, 0.32258065, 0.35483871, 0.38709677,
0.41935484, 0.4516129, 0.48387097, 0.51612903,
0.5483871, 0.58064516, 0.61290323, 0.64516129,
0.67741935, 0.70967742, 0.74193548, 0.77419355,
0.80645161, 0.83870968, 0.87096774, 0.90322581,
0.93548387, 0.96774194, 1.]),
'posY': np.array([0., 0.03225806, 0.06451613, 0.09677419, 0.12903226,
0.16129032, 0.19354839, 0.22580645, 0.25806452,
0.29032258, 0.32258065, 0.35483871, 0.38709677,
0.41935484, 0.4516129, 0.48387097, 0.51612903,
0.5483871, 0.58064516, 0.61290323, 0.64516129,
0.67741935, 0.70967742, 0.74193548, 0.77419355,
0.80645161, 0.83870968, 0.87096774, 0.90322581,
0.93548387, 0.96774194, 1.]),
'scale': np.array([0.5, 0.6, 0.7, 0.8, 0.9, 1.]),
'orientation': np.array([0., 0.16110732, 0.32221463, 0.48332195,
0.64442926, 0.80553658, 0.96664389, 1.12775121,
1.28885852, 1.44996584, 1.61107316, 1.77218047,
1.93328779, 2.0943951, 2.25550242, 2.41660973,
2.57771705, 2.73882436, 2.89993168, 3.061039,
3.22214631, 3.38325363, 3.54436094, 3.70546826,
3.86657557, 4.02768289, 4.1887902, 4.34989752,
4.51100484, 4.67211215, 4.83321947, 4.99432678,
5.1554341, 5.31654141, 5.47764873, 5.63875604,
5.79986336, 5.96097068, 6.12207799, 6.28318531]),
'shape': np.array([1., 2., 3.]),
'color': np.array([1.])}
def __init__(self, root=os.path.join(DIR, '../data/dsprites/'), **kwargs):
super().__init__(root, [transforms.ToTensor()], **kwargs)
dataset_zip = np.load(self.train_data)
self.imgs = dataset_zip['imgs']
self.lat_values = dataset_zip['latents_values']
def download(self):
"""Download the dataset."""
os.makedirs(self.root)
subprocess.check_call(["curl", "-L", type(self).urls["train"],
"--output", self.train_data])
def __getitem__(self, idx):
"""Get the image of `idx`
Return
------
sample : torch.Tensor
Tensor in [0.,1.] of shape `img_size`.
lat_value : np.array
Array of length 6, that gives the value of each factor of variation.
"""
# stored image have binary and shape (H x W) so multiply by 255 to get pixel
# values + add dimension
sample = np.expand_dims(self.imgs[idx] * 255, axis=-1)
# ToTensor transforms numpy.ndarray (H x W x C) in the range
# [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0]
sample = self.transforms(sample)
lat_value = self.lat_values[idx]
return sample, lat_value
class CelebA(DisentangledDataset):
"""CelebA Dataset from [1].
CelebFaces Attributes Dataset (CelebA) is a large-scale face attributes dataset
with more than 200K celebrity images, each with 40 attribute annotations.
The images in this dataset cover large pose variations and background clutter.
CelebA has large diversities, large quantities, and rich annotations, including
10,177 number of identities, and 202,599 number of face images.
Notes
-----
- Link : http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html
Parameters
----------
root : string
Root directory of dataset.
References
----------
[1] Liu, Z., Luo, P., Wang, X., & Tang, X. (2015). Deep learning face
attributes in the wild. In Proceedings of the IEEE international conference
on computer vision (pp. 3730-3738).
"""
urls = {"train": "https://s3-us-west-1.amazonaws.com/udacity-dlnfd/datasets/celeba.zip"}
files = {"train": "img_align_celeba"}
img_size = (3, 64, 64)
background_color = COLOUR_WHITE
def __init__(self, root=os.path.join(DIR, '../data/celeba'), **kwargs):
super().__init__(root, [transforms.ToTensor()], **kwargs)
self.imgs = glob.glob(self.train_data + '/*')
def download(self):
"""Download the dataset."""
save_path = os.path.join(self.root, 'celeba.zip')
os.makedirs(self.root)
subprocess.check_call(["curl", "-L", type(self).urls["train"],
"--output", save_path])
hash_code = '00d2c5bc6d35e252742224ab0c1e8fcb'
assert hashlib.md5(open(save_path, 'rb').read()).hexdigest() == hash_code, \
'{} file is corrupted. Remove the file and try again.'.format(save_path)
with zipfile.ZipFile(save_path) as zf:
self.logger.info("Extracting CelebA ...")
zf.extractall(self.root)
os.remove(save_path)
self.logger.info("Resizing CelebA ...")
preprocess(self.train_data, size=type(self).img_size[1:])
def __getitem__(self, idx):
"""Get the image of `idx`
Return
------
sample : torch.Tensor
Tensor in [0.,1.] of shape `img_size`.
placeholder :
Placeholder value as their are no targets.
"""
img_path = self.imgs[idx]
# img values already between 0 and 255
img = imread(img_path)
# put each pixel in [0.,1.] and reshape to (C x H x W)
img = self.transforms(img)
# no label so return 0 (note that can't return None because)
# dataloaders requires so
return img, 0
class Chairs(datasets.ImageFolder):
"""Chairs Dataset from [1].
Notes
-----
- Link : https://www.di.ens.fr/willow/research/seeing3Dchairs
Parameters
----------
root : string
Root directory of dataset.
References
----------
[1] Aubry, M., Maturana, D., Efros, A. A., Russell, B. C., & Sivic, J. (2014).
Seeing 3d chairs: exemplar part-based 2d-3d alignment using a large dataset
of cad models. In Proceedings of the IEEE conference on computer vision
and pattern recognition (pp. 3762-3769).
"""
urls = {"train": "https://www.di.ens.fr/willow/research/seeing3Dchairs/data/rendered_chairs.tar"}
files = {"train": "chairs_64"}
img_size = (1, 64, 64)
background_color = COLOUR_WHITE
def __init__(self, root=os.path.join(DIR, '../data/chairs'),
logger=logging.getLogger(__name__)):
self.root = root
self.train_data = os.path.join(root, type(self).files["train"])
self.transforms = transforms.Compose([transforms.Grayscale(),
transforms.ToTensor()])
self.logger = logger
if not os.path.isdir(root):
self.logger.info("Downloading {} ...".format(str(type(self))))
self.download()
self.logger.info("Finished Downloading.")
super().__init__(self.train_data, transform=self.transforms)
def download(self):
"""Download the dataset."""
save_path = os.path.join(self.root, 'chairs.tar')
os.makedirs(self.root)
subprocess.check_call(["curl", type(self).urls["train"],
"--output", save_path])
self.logger.info("Extracting Chairs ...")
tar = tarfile.open(save_path)
tar.extractall(self.root)
tar.close()
os.rename(os.path.join(self.root, 'rendered_chairs'), self.train_data)
os.remove(save_path)
self.logger.info("Preprocessing Chairs ...")
preprocess(os.path.join(self.train_data, '*/*'), # root/*/*/*.png structure
size=type(self).img_size[1:],
center_crop=(400, 400))
class MNIST(datasets.MNIST):
"""Mnist wrapper. Docs: `datasets.MNIST.`"""
img_size = (1, 32, 32)
background_color = COLOUR_BLACK
def __init__(self, root=os.path.join(DIR, '../data/mnist'), **kwargs):
super().__init__(root,
train=True,
download=True,
transform=transforms.Compose([
transforms.Resize(32),
transforms.ToTensor()
]))
class FashionMNIST(datasets.FashionMNIST):
"""Fashion Mnist wrapper. Docs: `datasets.FashionMNIST.`"""
img_size = (1, 32, 32)
background_color = COLOUR_BLACK
def __init__(self, root=os.path.join(DIR, '../data/fashionMnist'), **kwargs):
super().__init__(root,
train=True,
download=True,
transform=transforms.Compose([
transforms.Resize(32),
transforms.ToTensor()
]))
# HELPERS
def preprocess(root, size=(64, 64), img_format='JPEG', center_crop=None):
"""Preprocess a folder of images.
Parameters
----------
root : string
Root directory of all images.
size : tuple of int
Size (width, height) to rescale the images. If `None` don't rescale.
img_format : string
Format to save the image in. Possible formats:
https://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html.
center_crop : tuple of int
Size (width, height) to center-crop the images. If `None` don't center-crop.
"""
imgs = []
for ext in [".png", ".jpg", ".jpeg"]:
imgs += glob.glob(os.path.join(root, '*' + ext))
for img_path in tqdm(imgs):
img = Image.open(img_path)
width, height = img.size
if size is not None and width != size[1] or height != size[0]:
img = img.resize(size, Image.ANTIALIAS)
if center_crop is not None:
new_width, new_height = center_crop
left = (width - new_width) // 2
top = (height - new_height) // 2
right = (width + new_width) // 2
bottom = (height + new_height) // 2
img.crop((left, top, right, bottom))
img.save(img_path, img_format)