-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
datasets.py
333 lines (272 loc) · 12.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
import os
import gzip
import tarfile
import pickle
import numpy as np
import matplotlib.pyplot as plt
from dezero.utils import get_file, cache_dir
from dezero.transforms import Compose, Flatten, ToFloat, Normalize
class Dataset:
def __init__(self, train=True, transform=None, target_transform=None):
self.train = train
self.transform = transform
self.target_transform = target_transform
if self.transform is None:
self.transform = lambda x: x
if self.target_transform is None:
self.target_transform = lambda x: x
self.data = None
self.label = None
self.prepare()
def __getitem__(self, index):
assert np.isscalar(index)
if self.label is None:
return self.transform(self.data[index]), None
else:
return self.transform(self.data[index]),\
self.target_transform(self.label[index])
def __len__(self):
return len(self.data)
def prepare(self):
pass
# =============================================================================
# Toy datasets
# =============================================================================
def get_spiral(train=True):
seed = 1984 if train else 2020
np.random.seed(seed=seed)
num_data, num_class, input_dim = 100, 3, 2
data_size = num_class * num_data
x = np.zeros((data_size, input_dim), dtype=np.float32)
t = np.zeros(data_size, dtype=int)
for j in range(num_class):
for i in range(num_data):
rate = i / num_data
radius = 1.0 * rate
theta = j * 4.0 + 4.0 * rate + np.random.randn() * 0.2
ix = num_data * j + i
x[ix] = np.array([radius * np.sin(theta),
radius * np.cos(theta)]).flatten()
t[ix] = j
# Shuffle
indices = np.random.permutation(num_data * num_class)
x = x[indices]
t = t[indices]
return x, t
class Spiral(Dataset):
def prepare(self):
self.data, self.label = get_spiral(self.train)
# =============================================================================
# MNIST-like dataset: MNIST / CIFAR /
# =============================================================================
class MNIST(Dataset):
def __init__(self, train=True,
transform=Compose([Flatten(), ToFloat(),
Normalize(0., 255.)]),
target_transform=None):
super().__init__(train, transform, target_transform)
def prepare(self):
#url = 'http://yann.lecun.com/exdb/mnist/'
url = 'https://ossci-datasets.s3.amazonaws.com/mnist/' # mirror site
train_files = {'target': 'train-images-idx3-ubyte.gz',
'label': 'train-labels-idx1-ubyte.gz'}
test_files = {'target': 't10k-images-idx3-ubyte.gz',
'label': 't10k-labels-idx1-ubyte.gz'}
files = train_files if self.train else test_files
data_path = get_file(url + files['target'])
label_path = get_file(url + files['label'])
self.data = self._load_data(data_path)
self.label = self._load_label(label_path)
def _load_label(self, filepath):
with gzip.open(filepath, 'rb') as f:
labels = np.frombuffer(f.read(), np.uint8, offset=8)
return labels
def _load_data(self, filepath):
with gzip.open(filepath, 'rb') as f:
data = np.frombuffer(f.read(), np.uint8, offset=16)
data = data.reshape(-1, 1, 28, 28)
return data
def show(self, row=10, col=10):
H, W = 28, 28
img = np.zeros((H * row, W * col))
for r in range(row):
for c in range(col):
img[r * H:(r + 1) * H, c * W:(c + 1) * W] = self.data[
np.random.randint(0, len(self.data) - 1)].reshape(H, W)
plt.imshow(img, cmap='gray', interpolation='nearest')
plt.axis('off')
plt.show()
@staticmethod
def labels():
return {0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9'}
class CIFAR10(Dataset):
def __init__(self, train=True,
transform=Compose([ToFloat(), Normalize(mean=0.5, std=0.5)]),
target_transform=None):
super().__init__(train, transform, target_transform)
def prepare(self):
url='https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz'
self.data, self.label = load_cache_npz(url, self.train)
if self.data is not None:
return
filepath = get_file(url)
if self.train:
self.data = np.empty((50000, 3 * 32 * 32))
self.label = np.empty((50000), dtype=int)
for i in range(5):
self.data[i * 10000:(i + 1) * 10000] = self._load_data(
filepath, i + 1, 'train')
self.label[i * 10000:(i + 1) * 10000] = self._load_label(
filepath, i + 1, 'train')
else:
self.data = self._load_data(filepath, 5, 'test')
self.label = self._load_label(filepath, 5, 'test')
self.data = self.data.reshape(-1, 3, 32, 32)
save_cache_npz(self.data, self.label, url, self.train)
def _load_data(self, filename, idx, data_type='train'):
assert data_type in ['train', 'test']
with tarfile.open(filename, 'r:gz') as file:
for item in file.getmembers():
if ('data_batch_{}'.format(idx) in item.name and data_type == 'train') or ('test_batch' in item.name and data_type == 'test'):
data_dict = pickle.load(file.extractfile(item), encoding='bytes')
data = data_dict[b'data']
return data
def _load_label(self, filename, idx, data_type='train'):
assert data_type in ['train', 'test']
with tarfile.open(filename, 'r:gz') as file:
for item in file.getmembers():
if ('data_batch_{}'.format(idx) in item.name and data_type == 'train') or ('test_batch' in item.name and data_type == 'test'):
data_dict = pickle.load(file.extractfile(item), encoding='bytes')
return np.array(data_dict[b'labels'])
def show(self, row=10, col=10):
H, W = 32, 32
img = np.zeros((H*row, W*col, 3))
for r in range(row):
for c in range(col):
img[r*H:(r+1)*H, c*W:(c+1)*W] = self.data[np.random.randint(0, len(self.data)-1)].reshape(3,H,W).transpose(1,2,0)/255
plt.imshow(img, interpolation='nearest')
plt.axis('off')
plt.show()
@staticmethod
def labels():
return {0: 'ariplane', 1: 'automobile', 2: 'bird', 3: 'cat', 4: 'deer', 5: 'dog', 6: 'frog', 7: 'horse', 8: 'ship', 9: 'truck'}
class CIFAR100(CIFAR10):
def __init__(self, train=True,
transform=Compose([ToFloat(), Normalize(mean=0.5, std=0.5)]),
target_transform=None,
label_type='fine'):
assert label_type in ['fine', 'coarse']
self.label_type = label_type
super().__init__(train, transform, target_transform)
def prepare(self):
url = 'https://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz'
self.data, self.label = load_cache_npz(url, self.train)
if self.data is not None:
return
filepath = get_file(url)
if self.train:
self.data = self._load_data(filepath, 'train')
self.label = self._load_label(filepath, 'train')
else:
self.data = self._load_data(filepath, 'test')
self.label = self._load_label(filepath, 'test')
self.data = self.data.reshape(-1, 3, 32, 32)
save_cache_npz(self.data, self.label, url, self.train)
def _load_data(self, filename, data_type='train'):
with tarfile.open(filename, 'r:gz') as file:
for item in file.getmembers():
if data_type in item.name:
data_dict = pickle.load(file.extractfile(item), encoding='bytes')
data = data_dict[b'data']
return data
def _load_label(self, filename, data_type='train'):
assert data_type in ['train', 'test']
with tarfile.open(filename, 'r:gz') as file:
for item in file.getmembers():
if data_type in item.name:
data_dict = pickle.load(file.extractfile(item), encoding='bytes')
if self.label_type == 'fine':
return np.array(data_dict[b'fine_labels'])
elif self.label_type == 'coarse':
return np.array(data_dict[b'coarse_labels'])
@staticmethod
def labels(label_type='fine'):
coarse_labels = dict(enumerate(['aquatic mammals','fish','flowers','food containers','fruit and vegetables','household electrical device','household furniture','insects','large carnivores','large man-made outdoor things','large natural outdoor scenes','large omnivores and herbivores','medium-sized mammals','non-insect invertebrates','people','reptiles','small mammals','trees','vehicles 1','vehicles 2']))
fine_labels = []
return fine_labels if label_type is 'fine' else coarse_labels
# =============================================================================
# Big datasets
# =============================================================================
class ImageNet(Dataset):
def __init__(self):
NotImplemented
@staticmethod
def labels():
url = 'https://gist.githubusercontent.com/yrevar/942d3a0ac09ec9e5eb3a/raw/238f720ff059c1f82f368259d1ca4ffa5dd8f9f5/imagenet1000_clsidx_to_labels.txt'
path = get_file(url)
with open(path, 'r') as f:
labels = eval(f.read())
return labels
# =============================================================================
# Sequential datasets: SinCurve, Shapekspare
# =============================================================================
class SinCurve(Dataset):
def prepare(self):
num_data = 1000
dtype = np.float64
x = np.linspace(0, 2 * np.pi, num_data)
noise_range = (-0.05, 0.05)
noise = np.random.uniform(noise_range[0], noise_range[1], size=x.shape)
if self.train:
y = np.sin(x) + noise
else:
y = np.cos(x)
y = y.astype(dtype)
self.data = y[:-1][:, np.newaxis]
self.label = y[1:][:, np.newaxis]
class Shakespear(Dataset):
def prepare(self):
url = 'https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt'
file_name = 'shakespear.txt'
path = get_file(url, file_name)
with open(path, 'r') as f:
data = f.read()
chars = list(data)
char_to_id = {}
id_to_char = {}
for word in data:
if word not in char_to_id:
new_id = len(char_to_id)
char_to_id[word] = new_id
id_to_char[new_id] = word
indices = np.array([char_to_id[c] for c in chars])
self.data = indices[:-1]
self.label = indices[1:]
self.char_to_id = char_to_id
self.id_to_char = id_to_char
# =============================================================================
# Utils
# =============================================================================
def load_cache_npz(filename, train=False):
filename = filename[filename.rfind('/') + 1:]
prefix = '.train.npz' if train else '.test.npz'
filepath = os.path.join(cache_dir, filename + prefix)
if not os.path.exists(filepath):
return None, None
loaded = np.load(filepath)
return loaded['data'], loaded['label']
def save_cache_npz(data, label, filename, train=False):
filename = filename[filename.rfind('/') + 1:]
prefix = '.train.npz' if train else '.test.npz'
filepath = os.path.join(cache_dir, filename + prefix)
if os.path.exists(filepath):
return
print("Saving: " + filename + prefix)
try:
np.savez_compressed(filepath, data=data, label=label)
except (Exception, KeyboardInterrupt) as e:
if os.path.exists(filepath):
os.remove(filepath)
raise
print(" Done")
return filepath