-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathverify.c
97 lines (81 loc) · 1.94 KB
/
verify.c
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* verify.c * * * * * * * * * * * * * * * * * * * * * * * * * */
/* created by: jordan bonecutter * * * * * * * * * * * * * * * */
/* 29 october 2019 * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "net.h"
#include <stdio.h>
#include <assert.h>
// Read in an integer from the file
int fgeti(FILE* fp)
{
int ret = 0;
ret = ((int)fgetc(fp));
ret<<=8;
ret |= ((int)fgetc(fp));
ret<<=8;
ret |= ((int)fgetc(fp));
ret<<=8;
ret |= ((int)fgetc(fp));
return ret;
}
int main()
{
FILE* images, *labels, *net;
net = fopen("trained.txt", "r");
Net* trained = net_res(net);
fclose(net);
Matrix* in, *out;
int maxi, i, x, j, N, R, C, correct = 0;
double maxo;
in = matrix_new(28*28, 1);
out = matrix_new(10, 1);
images = fopen("mnist/t10k-images.idx3-ubyte", "r");
labels = fopen("mnist/t10k-labels.idx1-ubyte", "r");
if(!images || !labels)
{
printf("Please goto http://yann.lecun.com/exdb/mnist/ & download the dataset\n");
return 1;
}
// Read in the files
fgeti(images);
N = fgeti(images);
R = fgeti(images);
C = fgeti(images);
fgeti(labels);
i = fgeti(labels);
assert(i == N);
fseek(images, 16, SEEK_SET);
fseek(labels, 8, SEEK_SET);
for(i = 0; i < N; i++)
{
// Read in training data
for(x = 0; x < R*C; x++)
{
in->weights[x][0] = ((double)((unsigned char)fgetc(images)))/255.;
}
x = fgetc(labels);
// Feed to the net
net_eval(trained, in, out);
maxo = 0.;
for(j = 0; j < out->rows; j++)
{
if(out->weights[j][0] > maxo)
{
maxi = j;
maxo = out->weights[j][0];
}
}
if(maxi == x)
{
correct++;
}
}
net_del(trained);
matrix_del(in);
matrix_del(out);
fclose(images);
fclose(labels);
printf("Accuracy was %lf\n", (double)correct/(double)N);
return 0;
}