-
Notifications
You must be signed in to change notification settings - Fork 21
/
data.py
283 lines (245 loc) · 13.6 KB
/
data.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
from time import time
import numpy
import numpy as np
import csv
import scipy.io
import torch
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import pickle
import os
import scipy.misc
from PIL import Image
def get_next_batch(X, Y, batch_size, shuffle=False):
n_batches = len(X) // batch_size
if shuffle:
x_idx = np.random.permutation(len(X))[:n_batches * batch_size]
else:
x_idx = np.arange(n_batches * batch_size)
for batch_idx in x_idx.reshape([n_batches, batch_size]):
batch_x, batch_y = X[batch_idx], Y[batch_idx]
yield batch_x, batch_y
def normalize(x_train, x_test):
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train_pixel_mean = x_train.mean(axis=0) # per-pixel mean
x_train_pixel_std = x_train.std(axis=0) # per-pixel std
x_train = (x_train - x_train_pixel_mean) / x_train_pixel_std
x_test = (x_test - x_train_pixel_mean) / x_train_pixel_std
return x_train, x_test
class Dataset:
def __init__(self, batch_size, augm_flag):
self.batch_size = batch_size
self.augm_flag = augm_flag
self.train_dataset = None
self.test_dataset = None
# Num workers is really important. For small datasets it should be 1 (0 slows down x2),
# for large datasets 4*n_gpus maybe
self.n_workers_train = 1
self.n_workers_test = 1
self.base_path = '../datasets/'
@staticmethod
def yield_data(iterator, n_batches):
for i, (x, y) in enumerate(iterator):
if type(x) != np.ndarray:
x, y = x.numpy(), y.numpy()
yield (x, y)
if i + 1 == n_batches:
break
def get_train_batches(self, n_batches, shuffle):
# Creation of a DataLoader object is instant, the queue starts to fill up on enumerate(train_loader)
train_loader = torch.utils.data.DataLoader(self.train_dataset, batch_size=self.batch_size, shuffle=shuffle,
num_workers=self.n_workers_train, drop_last=True)
return self.yield_data(train_loader, n_batches)
def get_test_batches(self, n_batches, shuffle):
test_loader = torch.utils.data.DataLoader(self.test_dataset, batch_size=self.batch_size, shuffle=shuffle,
num_workers=self.n_workers_test, drop_last=True)
return self.yield_data(test_loader, n_batches)
class GrayscaleDataset(Dataset):
@staticmethod
def yield_data(iterator, n_batches):
"""
We need to redefine yield_data() to fix the fact that mnist by default is bs x 28 x 28 and not bs x 28 x 28 x 1
"""
for i, (x, y) in enumerate(iterator):
x = x[:, :, :, np.newaxis] # bs x 28 x 28 -> bs x 28 x 28 x 1
x, y = x.numpy(), y.numpy()
yield (x, y)
if i + 1 == n_batches:
break
class MNIST(GrayscaleDataset):
def __init__(self, batch_size, augm_flag):
super().__init__(batch_size, augm_flag)
self.n_train, self.n_test = 60000, 10000
self.n_classes = 10
self.height, self.width, self.n_colors = 28, 28, 1
self.data_dir = self.base_path + 'mnist/'
transform_base = [transforms.Lambda(lambda x: np.array(x) / 255.0)]
transform_train = transforms.Compose([
transforms.RandomCrop(self.height, padding=4),
] + transform_base)
transform_test = transforms.Compose(transform_base)
transform_train = transform_train if self.augm_flag else transform_test
self.train_dataset = datasets.MNIST(self.data_dir, train=True, transform=transform_train, download=True)
self.test_dataset = datasets.MNIST(self.data_dir, train=False, transform=transform_test, download=True)
class FMNIST(GrayscaleDataset):
def __init__(self, batch_size, augm_flag):
super().__init__(batch_size, augm_flag)
self.n_train, self.n_test = 60000, 10000
self.n_classes = 10
self.height, self.width, self.n_colors = 28, 28, 1
self.data_dir = self.base_path + 'fmnist/'
transform_base = [transforms.Lambda(lambda x: np.array(x) / 255.0)]
transform_train = transforms.Compose([
transforms.RandomCrop(self.height, padding=4),
] + transform_base)
transform_test = transforms.Compose(transform_base)
transform_train = transform_train if self.augm_flag else transform_test
self.train_dataset = datasets.FashionMNIST(self.data_dir, train=True, transform=transform_train, download=True)
self.test_dataset = datasets.FashionMNIST(self.data_dir, train=False, transform=transform_test, download=True)
class EMNIST(GrayscaleDataset):
def __init__(self, batch_size, augm_flag):
super().__init__(batch_size, augm_flag)
self.n_train, self.n_test = 60000, 10000
# TODO: actually, these numbers are smaller than the real ones.
self.n_classes = 10
self.height, self.width, self.n_colors = 28, 28, 1
self.data_dir = self.base_path + 'emnist/'
transform_base = [transforms.Lambda(lambda x: np.array(x).T / 255.0)]
transform_train = transforms.Compose([
transforms.RandomCrop(self.height, padding=4),
] + transform_base)
transform_test = transforms.Compose(transform_base)
transform_train = transform_train if self.augm_flag else transform_test
self.train_dataset = datasets.EMNIST(self.data_dir, split='letters', train=True, transform=transform_train, download=True)
self.test_dataset = datasets.EMNIST(self.data_dir, split='letters', train=False, transform=transform_test, download=True)
class CIFAR10(Dataset):
def __init__(self, batch_size, augm_flag):
super().__init__(batch_size, augm_flag)
self.n_train, self.n_test = 50000, 10000
self.n_classes = 10
self.height, self.width, self.n_colors = 32, 32, 3
self.data_dir = self.base_path + 'cifar10/'
# normalize = transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2470, 0.2435, 0.2616])
# transformations = transforms.Compose([transforms.ToTensor(), normalize])
transform_base = [transforms.Lambda(lambda x: np.array(x) / 255.0)]
transform_train = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(self.height, padding=4),
] + transform_base)
transform_test = transforms.Compose(transform_base)
transform_train = transform_train if self.augm_flag else transform_test
self.train_dataset = datasets.CIFAR10(self.data_dir, train=True, transform=transform_train, download=True)
self.test_dataset = datasets.CIFAR10(self.data_dir, train=False, transform=transform_test, download=True)
class CIFAR10Grayscale(GrayscaleDataset):
def __init__(self, batch_size, augm_flag):
super().__init__(batch_size, augm_flag)
self.n_train, self.n_test = 50000, 10000
self.n_classes = 10
self.height, self.width, self.n_colors = 28, 28, 3
self.data_dir = self.base_path + 'cifar10/'
# normalize = transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2470, 0.2435, 0.2616])
# transformations = transforms.Compose([transforms.ToTensor(), normalize])
transform_base = [
transforms.Resize(size=(self.height, self.width)), # resize from 32x32 to 28x28
transforms.Lambda(lambda x: np.array(x).mean(axis=2) / 255.0) # make them black-and-white
]
transform_train = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(self.height, padding=4),
] + transform_base)
transform_test = transforms.Compose(transform_base)
transform_train = transform_train if self.augm_flag else transform_test
self.train_dataset = datasets.CIFAR10(self.data_dir, train=True, transform=transform_train, download=True)
self.test_dataset = datasets.CIFAR10(self.data_dir, train=False, transform=transform_test, download=True)
class CIFAR100(Dataset):
def __init__(self, batch_size, augm_flag):
super().__init__(batch_size, augm_flag)
self.n_train, self.n_test = 50000, 10000
self.n_classes = 100
self.height, self.width, self.n_colors = 32, 32, 3
self.data_dir = self.base_path + 'cifar100/'
# normalize = transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2470, 0.2435, 0.2616])
# transformations = transforms.Compose([transforms.ToTensor(), normalize])
transform_base = [transforms.Lambda(lambda x: np.array(x) / 255.0)]
transform_train = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(self.height, padding=4),
] + transform_base)
transform_test = transforms.Compose(transform_base)
transform_train = transform_train if self.augm_flag else transform_test
self.train_dataset = datasets.CIFAR100(self.data_dir, train=True, transform=transform_train, download=True)
self.test_dataset = datasets.CIFAR100(self.data_dir, train=False, transform=transform_test, download=True)
class SVHN(Dataset):
def __init__(self, batch_size, augm_flag):
super().__init__(batch_size, augm_flag)
self.n_train, self.n_test = 73257, 26032
self.n_classes = 10
self.height, self.width, self.n_colors = 32, 32, 3
self.data_dir = self.base_path + 'svhn/'
# normalize = transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2470, 0.2435, 0.2616])
# transformations = transforms.Compose([transforms.ToTensor(), normalize])
transform_base = [transforms.Lambda(lambda x: np.array(x) / 255.0)]
transform_train = transforms.Compose([
transforms.RandomCrop(self.height, padding=4),
] + transform_base)
transform_test = transforms.Compose(transform_base)
transform_train = transform_train if self.augm_flag else transform_test
self.train_dataset = datasets.SVHN(self.data_dir, split='train', transform=transform_train, download=True)
self.test_dataset = datasets.SVHN(self.data_dir, split='test', transform=transform_test, download=True)
class LSUNClassroom(Dataset):
def __init__(self, batch_size, augm_flag, test_only=True):
super().__init__(batch_size, augm_flag)
self.n_train, self.n_test = 168103, 300
self.n_classes = 1 # i.e. only the classroom
self.height, self.width, self.n_colors = 32, 32, 3
self.data_dir = '/scratch/maksym'
#self.data_dir = self.base_path + 'lsun/'
# normalize = transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2470, 0.2435, 0.2616])
# transformations = transforms.Compose([transforms.ToTensor(), normalize])
transform_base = [transforms.Lambda(lambda x: np.array(x) / 255.0)]
transform_train = transforms.Compose([
transforms.Resize(size=(self.height, self.width)),
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(self.height, padding=4),
] + transform_base)
transform_test = transforms.Compose([
transforms.Resize(size=(self.height, self.width))
] + transform_base)
transform_train = transform_train if self.augm_flag else transform_test
#self.train_dataset = torch.utils.data.Subset(datasets.LSUN(self.data_dir, classes=['classroom_train'], transform=transform_train), range(0, 10000))
if test_only:
self.test_dataset = datasets.LSUN(self.data_dir, classes=['classroom_val'], transform=transform_test)
else:
self.test_dataset = torch.utils.data.Subset(datasets.LSUN(self.data_dir, classes=['classroom_train'], transform=transform_test), range(0, 10000))
print('LSUNClassroom __init__ done')
class ImageNetMinusCifar10(Dataset):
def __init__(self, batch_size, augm_flag):
super().__init__(batch_size, augm_flag)
self.n_train, self.n_test = 500000, 30000
self.n_classes = 853
self.height, self.width, self.n_colors = 32, 32, 3
#self.train_dir = self.base_path + 'imagenet_minus_cifar10/imagenet/train'
self.test_dir = self.base_path + 'imagenet_minus_cifar10/imagenet/val_orig'
# normalize = transforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2470, 0.2435, 0.2616])
# transformations = transforms.Compose([transforms.ToTensor(), normalize])
transform_base = [transforms.Lambda(lambda x: np.array(x) / 255.0)]
transform_train = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(self.height, padding=4),
] + transform_base)
transform_test = transforms.Compose(transform_base)
transform_train = transform_train if self.augm_flag else transform_test
#self.train_dataset = torch.utils.data.Subset(datasets.ImageFolder(self.test_dir, transform=transform_test), range(0,10000))
self.test_dataset = torch.utils.data.Subset(datasets.ImageFolder(self.test_dir, transform=transform_test), np.random.permutation(range(self.n_test))[:10000])
print('ImageNetMinusCifar10 __init__ done')
# All available datasets
datasets_dict = {'mnist': MNIST,
'fmnist': FMNIST,
'cifar10_gray': CIFAR10Grayscale,
'emnist': EMNIST,
'cifar10': CIFAR10,
'cifar100': CIFAR100,
'svhn': SVHN,
'lsun_classroom': LSUNClassroom,
'imagenet_minus_cifar10': ImageNetMinusCifar10, # no automatic download; you have to have it on disk
}