-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsps.py
More file actions
271 lines (224 loc) · 9.12 KB
/
Usps.py
File metadata and controls
271 lines (224 loc) · 9.12 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
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
264
265
266
267
268
269
270
271
#!/usr/bin/env python
"""
!!! Not certified fit for any purpose, use at your own risk !!!
Copyright (c) Rex Sutton 2016.
Python interface to USPS data.
The *_patterns variables contain a raster scan
of the 16 by 16 grey level pixel intensities,
which have scaled such that the range is [-1; 1].
The *_labels variables contain a one-of-k encoding,
with values -1 and +1 of the classification,
one +1 per column.
The USPS data is at
http://www.gaussianprocess.org/gpml/data/.
"""
import argparse
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import scipy.io
__pattern_data_type__ = np.float64
__label_data_type__ = np.int16
__class_data_type__ = np.float64
def load_data():
"""Load the USPS training and test patterns from disk.
Returns:
The data matrix.
"""
return scipy.io.loadmat('usps_resampled/usps_resampled.mat')
def extract_pattern_vector(all_patterns, pattern_idx):
"""Copy pattern from array format to a vector.
Args:
all_patterns(matrix): The patterns.
pattern_idx (int): The index of the selected pattern.
Returns:
vector: The pattern
"""
vec = np.empty([256], dtype=__pattern_data_type__)
for i in range(0, 256):
vec[i] = all_patterns[i][pattern_idx]
return vec
def extract_pattern_label(all_labels, pattern_idx):
"""Return the label.
Args:
all_labels(matrix): The patterns.
pattern_idx (int): The index of the selected pattern.
Returns:
int: The pattern label
"""
for i in range(0, 10):
if all_labels[i][pattern_idx] == 1:
return i
def pattern_vector_to_image_matrix(vec):
"""Format the vector as a matrix.
Args:
vec (vec): A vector.
Returns:
A 2d matrix view.
"""
return vec.view().reshape([16, 16])
def show(pattern_vec):
"""Show the pattern as a greyscale image.
Args:
pattern_vec (vector): A pattern vector.
"""
color_norm = matplotlib.colors.Normalize(
vmin=-1.0, vmax=1.0, clip=False)
plt.imshow(pattern_vector_to_image_matrix(pattern_vec), cmap='Greys',
norm=color_norm, interpolation='nearest')
plt.show()
def count_patterns(pos_label, neg_label, max_patterns, all_labels):
"""Count patterns from the input array of labels.
Args:
pos_label (int): Patterns with this label have +ve classification.
neg_label (int): Patterns with this label have -ve classification.
max_patterns (int): The maximum number of patterns to return, applies to both
training and test patterns.
all_labels: A vector of labels to select from.
Returns:
int: The number of patterns.
"""
max_patterns = np.minimum(len(all_labels[0]), max_patterns)
num_patterns = 0
for pat in range(0, len(all_labels[0])):
if num_patterns == max_patterns:
break
is_pos = all_labels[pos_label][pat] == 1
is_neg = all_labels[neg_label][pat] == 1
if is_pos or is_neg:
num_patterns += 1
return num_patterns
def select_patterns(pos_label, neg_label, max_patterns, all_patterns, all_labels):
"""Select patterns from the input array of patterns and labels.
Args:
pos_label (int): Patterns with this label have +ve classification.
neg_label (int): Patterns with this label have -ve classification.
max_patterns (int): The maximum number of patterns to return, applies to both
training and test vectors.
all_patterns: A vector of patterns to select from.
all_labels: A vector of labels to select from.
Returns:
tuple: Elements are, the matrix of patterns,
their classification vector.
"""
num_patterns = count_patterns(pos_label, neg_label, max_patterns, all_labels)
patterns, classifications = np.empty([num_patterns, 256], dtype=__pattern_data_type__),\
np.empty([num_patterns], dtype=__class_data_type__)
idx_write_pattern = 0
for idx_read_pattern in range(0, len(all_labels[0])):
if idx_write_pattern == num_patterns:
break
is_pos = all_labels[pos_label][idx_read_pattern] == 1
is_neg = all_labels[neg_label][idx_read_pattern] == 1
if is_pos:
classifications[idx_write_pattern] = 1.0
if is_neg:
classifications[idx_write_pattern] = -1.0
if is_pos or is_neg:
for i in range(0, 256):
patterns[idx_write_pattern][i] = all_patterns[i][idx_read_pattern]
idx_write_pattern += 1
return patterns, classifications
def load_test_data(pos_label, neg_label, max_patterns):
"""Load selected USPS test patterns from disk.
Args:
pos_label (int): Patterns with this label are recorded with + classification.
neg_label (int): Patterns with this label are recorded with - classification.
max_patterns (int): The maximum number of patterns to return,
applies to both training and test vectors.
Returns:
tuple: Elements are,
the test patterns, their classification vector.
"""
mat = load_data()
return select_patterns(pos_label, neg_label, max_patterns,
mat['test_patterns'], mat['test_labels'])
def load_training_data(pos_label, neg_label, max_patterns):
"""Load selected USPS training patterns from disk.
Args:
pos_label (int): Patterns with this label are recorded with + classification.
neg_label (int): Patterns with this label are recorded with - classification.
max_patterns (int): The maximum number of patterns to return,
applies to both training and test vectors.
Returns:
tuple: Elements are,
the training patterns, their classification vector,
"""
mat = load_data()
return select_patterns(pos_label, neg_label, max_patterns,
mat['train_patterns'], mat['train_labels'])
def load_pattern_data(pos_label, neg_label, max_patterns):
"""Load selected USPS training and test patterns from disk.
Args:
pos_label (int): Patterns with this label are recorded with + classification.
neg_label (int): Patterns with this label are recorded with - classification.
max_patterns (int): The maximum number of patterns to return,
applies to both training and test vectors.
Returns:
tuple: Elements are,
the training patterns, their classification vector,
the test patterns, their classification vector.
"""
mat = load_data()
training_patterns, training_classifications \
= select_patterns(pos_label, neg_label, max_patterns,
mat['train_patterns'], mat['train_labels'])
test_patterns, test_classifications \
= select_patterns(pos_label, neg_label, max_patterns,
mat['test_patterns'], mat['test_labels'])
return training_patterns, training_classifications, test_patterns, test_classifications
def load_test_pattern(pattern_idx):
"""Load selected USPS test pattern and label from disk.
Args:
pattern_idx (int): The index of the selected pattern.
Returns:
tuple: Elements are,
the pattern_vector, the pattern classification.
"""
mat = load_data()
return extract_pattern_vector(mat['test_patterns'], pattern_idx),\
extract_pattern_label(mat['test_labels'], pattern_idx)
def peek(pattern_idx, pos_label, neg_label, max_patterns):
""" Display a pattern from the USPS test patterns using the index w.r.t a pair of digits.
Args:
pattern_idx (int): The index of the selected pattern.
pos_label (int): Patterns with this label are recorded with + classification.
neg_label (int): Patterns with this label are recorded with - classification.
max_patterns (int): The maximum number of patterns to return,
applies to both training and test vectors.
"""
# load patterns
test_patterns, test_classifications \
= load_test_data(pos_label, neg_label, max_patterns)
# print the actual digit a human has classified the selected pattern as
if test_classifications[pattern_idx] > 0:
print "*** labelled digit:", pos_label
else:
print "*** labelled digit:", neg_label
# show the image
print "*** (close the plot window to continue)"
show(test_patterns[pattern_idx])
def peep(pattern_idx):
""" Display a pattern from the USPS test patterns.
Args:
pattern_idx (int): The index of the selected pattern.
"""
# load patterns
pattern, label = load_test_pattern(pattern_idx)
print "*** labelled digit:", label
# show the image
print "*** (close the plot window to continue)"
show(pattern)
def main():
""" The main entry point function.
"""
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--idx",
help="The index of the pattern to peep at.", type=int, default=2)
args = parser.parse_args()
if args.idx >= 0:
peep(args.idx)
else:
parser.print_help()
if __name__ == "__main__":
main()