-
Notifications
You must be signed in to change notification settings - Fork 75
/
main.cc
86 lines (75 loc) · 2.1 KB
/
main.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
#include <algorithm>
#include <array>
#include <fstream>
#include <iostream>
#include <vector>
#include "mnist_simple.h"
int main(int argc, char** argv) {
struct ImageFileHead {
int32_t magic;
uint32_t count;
uint32_t rows;
uint32_t cols;
};
struct LabelFileHead {
uint32_t magic;
uint32_t count;
};
if (argc != 3) {
std::cerr << "Usage: " << argv[0]
<< " [MNIST image-idx3 file] [MNIST label-idx3 file]\n";
return 1;
}
std::ifstream fs(argv[1], std::ios::binary);
struct ImageFileHead fh {
0, 0, 0, 0
};
struct LabelFileHead lh {
0, 0
};
fs.read(reinterpret_cast<char*>(&fh), sizeof(fh));
auto to_le = [](uint32_t& x) {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
x = __builtin_bswap32(x);
#endif
};
to_le(fh.count);
to_le(fh.rows);
to_le(fh.cols);
if (fh.rows != 28 || fh.cols != 28) {
std::cerr << "Invalid data file (rows:" << fh.rows << " cols: " << fh.cols
<< ")\n";
return 1;
}
std::vector<std::array<char, 28 * 28>> imgs(fh.count);
for (unsigned i = 0; i < fh.count; ++i) {
fs.read(imgs[i].data(), imgs[i].size());
if (fs.fail()) {
std::cerr << "Failed to read image file\n";
}
}
std::ifstream f(argv[2], std::ios::binary);
f.read(reinterpret_cast<char*>(&lh), sizeof(lh));
to_le(lh.count);
if (lh.count != fh.count) {
std::cerr << "data mismatch\n";
return 1;
}
std::vector<char> labels(lh.count);
f.read(labels.data(), labels.size());
int correct = 0;
int nr_tests = lh.count;
mnist_simple_init();
for (int i = 0; i < nr_tests; ++i) {
std::array<float, 28 * 28> input;
std::array<float, 10> output;
std::transform(imgs[i].begin(), imgs[i].end(), input.begin(),
[](char x) { return ((unsigned char)x) / 255.0F; });
mnist_simple(input.data(), output.data());
int pred = std::max_element(output.begin(), output.end()) - output.begin();
correct += (pred == labels[i]);
}
mnist_simple_fini();
std::cout << "Accuracy " << correct << "/" << nr_tests << " ("
<< correct * 100.0 / nr_tests << "%) \n";
}