-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathload_cifar.py
79 lines (55 loc) · 1.91 KB
/
load_cifar.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
import numpy as np
import os
DATA_PATH = './'
def one_hot(x, n):
x = np.array(x)
assert x.ndim == 1
return np.eye(n)[x]
def load_raw_image():
dir = DATA_PATH + 'cifar-10-batches-py/'
train_x = []
def _load_batch_raw_cifar10(filename):
batch = np.load(filename)
data = batch['data']
return data
for filename in os.listdir(dir):
path = dir + filename
if filename[:4] == 'data':
x = _load_batch_raw_cifar10(path)
train_x.append(x)
train_x = np.concatenate(train_x, axis=0)
return train_x
def _load_batch_cifar10(filename):
batch = np.load(filename)
data = batch['data'] / 255.0 # scale between [0, 1]
labels = np.asarray(batch['labels']) # convert labels to one-hot representation
return data.astype('float32'), labels.astype('int32')
def _load_batch_cifar100(filename):
batch = np.load(filename)
data = batch['data'] / 255.0
labels = np.asarray(batch['fine_labels'])
return data.astype('float32'), labels.astype('int32')
def load_cifar10():
dir = DATA_PATH + 'cifar-10-batches-py/'
train_x, train_y = [], []
for filename in [1, 2, 3, 4, 5]:
path = dir + 'data_batch_{}'.format(filename)
x, y = _load_batch_cifar10(path)
train_x.append(x)
train_y.append(y)
test_x, test_y = _load_batch_cifar10(dir + 'test_batch')
train_x = np.concatenate(train_x, axis=0)
train_y = np.concatenate(train_y, axis=0)
return train_x, train_y, test_x, test_y
def load_cifar100():
dir = DATA_PATH + 'cifar-100-python/'
train_x, train_y = _load_batch_cifar100(dir + 'train')
test_x, test_y = _load_batch_cifar100(dir + 'test')
return train_x, train_y, test_x, test_y
def load_cifar(label_size=10):
if label_size == 10:
return load_cifar10()
elif label_size == 100:
return load_cifar100()
if __name__ == '__main__':
pass