-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcnn_2d.cpp
245 lines (180 loc) · 7.17 KB
/
cnn_2d.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
/**
* @file cnn_2d.cpp
* @author Daniel Nichols
* @version 1.0
* @date 2019-07-08
*
* @copyright Copyright (c) 2019
*/
#include <cstdint>
#include <cstdio>
#include <vector>
#include "magmadnn.h"
using namespace magmadnn;
/* these are used for reading in the MNIST data set -- found at http://yann.lecun.com/exdb/mnist/ */
Tensor<float> *read_mnist_images(const char *file_name, uint32_t &n_images, uint32_t &n_rows, uint32_t &n_cols);
Tensor<float> *read_mnist_labels(const char *file_name, uint32_t &n_labels, uint32_t n_classes);
void print_image(uint32_t image_idx, Tensor<float> *images, Tensor<float> *labels, uint32_t n_rows, uint32_t n_cols);
int main(int argc, char **argv) {
#if defined(MAGMADNN_HAVE_MPI)
MPI_Init(&argc, &argv);
#endif
magmadnn_init();
Tensor<float> *images_host, *labels_host;
uint32_t n_images, n_rows, n_cols, n_labels, n_classes = 10;
memory_t training_memory_type;
/* these functions read-in and return tensors holding the mnist data set
to use them, please change the string to the path to your local copy of the mnist dataset.
it can be downloaded from http://yann.lecun.com/exdb/mnist */
images_host = read_mnist_images("./train-images-idx3-ubyte", n_images, n_rows, n_cols);
labels_host = read_mnist_labels("./train-labels-idx1-ubyte", n_labels, n_classes);
if (images_host == NULL || labels_host == NULL) {
return 1;
}
if (argc == 2) {
print_image(std::atoi(argv[1]), images_host, labels_host, n_rows, n_cols);
}
model::nn_params_t params;
params.batch_size = 128;
params.n_epochs = 20;
params.learning_rate = 0.05;
#if defined(MAGMADNN_HAVE_CUDA)
training_memory_type = DEVICE;
// training_memory_type = HOST;
#else
training_memory_type = HOST;
#endif
auto x_batch = op::var<float>("x_batch", {params.batch_size, 1, n_rows, n_cols}, {NONE, {}}, training_memory_type);
auto input = layer::input(x_batch);
auto conv2d1 = layer::conv2d(input->out(), {5, 5}, 32, {0, 0}, {1, 1}, {1, 1}, true, false);
auto act1 = layer::activation(conv2d1->out(), layer::RELU);
auto pool1 = layer::pooling(act1->out(), {2, 2}, {0, 0}, {2, 2}, MAX_POOL);
auto conv2d2 = layer::conv2d(pool1->out(), {5, 5}, 32, {0, 0}, {1, 1}, {1, 1}, true, false);
auto act2 = layer::activation(conv2d2->out(), layer::RELU);
auto pool2 = layer::pooling(act2->out(), {2, 2}, {0, 0}, {2, 2}, MAX_POOL);
auto flatten = layer::flatten(pool2->out());
auto fc1 = layer::fullyconnected(flatten->out(), 768, true);
auto act3 = layer::activation(fc1->out(), layer::RELU);
auto fc2 = layer::fullyconnected(act3->out(), 500, true);
auto act4 = layer::activation(fc2->out(), layer::RELU);
auto fc3 = layer::fullyconnected(act4->out(), n_classes, false);
auto act5 = layer::activation(fc3->out(), layer::SOFTMAX);
auto output = layer::output(act5->out());
std::vector<layer::Layer<float> *> layers =
{input,
conv2d1, act1,
pool1,
conv2d2, act2,
pool2,
flatten,
fc1, act3,
fc2, act4,
fc3, act5,
output};
model::NeuralNetwork<float> model(layers, optimizer::CROSS_ENTROPY, optimizer::SGD, params);
model::metric_t metrics;
model.summary();
model.fit(images_host, labels_host, metrics, true);
delete images_host;
delete labels_host;
delete output;
magmadnn_finalize();
#if defined(MAGMADNN_HAVE_MPI)
MPI_Finalize();
#endif
return 0;
}
#define FREAD_CHECK(res, nmemb) \
if ((res) != (nmemb)) { \
fprintf(stderr, "fread fail.\n"); \
return NULL; \
}
inline void endian_swap(uint32_t &val) {
/* taken from https://stackoverflow.com/questions/13001183/how-to-read-little-endian-integers-from-file-in-c */
val = (val >> 24) | ((val << 8) & 0xff0000) | ((val >> 8) & 0xff00) | (val << 24);
}
Tensor<float> *read_mnist_images(const char *file_name, uint32_t &n_images, uint32_t &n_rows, uint32_t &n_cols) {
FILE *file;
unsigned char magic[4];
Tensor<float> *data;
uint8_t val;
file = std::fopen(file_name, "r");
if (file == NULL) {
std::fprintf(stderr, "Could not open %s for reading.\n", file_name);
return NULL;
}
FREAD_CHECK(fread(magic, sizeof(char), 4, file), 4);
if (magic[2] != 0x08 || magic[3] != 0x03) {
std::fprintf(stderr, "Bad file magic.\n");
return NULL;
}
FREAD_CHECK(fread(&n_images, sizeof(uint32_t), 1, file), 1);
endian_swap(n_images);
FREAD_CHECK(fread(&n_rows, sizeof(uint32_t), 1, file), 1);
endian_swap(n_rows);
FREAD_CHECK(fread(&n_cols, sizeof(uint32_t), 1, file), 1);
endian_swap(n_cols);
printf("Preparing to read %u images with size %u x %u ...\n", n_images, n_rows, n_cols);
char bytes[n_rows * n_cols];
/* allocate tensor */
data = new Tensor<float>({n_images, 1, n_rows, n_cols}, {NONE, {}}, HOST);
for (uint32_t i = 0; i < n_images; i++) {
FREAD_CHECK(fread(bytes, sizeof(char), n_rows * n_cols, file), n_rows * n_cols);
for (uint32_t r = 0; r < n_rows; r++) {
for (uint32_t c = 0; c < n_cols; c++) {
val = bytes[r * n_cols + c];
data->set(i * n_rows * n_cols + r * n_cols + c, (val / 128.0f) - 1.0f);
}
}
}
printf("finished reading images.\n");
fclose(file);
return data;
}
Tensor<float> *read_mnist_labels(const char *file_name, uint32_t &n_labels, uint32_t n_classes) {
FILE *file;
unsigned char magic[4];
Tensor<float> *labels;
uint8_t val;
file = std::fopen(file_name, "r");
if (file == NULL) {
std::fprintf(stderr, "Could not open %s for reading.\n", file_name);
return NULL;
}
FREAD_CHECK(fread(magic, sizeof(char), 4, file), 4);
if (magic[2] != 0x08 || magic[3] != 0x01) {
std::fprintf(stderr, "Bad file magic.\n");
return NULL;
}
FREAD_CHECK(fread(&n_labels, sizeof(uint32_t), 1, file), 1);
endian_swap(n_labels);
printf("Preparing to read %u labels with %u classes ...\n", n_labels, n_classes);
/* allocate tensor */
labels = new Tensor<float>({n_labels, n_classes}, {ZERO, {}}, HOST);
printf("finished reading labels.\n");
for (unsigned int i = 0; i < n_labels; i++) {
FREAD_CHECK(fread(&val, sizeof(char), 1, file), 1);
labels->set(i * n_classes + val, 1.0f);
}
fclose(file);
return labels;
}
void print_image(uint32_t image_idx, Tensor<float> *images, Tensor<float> *labels, uint32_t n_rows, uint32_t n_cols) {
uint8_t label = 0;
uint32_t n_classes = labels->get_shape(1);
/* assign the label */
for (uint32_t i = 0; i < n_classes; i++) {
if (std::fabs(labels->get(image_idx * n_classes + i) - 1.0f) <= 1E-8) {
label = i;
break;
}
}
printf("Image[%u] is digit %u:\n", image_idx, label);
for (unsigned int r = 0; r < n_rows; r++) {
for (unsigned int c = 0; c < n_cols; c++) {
printf("%3u ", (uint8_t)((images->get(image_idx * n_rows * n_cols + r * n_cols + c) + 1.0f) * 128.0f));
}
printf("\n");
}
}
#undef FREAD_CHECK