-
Notifications
You must be signed in to change notification settings - Fork 3
/
preprocess_usps.py
63 lines (50 loc) · 2.25 KB
/
preprocess_usps.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
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 2 16:23:56 2018
@author: luckycallor
"""
from sklearn import datasets as ds
import numpy as np
import pickle
from PIL import Image
from tensorflow.examples.tutorials.mnist import input_data
def resize_images(image_arrays, size=[32, 32]):
# convert float type to integer
image_arrays = (image_arrays * 255).astype('uint8')
resized_image_arrays = np.zeros([image_arrays.shape[0]] + size + [3])
for i, image_array in enumerate(image_arrays):
image = Image.fromarray(image_array)
resized_image = image.resize(size=size, resample=Image.ANTIALIAS)
final_image = np.zeros(size+[3])
final_image[:,:,0] = final_image[:,:,1] = final_image[:,:,2] = resized_image
resized_image_arrays[i] = np.asarray(final_image)
return resized_image_arrays
def resize_cover_images(image_arrays, size=[22, 22]):
# image_arrays = (image_arrays * 255).astype('uint8')
ori_size = image_arrays.shape[1]
new_size = size[0]
s = (new_size-ori_size)//2
resized_image_arrays = np.ones([image_arrays.shape[0]] + size) * -1
resized_image_arrays[:, s: s+ori_size, s: s+ori_size] = image_arrays
resized_image_arrays = (resized_image_arrays+1)/2
# resized_image_arrays = (resized_image_arrays*255).astype('unit8')
return resized_image_arrays
def save_pickle(data, path):
with open(path, 'wb') as f:
pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
print ('Saved %s..' %path)
def main():
train_path = r'F:\digit-data\usps\usps_train'
test_path = r'F:\digit-data\usps\usps_test'
train_x, train_y = ds.load_svmlight_file(train_path)
test_x, test_y = ds.load_svmlight_file(test_path)
train_x = np.array(train_x.todense())
test_x = np.array(test_x.todense())
train = {'X': resize_images(resize_cover_images(train_x.reshape(-1, 16, 16))),
'y': np.array([y-1 for y in train_y])}
test = {'X': resize_images(resize_cover_images(test_x.reshape(-1, 16, 16))),
'y': np.array([y-1 for y in test_y])}
save_pickle(train, r'F:\digit-data\usps_1\train.pkl')
save_pickle(test, r'F:\digit-data\usps_1\test.pkl')
if __name__ == "__main__":
main()