-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhrs-binary-search-directed-test.cc
224 lines (157 loc) · 6.12 KB
/
hrs-binary-search-directed-test.cc
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
//========================================================================
// hrs-binary-search-directed-test.cc
//========================================================================
// Directed test cases for HRSBinarySearch.
#include <iostream>
#include "Image.h"
#include "Vector.h"
#include "HRSBinarySearch.h"
#include "mnist-utils.h"
#include "utst.h"
//------------------------------------------------------------------------
// Inputs
//------------------------------------------------------------------------
#include "digits.dat"
// The data included is as follows:
//
// Digit | Label
// ---------+-------
// digit0 | 5
// digit1 | 3
// digit2 | 2
// digit3 | 1
// digit4 | 1
// digit5 | 6
// digit6 | 5
// digit7 | 8
// digit8 | 9
// digit9 | 7
// digit10 | 0
// digit11 | 7
// digit12 | 4
// digit13 | 0
//
//------------------------------------------------------------------------
// constants
//------------------------------------------------------------------------
const std::string mnsit_dir = "/classes/ece2400/mnist/";
const unsigned int ncols = 28;
const unsigned int nrows = 28;
const unsigned int img_size = nrows * ncols;
//------------------------------------------------------------------------
// test_case_1_classify_zero
//------------------------------------------------------------------------
void test_case_1_classify_zero()
{
std::printf( "\n%s\n", __func__ );
Image test_img( Vector<int>( digit10_image, img_size ), ncols, nrows );
// Train with the first 5 MNIST training images
Vector<Image> img_vec;
std::string image_path = mnsit_dir + "tiny-training-images-10.bin";
std::string label_path = mnsit_dir + "tiny-training-labels-10.bin";
read_labeled_images( image_path, label_path, img_vec, 5 );
// You may uncommment these prints to see the images
//std::cout << "training images" << std::endl;
//for ( int i = 0; i < 5; i++ ){
// img_vec[i].print();
// std::cout << "label:" << img_vec[i].get_label() << std::endl;
//}
//std::cout << "\ntesting image" << std::endl;
// test_img.print();
// Train the classifier
HRSBinarySearch clf;
clf.train( img_vec );
// Check the predicted result
UTST_ASSERT_CHAR_EQ( clf.classify( test_img ).get_label(), digit10_label );
}
//------------------------------------------------------------------------
// test_case_2_classify_seven
//------------------------------------------------------------------------
void test_case_2_classify_seven()
{
std::printf( "\n%s\n", __func__ );
Image test_img = Image( Vector<int>( digit9_image, img_size ),
ncols, nrows );
// Train with the first 20 MNIST training images
Vector<Image> img_vec;
std::string image_path = mnsit_dir + "training-images-small.bin";
std::string label_path = mnsit_dir + "training-labels-small.bin";
read_labeled_images( image_path, label_path, img_vec, 20 );
// You may uncommment these prints to see the images
// std::cout << "training images" << std::endl;
// for ( int i = 0; i < 20; i++ ){
// img_vec[i].print();
// std::cout << "label:" << img_vec[i].get_label() << std::endl;
// }
// std::cout << "\ntesting image" << std::endl;
// test_img.print();
// Train the classifier
HRSBinarySearch clf;
clf.train( img_vec );
// Check the predicted result
UTST_ASSERT_CHAR_EQ( clf.classify( test_img ).get_label(), digit9_label );
}
//------------------------------------------------------------------------
// test_case_3_tiny_accuracy
//------------------------------------------------------------------------
void test_case_3_tiny_accuracy()
{
std::printf( "\n%s\n", __func__ );
const int training_size = 10;
const int testing_size = 10;
// Read the training images
Vector<Image> v_train;
std::string image_path = mnsit_dir + "training-images-tiny.bin";
std::string label_path = mnsit_dir + "training-labels-tiny.bin";
read_labeled_images( image_path, label_path, v_train, training_size );
// Read the testing images
Vector<Image> v_test;
image_path = mnsit_dir + "testing-images-tiny.bin";
label_path = mnsit_dir + "testing-labels-tiny.bin";
read_labeled_images( image_path, label_path, v_test, testing_size );
// Train and classify
HRSBinarySearch clf;
double accuracy = train_and_classify( clf, v_train, v_test );
std::cout << "Accuracy: " << accuracy << std::endl;
// Accuracy should be...
double expected_accuracy = 0.59;
UTST_ASSERT_TRUE( accuracy > expected_accuracy );
}
//------------------------------------------------------------------------
// test_case_4_small_accuracy
//------------------------------------------------------------------------
void test_case_4_small_accuracy()
{
std::printf( "\n%s\n", __func__ );
const int training_size = 600;
const int testing_size = 100;
// Read the training images
Vector<Image> v_train;
std::string image_path = mnsit_dir + "training-images-small.bin";
std::string label_path = mnsit_dir + "training-labels-small.bin";
read_labeled_images( image_path, label_path, v_train, training_size );
// Read the testing images
Vector<Image> v_test;
image_path = mnsit_dir + "testing-images-small.bin";
label_path = mnsit_dir + "testing-labels-small.bin";
read_labeled_images( image_path, label_path, v_test, testing_size );
// Train and classify
HRSBinarySearch clf;
double accuracy = train_and_classify( clf, v_train, v_test );
std::cout << "Accuracy: " << accuracy << std::endl;
// Accuracy should be...
double expected_accuracy = 0.75;
UTST_ASSERT_TRUE( accuracy >= expected_accuracy );
}
//------------------------------------------------------------------------
// main
//------------------------------------------------------------------------
int main( int argc, char** argv )
{
__n = ( argc == 1 ) ? 0 : atoi( argv[1] );
if ( ( __n == 0 ) || ( __n == 1 ) ) test_case_1_classify_zero();
if ( ( __n == 0 ) || ( __n == 2 ) ) test_case_2_classify_seven();
if ( ( __n == 0 ) || ( __n == 3 ) ) test_case_3_tiny_accuracy();
if ( ( __n == 0 ) || ( __n == 4 ) ) test_case_4_small_accuracy();
return 0;
}