-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist_data.py
263 lines (200 loc) · 8.04 KB
/
mnist_data.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 29 19:12:15 2017
Loads the MNIST data into training and testing sets
(X_train, Y_train) and (X_test, Y_test), respectively.
X_train and X_test are normalized matrices where "one row = one sample",
with values between -1 and 1 unless specified.
Y_train and Y_test are matrices where "one row = one label" in
one-hot vector form of length 10.
@author: bettmensch
"""
import numpy
import os
import struct
from array import array
import random
_allowed_modes = (
# integer values in {0..255}
'vanilla',
# integer values in {0,1}
# values set at 1 (instead of 0) with probability p = orig/255
# as in Ruslan Salakhutdinov and Iain Murray's paper
# 'On The Quantitative Analysis of Deep Belief Network' (2008)
'randomly_binarized',
# integer values in {0,1}
# values set at 1 (instead of 0) if orig/255 > 0.5
'rounded_binarized',
)
_allowed_return_types = (
# default return type. Computationally more expensive.
# Useful if numpy is not installed.
'lists',
# Numpy module will be dynamically loaded on demand.
'numpy',
)
np = None
def _import_numpy():
# will be called only when the numpy return type has been specifically
# requested via the 'return_type' parameter in MNIST class' constructor.
global np
if np is None: # import only once
try:
import numpy as _np
except ImportError as e:
raise MNISTException(
"need to have numpy installed to return numpy arrays."\
+" Otherwise, please set return_type='lists' in constructor."
)
np = _np
else:
pass # was already previously imported
return np
class MNISTException(Exception):
pass
class MNIST(object):
def __init__(self, path='.', mode='vanilla', return_type='lists'):
self.path = path
assert mode in _allowed_modes, \
"selected mode '{}' not in {}".format(mode,_allowed_modes)
self._mode = mode
assert return_type in _allowed_return_types, \
"selected return_type '{}' not in {}".format(
return_type,
_allowed_return_types
)
self._return_type = return_type
self.test_img_fname = 'mnist_training_images'
self.test_lbl_fname = 'mnist_training_labels'
self.train_img_fname = 'mnist_testing_images'
self.train_lbl_fname = 'mnist_testing_labels'
self.test_images = []
self.test_labels = []
self.train_images = []
self.train_labels = []
@property # read only because set only once, via constructor
def mode(self):
return self._mode
@property # read only because set only once, via constructor
def return_type(self):
return self._return_type
def load_testing(self):
ims, labels = self.load(os.path.join(self.path, self.test_img_fname),
os.path.join(self.path, self.test_lbl_fname))
self.test_images = self.process_images(ims)
self.test_labels = self.process_labels(labels)
return self.test_images, self.test_labels
def load_training(self):
ims, labels = self.load(os.path.join(self.path, self.train_img_fname),
os.path.join(self.path, self.train_lbl_fname))
self.train_images = self.process_images(ims)
self.train_labels = self.process_labels(labels)
return self.train_images, self.train_labels
def process_images(self, images):
if self.return_type is 'lists':
return self.process_images_to_lists(images)
elif self.return_type is 'numpy':
return self.process_images_to_numpy(images)
else:
raise MNISTException("unknown return_type '{}'".format(self.return_type))
def process_labels(self, labels):
if self.return_type is 'lists':
return labels
elif self.return_type is 'numpy':
_np = _import_numpy()
return _np.array(labels)
else:
raise MNISTException("unknown return_type '{}'".format(self.return_type))
def process_images_to_numpy(self,images):
_np = _import_numpy()
images_np = _np.array(images)
if self.mode == 'vanilla':
pass # no processing, return them vanilla
elif self.mode == 'randomly_binarized':
r = _np.random.random(images_np.shape)
images_np = (r <= ( images_np / 255)).astype('int') # bool to 0/1
elif self.mode == 'rounded_binarized':
images_np = ((images_np / 255) > 0.5).astype('int') # bool to 0/1
else:
raise MNISTException("unknown mode '{}'".format(self.mode))
return images_np
def process_images_to_lists(self,images):
if self.mode == 'vanilla':
pass # no processing, return them vanilla
elif self.mode == 'randomly_binarized':
for i in range(len(images)):
for j in range(len(images[i])):
pixel = images[i][j]
images[i][j] = int(random.random() <= pixel/255) # bool to 0/1
elif self.mode == 'rounded_binarized':
for i in range(len(images)):
for j in range(len(images[i])):
pixel = images[i][j]
images[i][j] = int(pixel/255 > 0.5) # bool to 0/1
else:
raise MNISTException("unknown mode '{}'".format(self.mode))
return images
@classmethod
def load(cls, path_img, path_lbl):
with open(path_lbl, 'rb') as file:
magic, size = struct.unpack(">II", file.read(8))
if magic != 2049:
raise ValueError('Magic number mismatch, expected 2049,'
'got {}'.format(magic))
labels = array("B", file.read())
with open(path_img, 'rb') as file:
magic, size, rows, cols = struct.unpack(">IIII", file.read(16))
if magic != 2051:
raise ValueError('Magic number mismatch, expected 2051,'
'got {}'.format(magic))
image_data = array("B", file.read())
images = []
for i in range(size):
images.append([0] * rows * cols)
for i in range(size):
images[i][:] = image_data[i * rows * cols:(i + 1) * rows * cols]
return images, labels
@classmethod
def display(cls, img, width=28, threshold=200):
render = ''
for i in range(len(img)):
if i % width == 0:
render += '\n'
if img[i] > threshold:
render += '@'
else:
render += '.'
return render
class normalizer(object):
"""Takes a matrix where "one row = one sample" and normalizes entries
based on maximum-minimum value range over all entries.
Returns normalized (all entries in the range (-1,1)) matrix."""
def __init__(self, factor):
self.mean = None
self.std = None
self.factor = factor
self.fitted = False
def fit(self, X):
self.mean = numpy.mean(X)
self.std = numpy.std(X)
self.fitted = True
def fit_transform(self, X):
self.fit(X)
self.fitted = True
return (X - self.mean) / (self.factor * self.std)
def transform(self, X):
if self.fitted:
return (X - self.mean) / (self.factor * self.std)
def get_mnist_data(factor = 1):
mndata = MNIST('/home/bettmensch/Datasets/MNIST')
X_train_init, y_train = mndata.load_training()
X_test_init, y_test = mndata.load_testing()
y_train = numpy.array([y_i for y_i in y_train])
y_test = numpy.array([y_i for y_i in y_test])
norm = normalizer(factor)
X_train_mat = numpy.matrix([x_sample for x_sample in X_train_init])
X_train = norm.fit_transform(X_train_mat)
X_test_mat = numpy.matrix([x_sample for x_sample in X_test_init])
X_test = norm.transform(X_test_mat)
return X_train, y_train, X_test, y_test