-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloading_cifar10.py
53 lines (40 loc) · 1.59 KB
/
loading_cifar10.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
import numpy as np
import matplotlib.pyplot as plt
import pickle
#label_names= [airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck]
def unpickle(file):
'''Load byte data from file'''
with open(file, 'rb') as f:
data = pickle.load(f, encoding='latin-1')
return data
def load_cifar10_data(data_dir):
'''Return train_data, train_labels, test_data, test_labels
The shape of data is 32 x 32 x3'''
train_data = None
train_labels = []
for i in range(1, 6):
data_dic = unpickle(data_dir + "/data_batch_{}".format(i))
if i == 1:
train_data = data_dic['data']
else:
train_data = np.vstack((train_data, data_dic['data']))
train_labels += data_dic['labels']
test_data_dic = unpickle(data_dir + "/test_batch")
test_data = test_data_dic['data']
test_labels = test_data_dic['labels']
train_data = train_data.reshape((len(train_data), 3, 32, 32))
train_data = np.rollaxis(train_data, 1, 4)
train_labels = np.array(train_labels)
test_data = test_data.reshape((len(test_data), 3, 32, 32))
test_data = np.rollaxis(test_data, 1, 4)
test_labels = np.array(test_labels)
return train_data, train_labels, test_data, test_labels
data_dir = 'cifar-10-batches-py'
train_data, train_labels, test_data, test_labels = load_cifar10_data(data_dir)
print(train_data.shape)
print(train_labels.shape)
print(test_data.shape)
print(test_labels.shape)
# In order to check where the data shows an image correctly
plt.imshow(train_data[2])
plt.show()