-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist_example.c
152 lines (121 loc) · 3.43 KB
/
mnist_example.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
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
#include <string.h>
#include <time.h>
#ifdef USE_OPENCL
#include "clhelper.h"
#endif
#ifdef USE_CUDA
#include "cudahelper.h"
#endif
#include "mnist.h"
#include "net.h"
#include "layers.h"
#include "log.h"
static idx_t *images = NULL;
static idx_t *labels = NULL;
static void feed_data(net_t *n)
{
for (int b = 0; b < n->batch; ++b)
{
int i = rand();
int j = 0;
for (j = 0; j < 28 * 28; ++j)
n->layer[0]->in.val[b * 28 * 28 + j] = images->data[(i % images->dim[0]) * 28 * 28 + j];
for (j = 0; j < 10; ++j)
LAST_LAYER(n)->extra.val[b * 10 + j] = (labels->data[i % labels->dim[0]] == j);
}
}
static int arg_max(data_val_t *data, int n)
{
int i = 0;
int max = 0;
for (i = 0; i < n; ++i)
if (data[i] > data[max])
max = i;
return max;
}
int main(int argc, char **argv)
{
int right = 0;
float loss = 0;
time_t start = time(NULL);
net_t *n = NULL;
#ifdef USE_OPENCL
cl_init();
#endif
#ifdef USE_CUDA
cublas_init();
#endif
NET_CREATE(n, TRAIN_SGD, 100);
NET_ADD(n, conv_layer(1, 28, 28, 32, 28, 28, 5, 0, 0, FILLER_MSRA, 0.5, 0));
NET_ADD(n, relu_layer(0));
NET_ADD(n, max_pooling_layer(32, 28, 28, 14, 14, 2, 0, 0));
NET_ADD(n, conv_layer(32, 14, 14, 64, 14, 14, 5, 0, 0, FILLER_MSRA, 0.5, 0));
NET_ADD(n, relu_layer(0));
NET_ADD(n, max_pooling_layer(64, 14, 14, 7, 7, 2, 0, 0));
NET_ADD(n, fc_layer(0, 1024, FILLER_MSRA, 0.5, 0));
NET_ADD(n, relu_layer(0));
NET_ADD(n, dropout_layer(0, 0.6));
NET_ADD(n, fc_layer(0, 10, FILLER_MSRA, 0.5, 0));
NET_ADD(n, softmax_layer(0));
NET_ADD(n, cee_layer(0));
NET_FINISH(n);
images = mnist_open(argv[1]);
labels = mnist_open(argv[2]);
net_param_load(n, "params.bin");
for (int i = 0; i < 20000; ++i)
{
net_train(n, feed_data, 0.001);
for (int b = 0; b < n->batch; ++b)
loss += LAST_LAYER(n)->out.val[b];
if (i % 50 == 0)
{
feed_data(n);
net_forward(n);
#ifdef USE_OPENCV
for (int b = 0; b < 10; ++b)
{
int predict = arg_max(&LAST_LAYER(n)->in.val[b * 10], 10);
int truth = arg_max(&LAST_LAYER(n)->extra.val[b * 10], 10);
if (predict != truth)
{
//cvRectangle(&M[0], cvPoint(0, b * 28), cvPoint(27, b * 28 + 27), cvScalar(255, 255, 255, 255), 1, 8, 0);
LOG("%d ", predict);
}
}
LOG("\n");
CV_DATA_SHOW_VAL("input", 100, &n->layer[0]->in, 0, 28, 10 * 28, 28, 10 * 28);
#endif
for (int b = 0; b < n->batch; ++b)
right += (arg_max(&LAST_LAYER(n)->in.val[b * 10], 10) == arg_max(&LAST_LAYER(n)->extra.val[b * 10], 10));
LOG("loss = %f, train accurcy = %f, step = %d\n", loss / n->batch / 50, 1.0 * right / n->batch, i);
LOG("steps/sec = %f\n", 50.0 / (time(NULL) - start));
loss = 0;
right = 0;
start = time(NULL);
net_param_save(n, "params.bin");
}
}
net_param_save(n, "params.bin");
mnist_close(labels);
mnist_close(images);
images = mnist_open(argv[3]);
labels = mnist_open(argv[4]);
for (int i = 0; i < images->dim[0] / n->batch; ++i)
{
feed_data(n);
net_forward(n);
for (int b = 0; b < n->batch; ++b)
right += (arg_max(&LAST_LAYER(n)->in.val[b * 10], 10) == arg_max(&LAST_LAYER(n)->extra.val[b * 10], 10));
}
LOG("accurcy %f\n", 1.0 * right / (images->dim[0] / n->batch * n->batch));
mnist_close(labels);
mnist_close(images);
net_destroy(n);
#ifdef USE_CUDA
cublas_deinit();
#endif
#ifdef USE_OPENCL
cl_deinit();
#endif
return 0;
}