-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreprocess_data.py
More file actions
174 lines (127 loc) · 5.18 KB
/
preprocess_data.py
File metadata and controls
174 lines (127 loc) · 5.18 KB
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
"""
Download data from cifar webpage,
unpack the downloaded tar file,
convert data to BGR format and save to HDF5.
Visualize sample images from the data.
"""
from __future__ import print_function
from preprocessor import utils
from ConfigParser import SafeConfigParser
import argparse
import os, glob
import h5py
import cv2
#Create command line arguments parser
ap = argparse.ArgumentParser()
ap.add_argument('-c', '--config', required=True, help='Path to the configuration file')
ap.add_argument('-d', '--download', action='store_true', help='Download data from external source')
ap.add_argument('-u', '--unpack', action='store_true', help='Extract downloaded data')
ap.add_argument('-p', '--preprocess', action='store_true', help='Preprocess data and save to HDF5')
ap.add_argument('-s', '--samples', nargs='?', type=int, choices=[10,20,30,50], const=True,
action='store', help='Show and save n sample images from each category')
args = vars(ap.parse_args())
#Parse config file
parser = SafeConfigParser()
parser.read(args['config'])
#Set local name for external file
cifar_file = 'cifar_data.tar.gz'
#Execute if the script started with the -d option
if args['download']:
url = parser.get('external_paths', 'cifar10_url')
destination = parser.get('local_paths', 'raw_data')
utils.download_file(url, os.path.join(destination, cifar_file))
#Execute if the script started with the -u option
if args['unpack']:
raw_data_path = parser.get('local_paths', 'raw_data')
source_path = os.path.join(raw_data_path, cifar_file)
#Check if the file exists
if os.path.exists(source_path):
utils.unpack_tar(source_path, raw_data_path)
else:
print('[WARNING] File {} does not exist in {}'.format(cifar_file, raw_data_path))
#Execute if the script started with the -p option
if args['preprocess']:
#Define local helper functions
def prepare_datasets(file_path,features_ds, labels_ds):
data_file = h5py.File(file_path, mode='w')
data_file.create_dataset(features_ds, (0,32,32,3), maxshape=(None,32,32,3),
dtype='uint8')
data_file.create_dataset(labels_ds, (0,), maxshape=(None,),
dtype='uint8')
return data_file
def process_cifar(file):
cifar_dict = utils.unpickle(file)
img_array = utils.cvt_cifar(cifar_dict['data'])
utils.save_data(img_array, data_file[features_ds])
utils.save_data(cifar_dict['labels'], data_file[labels_ds])
#Get datasets names
features_ds = parser.get('datasets', 'data')
labels_ds = parser.get('datasets', 'target')
#--------------------#
#Process training data
#--------------------#
training_data = parser.get('local_paths', 'training_data')
#Check if hdf5 file with training data exists
if os.path.exists(training_data):
data_file = h5py.File(training_data, mode='a')
else:
data_file = prepare_datasets(training_data, features_ds, labels_ds)
#Get paths to pickle files with training data
raw_data_path = parser.get('local_paths', 'raw_data')
training_files = glob.glob(os.path.join(raw_data_path, 'data_batch_*') )
#Convert and save training data to hdf5 file
print('[INFO] Saving training data to {}'.format(training_data))
for file in training_files:
process_cifar(file)
print(' [INFO] Saved {} rows to {}'.format(data_file[features_ds].shape[0], training_data))
data_file.close()
#--------------------#
#Process testing data
#--------------------#
testing_data = parser.get('local_paths', 'testing_data')
#Check if hdf5 file with testing data exists
if os.path.exists(testing_data):
data_file = h5py.File(testing_data, mode='a')
else:
data_file = prepare_datasets(testing_data, features_ds, labels_ds)
#Get path to a picke file with testing data
testing_file = os.path.join(raw_data_path, 'test_batch')
#Convert and save testing data to hdf5 file
print('[INFO] Saving testing data to {}'.format(testing_data))
process_cifar(testing_file)
print('[INFO] Saved {} rows to {}'.format(data_file[features_ds].shape[0], testing_data))
data_file.close()
#Execute if the script started with the -s option
if args['samples']:
#Max accepted value is 50
n = args['samples']
#Get paths to necessary files
data_path = parser.get('local_paths', 'training_data')
figures_path = parser.get('local_paths', 'figures')
#Get datasets names
features_ds = parser.get('datasets', 'data')
labels_ds = parser.get('datasets', 'target')
#Check if HDF5 file with training data exists
if os.path.exists(data_path):
file = h5py.File(data_path,'r')
'''
As stated on the CIFAR webpage, every pickle file contains roughly
the same number of images from each category.
Therefore it's safe to select random images only from the subset of the data.
'''
#Get 10000 images and labels from data
data = file[features_ds][:10000]
labels = file[labels_ds][:10000]
file.close()
#Get random images from data
samples = utils.get_samples(data, labels, n)
#Create 2D image from sample images
mosaic = utils.create_mosaic(samples)
#Save image to disk
cv2.imwrite(os.path.join(figures_path, 'samples.png'), mosaic)
print('[INFO] Saved image to {}'.format(figures_path))
#Show generated image
cv2.imshow("Sample images", mosaic)
cv2.waitKey(0)
else:
print('[WARNING] {} does not exist'.format(data_path))