-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_load_cifar100.py
57 lines (43 loc) · 1.82 KB
/
local_load_cifar100.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
import pickle
import numpy as np
from keras.utils.np_utils import to_categorical
def unpickle(file):
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
def load_cifar100(path="data/"):
meta = unpickle(path+'meta')
train = unpickle(path+'train')
fine_label_names = [t.decode('utf8') for t in meta[b'fine_label_names']]
filenames = [t.decode('utf8') for t in train[b'filenames']]
fine_labels = train[b'fine_labels']
test = unpickle(path+'test')
test_filenames = [t.decode('utf8') for t in test[b'filenames']]
test_fine_labels = test[b'fine_labels']
train_data = train[b'data']
test_data = test[b'data']
train_images = list()
for d in train_data:
image = np.zeros((32,32,3), dtype=np.uint8)
image[...,0] = np.reshape(d[:1024], (32,32)) # Red channel
image[...,1] = np.reshape(d[1024:2048], (32,32)) # Green channel
image[...,2] = np.reshape(d[2048:], (32,32)) # Blue channel
train_images.append(image)
test_images = list()
for d in test_data:
image = np.zeros((32,32,3), dtype=np.uint8)
image[...,0] = np.reshape(d[:1024], (32,32)) # Red channel
image[...,1] = np.reshape(d[1024:2048], (32,32)) # Green channel
image[...,2] = np.reshape(d[2048:], (32,32)) # Blue channel
test_images.append(image)
x_train = np.array(train_images)
y_train = np.array(fine_labels)
y_train = np.reshape(y_train,(50000,1))
x_test = np.array(test_images)
y_test = np.array(test_fine_labels)
y_test = np.reshape(y_test,(10000,1))
assert x_train.shape == (50000, 32, 32, 3)
assert x_test.shape == (10000, 32, 32, 3)
assert y_train.shape == (50000, 1)
assert y_test.shape == (10000, 1)
return (x_train, y_train), (x_test, y_test)