-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_mnist.py
170 lines (140 loc) · 5.04 KB
/
save_mnist.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
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 30 10:28:03 2018
@author: A.Akl
"""
import os
import scipy.misc
from tensorflow.contrib.learn.python.learn.datasets.mnist import extract_images, extract_labels
data_path = "/emnist-digits/"
output_dir = "output_dir/"
train_images_path = os.path.join(data_path,"emnist-digits-train-images-idx3-ubyte.gz")
train_labels_path = os.path.join(data_path,"emnist-digits-train-labels-idx1-ubyte.gz")
test_images_path = os.path.join(data_path,"emnist-digits-test-images-idx3-ubyte.gz")
test_labels_path = os.path.join(data_path,"emnist-digits-test-labels-idx1-ubyte.gz")
# load training images binary file
with open(train_images_path, 'rb') as f:
train_images = extract_images(f)
# load training labels binary file
with open(train_labels_path, 'rb') as f:
train_labels = extract_labels(f)
# load test images binary file
with open(test_images_path, 'rb') as f:
test_images = extract_images(f)
# load test labels binary file
with open(test_labels_path, 'rb') as f:
test_labels = extract_labels(f)
def save_class0(save_dir,image,i):
class_dir = os.path.join(output_dir, save_dir +'/class0')
file_name = str(class_dir + '/' + str(i) + '.jpg')
if not os.path.exists(class_dir):
os.mkdir(class_dir)
scipy.misc.imsave(file_name,image)
else:
scipy.misc.imsave(file_name,image)
def save_class1(save_dir,image,i):
class_dir = os.path.join(output_dir, save_dir +'/class1')
file_name = str(class_dir + '/' + str(i) + '.jpg')
if not os.path.exists(class_dir):
os.mkdir(class_dir)
scipy.misc.imsave(file_name,image)
else:
scipy.misc.imsave(file_name,image)
def save_class2(save_dir,image,i):
class_dir = os.path.join(output_dir, save_dir +'/class2')
file_name = str(class_dir + '/' + str(i) + '.jpg')
if not os.path.exists(class_dir):
os.mkdir(class_dir)
scipy.misc.imsave(file_name,image)
else:
scipy.misc.imsave(file_name,image)
def save_class3(save_dir, image,i):
class_dir = os.path.join(output_dir, save_dir +'/class3')
file_name = str(class_dir + '/' + str(i) + '.jpg')
if not os.path.exists(class_dir):
os.mkdir(class_dir)
scipy.misc.imsave(file_name,image)
else:
scipy.misc.imsave(file_name,image)
def save_class4(save_dir,image,i):
class_dir = os.path.join(output_dir, save_dir +'/class4')
file_name = str(class_dir + '/' + str(i) + '.jpg')
if not os.path.exists(class_dir):
os.mkdir(class_dir)
scipy.misc.imsave(file_name,image)
else:
scipy.misc.imsave(file_name,image)
def save_class5(save_dir,image,i):
class_dir = os.path.join(output_dir, save_dir +'/class5')
file_name = str(class_dir + '/' + str(i) + '.jpg')
if not os.path.exists(class_dir):
os.mkdir(class_dir)
scipy.misc.imsave(file_name,image)
else:
scipy.misc.imsave(file_name,image)
def save_class6(save_dir,image,i):
class_dir = os.path.join(output_dir, save_dir +'/class6')
# print(class_dir)
file_name = str(class_dir + '/' + str(i) + '.jpg')
if not os.path.exists(class_dir):
os.mkdir(class_dir)
scipy.misc.imsave(file_name,image)
else:
scipy.misc.imsave(file_name,image)
def save_class7(save_dir,image,i):
class_dir = os.path.join(output_dir, save_dir +'/class7')
file_name = str(class_dir + '/' + str(i) + '.jpg')
if not os.path.exists(class_dir):
os.mkdir(class_dir)
scipy.misc.imsave(file_name,image)
else:
scipy.misc.imsave(file_name,image)
def save_class8(save_dir,image,i):
class_dir = os.path.join(output_dir, save_dir +'/class8')
file_name = str(class_dir + '/' + str(i) + '.jpg')
if not os.path.exists(class_dir):
os.mkdir(class_dir)
scipy.misc.imsave(file_name,image)
else:
scipy.misc.imsave(file_name,image)
def save_class9(save_dir,image,i):
class_dir = os.path.join(output_dir, save_dir +'/class9')
file_name = str(class_dir + '/' + str(i) + '.jpg')
if not os.path.exists(class_dir):
os.mkdir(class_dir)
scipy.misc.imsave(file_name,image)
else:
scipy.misc.imsave(file_name,image)
# dict instead of switch case or if else technique
class_label = {
0: save_class0,
1: save_class1,
2: save_class2,
3: save_class3,
4: save_class4,
5: save_class5,
6: save_class6,
7: save_class7,
8: save_class8,
9: save_class9
}
# saving training data
i = 0
num_images = len(train_images)
for i in range(0, num_images):
image = train_images[i]
image = image.transpose([1,2,0])
image = image.reshape(28,28)
label = train_labels[i]
class_label[label]('train',image,i) # call dict as method
i += 1
# saving test data
i = 0
num_images = len(test_images)
for i in range(0, num_images):
image = test_images[i]
image = image.transpose([1,2,0])
image = image.reshape(28,28)
label = test_labels[i]
class_label[label]('test',image,i) # call dict as method
i += 1