forked from wenxinxu/resnet-in-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cifar10_input.py
190 lines (151 loc) · 6.68 KB
/
cifar10_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
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
# Coder: Wenxin Xu
# Github: https://github.com/wenxinxu/resnet_in_tensorflow
# ==============================================================================
import tarfile
from six.moves import urllib
import sys
import numpy as np
import cPickle
import os
import cv2
data_dir = 'cifar10_data'
full_data_dir = 'cifar10_data/cifar-10-batches-py/data_batch_'
vali_dir = 'cifar10_data/cifar-10-batches-py/test_batch'
DATA_URL = 'http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz'
IMG_WIDTH = 32
IMG_HEIGHT = 32
IMG_DEPTH = 3
NUM_CLASS = 10
TRAIN_RANDOM_LABEL = False # Want to use random label for train data?
VALI_RANDOM_LABEL = False # Want to use random label for validation?
NUM_TRAIN_BATCH = 5 # How many batches of files you want to read in, from 0 to 5)
EPOCH_SIZE = 10000 * NUM_TRAIN_BATCH
def maybe_download_and_extract():
'''
Will download and extract the cifar10 data automatically
:return: nothing
'''
dest_directory = data_dir
if not os.path.exists(dest_directory):
os.makedirs(dest_directory)
filename = DATA_URL.split('/')[-1]
filepath = os.path.join(dest_directory, filename)
if not os.path.exists(filepath):
def _progress(count, block_size, total_size):
sys.stdout.write('\r>> Downloading %s %.1f%%' % (filename, float(count * block_size)
/ float(total_size) * 100.0))
sys.stdout.flush()
filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress)
print()
statinfo = os.stat(filepath)
print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')
tarfile.open(filepath, 'r:gz').extractall(dest_directory)
def _read_one_batch(path, is_random_label):
'''
The training data contains five data batches in total. The validation data has only one
batch. This function takes the directory of one batch of data and returns the images and
corresponding labels as numpy arrays
:param path: the directory of one batch of data
:param is_random_label: do you want to use random labels?
:return: image numpy arrays and label numpy arrays
'''
fo = open(path, 'rb')
dicts = cPickle.load(fo)
fo.close()
data = dicts['data']
if is_random_label is False:
label = np.array(dicts['labels'])
else:
labels = np.random.randint(low=0, high=10, size=10000)
label = np.array(labels)
return data, label
def read_in_all_images(address_list, shuffle=True, is_random_label = False):
"""
This function reads all training or validation data, shuffles them if needed, and returns the
images and the corresponding labels as numpy arrays
:param address_list: a list of paths of cPickle files
:return: concatenated numpy array of data and labels. Data are in 4D arrays: [num_images,
image_height, image_width, image_depth] and labels are in 1D arrays: [num_images]
"""
data = np.array([]).reshape([0, IMG_WIDTH * IMG_HEIGHT * IMG_DEPTH])
label = np.array([])
for address in address_list:
print 'Reading images from ' + address
batch_data, batch_label = _read_one_batch(address, is_random_label)
# Concatenate along axis 0 by default
data = np.concatenate((data, batch_data))
label = np.concatenate((label, batch_label))
num_data = len(label)
# This reshape order is really important. Don't change
# Reshape is correct. Double checked
data = data.reshape((num_data, IMG_HEIGHT * IMG_WIDTH, IMG_DEPTH), order='F')
data = data.reshape((num_data, IMG_HEIGHT, IMG_WIDTH, IMG_DEPTH))
if shuffle is True:
print 'Shuffling'
order = np.random.permutation(num_data)
data = data[order, ...]
label = label[order]
data = data.astype(np.float32)
return data, label
def horizontal_flip(image, axis):
'''
Flip an image at 50% possibility
:param image: a 3 dimensional numpy array representing an image
:param axis: 0 for vertical flip and 1 for horizontal flip
:return: 3D image after flip
'''
flip_prop = np.random.randint(low=0, high=2)
if flip_prop == 0:
image = cv2.flip(image, axis)
return image
def whitening_image(image_np):
'''
Performs per_image_whitening
:param image_np: a 4D numpy array representing a batch of images
:return: the image numpy array after whitened
'''
for i in range(len(image_np)):
mean = np.mean(image_np[i, ...])
# Use adjusted standard deviation here, in case the std == 0.
std = np.max(np.std(image_np[i, ...]), 1.0/np.sqrt(IMG_HEIGHT * IMG_WIDTH * IMG_DEPTH))
image_np[i,...] = (image_np[i, ...] - mean) / std
return image_np
def random_crop_and_flip(batch_data, padding_size):
'''
Helper to random crop and random flip a batch of images
:param padding_size: int. how many layers of 0 padding was added to each side
:param batch_data: a 4D batch array
:return: randomly cropped and flipped image
'''
cropped_batch = np.zeros(len(batch_data) * IMG_HEIGHT * IMG_WIDTH * IMG_DEPTH).reshape(
len(batch_data), IMG_HEIGHT, IMG_WIDTH, IMG_DEPTH)
for i in range(len(batch_data)):
x_offset = np.random.randint(low=0, high=2 * padding_size, size=1)[0]
y_offset = np.random.randint(low=0, high=2 * padding_size, size=1)[0]
cropped_batch[i, ...] = batch_data[i, ...][x_offset:x_offset+IMG_HEIGHT,
y_offset:y_offset+IMG_WIDTH, :]
cropped_batch[i, ...] = horizontal_flip(image=cropped_batch[i, ...], axis=1)
return cropped_batch
def prepare_train_data(padding_size):
'''
Read all the train data into numpy array and add padding_size of 0 paddings on each side of the
image
:param padding_size: int. how many layers of zero pads to add on each side?
:return: all the train data and corresponding labels
'''
path_list = []
for i in range(1, NUM_TRAIN_BATCH+1):
path_list.append(full_data_dir + str(i))
data, label = read_in_all_images(path_list, is_random_label=TRAIN_RANDOM_LABEL)
pad_width = ((0, 0), (padding_size, padding_size), (padding_size, padding_size), (0, 0))
data = np.pad(data, pad_width=pad_width, mode='constant', constant_values=0)
return data, label
def read_validation_data():
'''
Read in validation data. Whitening at the same time
:return: Validation image data as 4D numpy array. Validation labels as 1D numpy array
'''
validation_array, validation_labels = read_in_all_images([vali_dir],
is_random_label=VALI_RANDOM_LABEL)
validation_array = whitening_image(validation_array)
return validation_array, validation_labels