forked from lrjconan/deep_parsimonious
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CIFAR_input.py
132 lines (102 loc) · 4.26 KB
/
CIFAR_input.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
import os
import cPickle
import numpy as np
def unpickle(file):
fo = open(file, 'rb')
dict = cPickle.load(fo)
fo.close()
return dict
def read_CIFAR10(data_folder):
""" Reads and parses examples from CIFAR10 data files """
# Constants describing the CIFAR-10 data set.
img_height = 32
img_width = 32
num_class = 10
num_channel = 3
num_val_img = 5000
train_img = []
train_label = []
test_img = []
test_label = []
train_file_list = ['data_batch_1', 'data_batch_2',
'data_batch_3', 'data_batch_4', 'data_batch_5']
test_file_list = ['test_batch']
for i in xrange(len(train_file_list)):
tmp_dict = unpickle(os.path.join(data_folder, train_file_list[i]))
train_img.append(tmp_dict['data'])
train_label.append(tmp_dict['labels'])
tmp_dict = unpickle(os.path.join(data_folder, test_file_list[0]))
test_img.append(tmp_dict['data'])
test_label.append(tmp_dict['labels'])
train_img = np.concatenate(train_img)
train_label = np.concatenate(train_label)
test_img = np.concatenate(test_img)
test_label = np.concatenate(test_label)
train_img = np.reshape(train_img, [-1, num_channel, img_height, img_width])
test_img = np.reshape(test_img, [-1, num_channel, img_height, img_width])
# change format from [B, C, H, W] to [B, H, W, C] for feeding to Tensorflow
train_img = np.transpose(train_img, [0, 2, 3, 1])
test_img = np.transpose(test_img, [0, 2, 3, 1])
mean_img = np.mean(np.concatenate([train_img, test_img]), axis=0)
# random split for train/val
num_train_img = train_img.shape[0] - num_val_img
idx_rand = np.random.permutation(train_img.shape[0])
train_img_new = train_img[idx_rand[:num_train_img], :, :, :]
val_img = train_img[idx_rand[num_train_img:], :, :, :]
train_label_new = train_label[idx_rand[:num_train_img]]
val_label = train_label[idx_rand[num_train_img:]]
CIFAR10_data = {}
CIFAR10_data['train_img'] = train_img_new
CIFAR10_data['val_img'] = val_img
CIFAR10_data['test_img'] = test_img
CIFAR10_data['train_label'] = train_label_new
CIFAR10_data['val_label'] = val_label
CIFAR10_data['test_label'] = test_label
CIFAR10_data['mean_img'] = mean_img
return CIFAR10_data
def read_CIFAR100(data_folder):
""" Reads and parses examples from CIFAR100 python data files """
# Constants describing the CIFAR-100 data set.
img_height = 32
img_width = 32
num_class = 100
num_channel = 3
num_val_img = 5000
train_img = []
train_label = []
test_img = []
test_label = []
train_file_list = ['train']
test_file_list = ['test']
tmp_dict = unpickle(os.path.join(data_folder, train_file_list[0]))
train_img.append(tmp_dict['data'])
train_label.append(tmp_dict['fine_labels'])
tmp_dict = unpickle(os.path.join(data_folder, test_file_list[0]))
test_img.append(tmp_dict['data'])
test_label.append(tmp_dict['fine_labels'])
train_img = np.concatenate(train_img)
train_label = np.concatenate(train_label)
test_img = np.concatenate(test_img)
test_label = np.concatenate(test_label)
train_img = np.reshape(train_img, [-1, num_channel, img_height, img_width])
test_img = np.reshape(test_img, [-1, num_channel, img_height, img_width])
# change format from [B, C, H, W] to [B, H, W, C] for feeding to Tensorflow
train_img = np.transpose(train_img, [0, 2, 3, 1])
test_img = np.transpose(test_img, [0, 2, 3, 1])
mean_img = np.mean(np.concatenate([train_img, test_img]), axis=0)
# random split for train/val
num_train_img = train_img.shape[0] - num_val_img
idx_rand = np.random.permutation(train_img.shape[0])
train_img_new = train_img[idx_rand[:num_train_img], :, :, :]
val_img = train_img[idx_rand[num_train_img:], :, :, :]
train_label_new = train_label[idx_rand[:num_train_img]]
val_label = train_label[idx_rand[num_train_img:]]
CIFAR100_data = {}
CIFAR100_data['train_img'] = train_img_new
CIFAR100_data['val_img'] = val_img
CIFAR100_data['test_img'] = test_img
CIFAR100_data['train_label'] = train_label_new
CIFAR100_data['val_label'] = val_label
CIFAR100_data['test_label'] = test_label
CIFAR100_data['mean_img'] = mean_img
return CIFAR100_data