-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mnist.c
259 lines (213 loc) · 7.18 KB
/
mnist.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
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
/**********************************************************************************/
/* Copyright (c) 2023 Mark Seminatore */
/* All rights reserved. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining a copy */
/* of this software and associated documentation files(the "Software"), to deal */
/* in the Software without restriction, including without limitation the rights */
/* to use, copy, modify, merge, publish, distribute, sublicense, and / or sell */
/* copies of the Software, and to permit persons to whom the Software is */
/* furnished to do so, subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in all */
/* copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/**********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "ann.h"
#ifdef USE_BLAS
# include <cblas.h>
#endif
static int threads = -1;
//----------------------------------
// get options from the command line
//----------------------------------
int getopt(int n, char *args[])
{
int i;
for (i = 1; (i < n) && (args[i][0] == '-'); i++)
{
if (args[i][1] == 't')
{
threads = atoi(args[i + 1]);
i++;
}
}
return i;
}
//------------------------------
//
//------------------------------
void print_data(real *data, int rows, int stride)
{
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < stride; col++)
{
printf("%g, ", *data++);
}
puts("");
}
}
//------------------------------------------
// get/print prediction from one-hot vector
//------------------------------------------
void print_class_pred(real * data)
{
int class = ann_class_prediction(data, 10);
printf("\nClass prediction is: %d\n\n", class + 1);
}
//--------------------------------------
// display a 28x28 image from flat data
//--------------------------------------
void print_ascii_art(real *data, int rows, int cols)
{
char c;
char *pixels = " `. - ':_,^=;><+!rc*/z?sLTv)J7(|Fi{C}fI31tlu[neoZ5Yxjya]2ESwqkP6h9d4VpOGbUAKXHm8RD#$Bg0MNWQ%&@";
puts("\nInput image\n");
for (int row = 0; row < rows; row++)
{
for (int col = 0; col< cols; col++)
{
c = (int)(94.0 * data[row * cols + col]);
putchar(pixels[c]);
}
puts("");
}
}
//--------------------------------------
// print a histogram of tensor values
//--------------------------------------
void class_histogram(PTensor outputs)
{
int pred;
int classes = outputs->cols;
int *histo = alloca(classes * sizeof(int));
int sum = 0;
memset(histo, 0, classes * sizeof(int));
printf("\nClass Histogram\n");
for (int row = 0; row < outputs->rows; row++)
{
pred = ann_class_prediction(&outputs->values[row * classes], classes);
histo[pred]++;
}
for (int i = 0; i < classes; i++)
sum += histo[i];
for (int i = 0; i < classes; i++)
histo[i] = 40 * histo[i] / sum;
for (int i = 0; i < classes; i++)
{
printf("%3d|", i);
for (int j = 0; j < histo[i]; j++)
putchar('*');
puts("");
}
printf(" +");
for (int i = 0; i < 40; i++)
putchar('-');
puts("");
}
//------------------------------
// main program start
//------------------------------
int main(int argc, char *argv[])
{
char *classes[] =
{
"T - shirt / top",
"Trouser",
"Pullover",
"Dress",
"Coat",
"Sandal",
"Shirt",
"Sneaker",
"Bag",
"Ankle boot"
};
int iFirstArg = getopt(argc, argv);
#if defined(USE_BLAS)
#if defined(CBLAS)
cblas_init(CBLAS_DEFAULT_THREADS);
if (threads != -1)
cblas_set_num_threads(threads);
printf( "%s\n", cblas_get_config());
printf(" CPU uArch: %s\n", cblas_get_corename());
printf(" Cores/Threads: %d/%d\n", cblas_get_num_procs(), cblas_get_num_threads());
#else
if (threads != -1)
openblas_set_num_threads(threads);
printf( "%s\n", openblas_get_config());
printf(" CPU uArch: %s\n", openblas_get_corename());
printf(" Cores/Threads: %d/%d\n", openblas_get_num_procs(), openblas_get_num_threads());
#endif
#endif
// make a new network
PNetwork pnet = ann_make_network(OPT_ADAPT, LOSS_CATEGORICAL_CROSS_ENTROPY);
// define our network
ann_add_layer(pnet, 784, LAYER_INPUT, ACTIVATION_NULL);
ann_add_layer(pnet, 128, LAYER_HIDDEN, ACTIVATION_SIGMOID);
// ann_add_layer(pnet, 128, LAYER_HIDDEN, ACTIVATION_RELU);
ann_add_layer(pnet, 10, LAYER_OUTPUT, ACTIVATION_SOFTMAX);
real *data = NULL, *test_data = NULL;
int rows, stride, test_rows, test_stride;
char *training_data_file = "fashion-mnist_train.csv";
char* testing_data_file = "fashion-mnist_test.csv";
// load the training data
if (iFirstArg < argc)
training_data_file = argv[iFirstArg];
if (ERR_OK != ann_load_csv(training_data_file, CSV_HAS_HEADER, &data, &rows, &stride))
{
printf("Error: unable to open training file - %s\n", training_data_file);
return ERR_FAIL;
}
// load the test data
if (iFirstArg + 1 < argc)
testing_data_file = argv[iFirstArg + 1];
if (ERR_OK != ann_load_csv(testing_data_file, CSV_HAS_HEADER, &test_data, &test_rows, &test_stride))
{
printf("Error: unable to open training file - %s\n", training_data_file);
return ERR_FAIL;
}
// convert outputs to onehot code
PTensor y_labels = tensor_create_from_array(rows, stride, data);
free(data);
PTensor x_train = tensor_slice_cols(y_labels, 1);
PTensor y_train = tensor_onehot(y_labels, 10);
PTensor y_test_labels = tensor_create_from_array(test_rows, test_stride, test_data);
free(test_data);
PTensor x_test = tensor_slice_cols(y_test_labels, 1);
PTensor y_test = tensor_onehot(y_test_labels, 10);
// normalize inputs
tensor_mul_scalar(x_train, (real)(1.0 / 255.0));
tensor_mul_scalar(x_test, (real)(1.0 / 255.0));
// set some hyper-parameters
pnet->epochLimit = 5;
pnet->convergence_epsilon = (real)1e-5;
pnet->batchSize = 8;
// train the network
ann_train_network(pnet, x_train, y_train, x_train->rows);
// evaluate the network against the test data
real acc = ann_evaluate_accuracy(pnet, x_test, y_test);
printf("\nTest accuracy: %g%%\n", acc * 100);
// free memory
ann_free_network(pnet);
tensor_free(y_labels);
tensor_free(x_train);
tensor_free(y_train);
tensor_free(y_test_labels);
tensor_free(x_test);
tensor_free(y_test);
return 0;
}