-
Notifications
You must be signed in to change notification settings - Fork 17
/
testing_nn.cpp
305 lines (246 loc) · 7.52 KB
/
testing_nn.cpp
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Software: Testing Artificial Neural Network for MNIST database
// Author: Hy Truong Son
// Major: BSc. Computer Science
// Class: 2013 - 2016
// Institution: Eotvos Lorand University
// Email: sonpascal93@gmail.com
// Website: http://people.inf.elte.hu/hytruongson/
// Copyright 2015 (c). All rights reserved.
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <set>
#include <iterator>
#include <algorithm>
using namespace std;
// Testing image file name
const string testing_image_fn = "mnist/t10k-images.idx3-ubyte";
// Testing label file name
const string testing_label_fn = "mnist/t10k-labels.idx1-ubyte";
// Weights file name
const string model_fn = "model-neural-network.dat";
// Report file name
const string report_fn = "testing-report.dat";
// Number of testing samples
const int nTesting = 10000;
// Image size in MNIST database
const int width = 28;
const int height = 28;
// n1 = Number of input neurons
// n2 = Number of hidden neurons
// n3 = Number of output neurons
const int n1 = width * height; // = 784, without bias neuron
const int n2 = 128;
const int n3 = 10; // Ten classes: 0 - 9
// From layer 1 to layer 2. Or: Input layer - Hidden layer
double *w1[n1 + 1], *out1;
// From layer 2 to layer 3. Or; Hidden layer - Output layer
double *w2[n2 + 1], *in2, *out2;
// Layer 3 - Output layer
double *in3, *out3;
double expected[n3 + 1];
// Image. In MNIST: 28x28 gray scale images.
int d[width + 1][height + 1];
// File stream to read data (image, label) and write down a report
ifstream image;
ifstream label;
ofstream report;
// +--------------------+
// | About the software |
// +--------------------+
void about() {
// Details
cout << "*************************************************" << endl;
cout << "*** Testing Neural Network for MNIST database ***" << endl;
cout << "*************************************************" << endl;
cout << endl;
cout << "No. input neurons: " << n1 << endl;
cout << "No. hidden neurons: " << n2 << endl;
cout << "No. output neurons: " << n3 << endl;
cout << endl;
cout << "Testing image data: " << testing_image_fn << endl;
cout << "Testing label data: " << testing_label_fn << endl;
cout << "No. testing sample: " << nTesting << endl << endl;
}
// +-----------------------------------+
// | Memory allocation for the network |
// +-----------------------------------+
void init_array() {
// Layer 1 - Layer 2 = Input layer - Hidden layer
for (int i = 1; i <= n1; ++i) {
w1[i] = new double [n2 + 1];
}
out1 = new double [n1 + 1];
// Layer 2 - Layer 3 = Hidden layer - Output layer
for (int i = 1; i <= n2; ++i) {
w2[i] = new double [n3 + 1];
}
in2 = new double [n2 + 1];
out2 = new double [n2 + 1];
// Layer 3 - Output layer
in3 = new double [n3 + 1];
out3 = new double [n3 + 1];
}
// +----------------------------------------+
// | Load model of a trained Neural Network |
// +----------------------------------------+
void load_model(string file_name) {
ifstream file(file_name.c_str(), ios::in);
// Input layer - Hidden layer
for (int i = 1; i <= n1; ++i) {
for (int j = 1; j <= n2; ++j) {
file >> w1[i][j];
}
}
// Hidden layer - Output layer
for (int i = 1; i <= n2; ++i) {
for (int j = 1; j <= n3; ++j) {
file >> w2[i][j];
}
}
file.close();
}
// +------------------+
// | Sigmoid function |
// +------------------+
double sigmoid(double x) {
return 1.0 / (1.0 + exp(-x));
}
// +------------------------------+
// | Forward process - Perceptron |
// +------------------------------+
void perceptron() {
for (int i = 1; i <= n2; ++i) {
in2[i] = 0.0;
}
for (int i = 1; i <= n3; ++i) {
in3[i] = 0.0;
}
for (int i = 1; i <= n1; ++i) {
for (int j = 1; j <= n2; ++j) {
in2[j] += out1[i] * w1[i][j];
}
}
for (int i = 1; i <= n2; ++i) {
out2[i] = sigmoid(in2[i]);
}
for (int i = 1; i <= n2; ++i) {
for (int j = 1; j <= n3; ++j) {
in3[j] += out2[i] * w2[i][j];
}
}
for (int i = 1; i <= n3; ++i) {
out3[i] = sigmoid(in3[i]);
}
}
// +---------------+
// | Norm L2 error |
// +---------------+
double square_error(){
double res = 0.0;
for (int i = 1; i <= n3; ++i) {
res += (out3[i] - expected[i]) * (out3[i] - expected[i]);
}
res *= 0.5;
return res;
}
// +--------------------------------------------------------------+
// | Reading input - gray scale image and the corresponding label |
// +--------------------------------------------------------------+
int input() {
// Reading image
char number;
for (int j = 1; j <= height; ++j) {
for (int i = 1; i <= width; ++i) {
image.read(&number, sizeof(char));
if (number == 0) {
d[i][j] = 0;
} else {
d[i][j] = 1;
}
}
}
for (int j = 1; j <= height; ++j) {
for (int i = 1; i <= width; ++i) {
int pos = i + (j - 1) * width;
out1[pos] = d[i][j];
}
}
// Reading label
label.read(&number, sizeof(char));
for (int i = 1; i <= n3; ++i) {
expected[i] = 0.0;
}
expected[number + 1] = 1.0;
return (int)(number);
}
// +--------------+
// | Main Program |
// +--------------+
int main(int argc, char *argv[]) {
about();
report.open(report_fn.c_str(), ios::out);
image.open(testing_image_fn.c_str(), ios::in | ios::binary); // Binary image file
label.open(testing_label_fn.c_str(), ios::in | ios::binary ); // Binary label file
// Reading file headers
char number;
for (int i = 1; i <= 16; ++i) {
image.read(&number, sizeof(char));
}
for (int i = 1; i <= 8; ++i) {
label.read(&number, sizeof(char));
}
// Neural Network Initialization
init_array(); // Memory allocation
load_model(model_fn); // Load model (weight matrices) of a trained Neural Network
int nCorrect = 0;
for (int sample = 1; sample <= nTesting; ++sample) {
cout << "Sample " << sample << endl;
// Getting (image, label)
int label = input();
// Classification - Perceptron procedure
perceptron();
// Prediction
int predict = 1;
for (int i = 2; i <= n3; ++i) {
if (out3[i] > out3[predict]) {
predict = i;
}
}
--predict;
// Write down the classification result and the squared error
double error = square_error();
printf("Error: %0.6lf\n", error);
if (label == predict) {
++nCorrect;
cout << "Classification: YES. Label = " << label << ". Predict = " << predict << endl << endl;
report << "Sample " << sample << ": YES. Label = " << label << ". Predict = " << predict << ". Error = " << error << endl;
} else {
cout << "Classification: NO. Label = " << label << ". Predict = " << predict << endl;
cout << "Image:" << endl;
for (int j = 1; j <= height; ++j) {
for (int i = 1; i <= width; ++i) {
cout << d[i][j];
}
cout << endl;
}
cout << endl;
report << "Sample " << sample << ": NO. Label = " << label << ". Predict = " << predict << ". Error = " << error << endl;
}
}
// Summary
double accuracy = (double)(nCorrect) / nTesting * 100.0;
cout << "Number of correct samples: " << nCorrect << " / " << nTesting << endl;
printf("Accuracy: %0.2lf\n", accuracy);
report << "Number of correct samples: " << nCorrect << " / " << nTesting << endl;
report << "Accuracy: " << accuracy << endl;
report.close();
image.close();
label.close();
return 0;
}