forked from pytorch/glow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
272 lines (231 loc) · 8.71 KB
/
main.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* Copyright (c) Glow Contributors. See CONTRIBUTORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <inttypes.h>
#include <png.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <string>
#include <vector>
#include "lenet_mnist.h"
/// This is an example demonstrating how to use auto-generated bundles and
/// create standalone executables that can perform neural network computations.
/// This example loads and runs the compiled lenet_mnist network model.
/// This example is using the static bundle API.
#define DEFAULT_HEIGHT 28
#define DEFAULT_WIDTH 28
#define OUTPUT_LEN 10
//===----------------------------------------------------------------------===//
// Image processing helpers
//===----------------------------------------------------------------------===//
std::vector<std::string> inputImageFilenames;
/// \returns the index of the element at x,y,z,w.
size_t getXYZW(const size_t *dims, size_t x, size_t y, size_t z, size_t w) {
return (x * dims[1] * dims[2] * dims[3]) + (y * dims[2] * dims[3]) +
(z * dims[3]) + w;
}
/// \returns the index of the element at x,y,z.
size_t getXYZ(const size_t *dims, size_t x, size_t y, size_t z) {
return (x * dims[1] * dims[2]) + (y * dims[2]) + z;
}
/// Reads a PNG image from a file into a newly allocated memory block \p imageT
/// representing a WxHxNxC tensor and returns it. The client is responsible for
/// freeing the memory block.
bool readPngImage(const char *filename, std::pair<float, float> range,
float *&imageT, size_t *imageDims) {
unsigned char header[8];
// open file and test for it being a png.
FILE *fp = fopen(filename, "rb");
// Can't open the file.
if (!fp) {
return true;
}
// Validate signature.
size_t fread_ret = fread(header, 1, 8, fp);
if (fread_ret != 8) {
return true;
}
if (png_sig_cmp(header, 0, 8)) {
return true;
}
// Initialize stuff.
png_structp png_ptr =
png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!png_ptr) {
return true;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
return true;
}
if (setjmp(png_jmpbuf(png_ptr))) {
return true;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
size_t width = png_get_image_width(png_ptr, info_ptr);
size_t height = png_get_image_height(png_ptr, info_ptr);
int color_type = png_get_color_type(png_ptr, info_ptr);
int bit_depth = png_get_bit_depth(png_ptr, info_ptr);
const bool isGray = color_type == PNG_COLOR_TYPE_GRAY;
const size_t numChannels = 1;
(void)bit_depth;
assert(bit_depth == 8 && "Invalid image");
assert(isGray && "Invalid image");
(void)isGray;
bool hasAlpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
int number_of_passes = png_set_interlace_handling(png_ptr);
(void)number_of_passes;
assert(number_of_passes == 1 && "Invalid image");
png_read_update_info(png_ptr, info_ptr);
// Error during image read.
if (setjmp(png_jmpbuf(png_ptr))) {
return true;
}
auto *row_pointers = (png_bytep *)malloc(sizeof(png_bytep) * height);
for (size_t y = 0; y < height; y++) {
row_pointers[y] = (png_byte *)malloc(png_get_rowbytes(png_ptr, info_ptr));
}
png_read_image(png_ptr, row_pointers);
png_read_end(png_ptr, info_ptr);
imageDims[0] = width;
imageDims[1] = height;
imageDims[2] = numChannels;
imageT = static_cast<float *>(
calloc(1, width * height * numChannels * sizeof(float)));
float scale = ((range.second - range.first) / 255.0);
float bias = range.first;
for (size_t row_n = 0; row_n < height; row_n++) {
png_byte *row = row_pointers[row_n];
for (size_t col_n = 0; col_n < width; col_n++) {
png_byte *ptr =
&(row[col_n * (hasAlpha ? (numChannels + 1) : numChannels)]);
imageT[getXYZ(imageDims, row_n, col_n, 0)] = float(ptr[0]) * scale + bias;
}
}
for (size_t y = 0; y < height; y++) {
free(row_pointers[y]);
}
free(row_pointers);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(fp);
printf("Loaded image: %s\n", filename);
return false;
}
/// Loads and normalizes all PNGs into a tensor memory block \p resultT in the
/// NCHW 1x28x28 format.
static void loadImagesAndPreprocess(const std::vector<std::string> &filenames,
float *&resultT, size_t *resultDims) {
assert(filenames.size() > 0 &&
"There must be at least one filename in filenames");
std::pair<float, float> range = std::make_pair(0., 1.0);
unsigned numImages = filenames.size();
// N x C x H x W
resultDims[0] = numImages;
resultDims[1] = 1;
resultDims[2] = DEFAULT_HEIGHT;
resultDims[3] = DEFAULT_WIDTH;
size_t resultSizeInBytes =
numImages * DEFAULT_HEIGHT * DEFAULT_WIDTH * sizeof(float);
resultT = static_cast<float *>(malloc(resultSizeInBytes));
// We iterate over all the png files, reading them all into our result tensor
// for processing
for (unsigned n = 0; n < numImages; n++) {
float *imageT{nullptr};
size_t dims[3];
bool loadSuccess = !readPngImage(filenames[n].c_str(), range, imageT, dims);
assert(loadSuccess && "Error reading input image.");
(void)loadSuccess;
assert((dims[0] == DEFAULT_HEIGHT && dims[1] == DEFAULT_WIDTH) &&
"All images must have the same Height and Width");
// Convert to BGR, as this is what NN is expecting.
for (unsigned y = 0; y < dims[1]; y++) {
for (unsigned x = 0; x < dims[0]; x++) {
resultT[getXYZW(resultDims, n, 0, x, y)] =
imageT[getXYZ(dims, x, y, 0)];
}
}
}
printf("Loaded images size in bytes is: %lu\n", resultSizeInBytes);
}
/// Parse images file names into a vector.
void parseCommandLineOptions(int argc, char **argv) {
int arg = 1;
while (arg < argc) {
inputImageFilenames.push_back(argv[arg++]);
}
}
//===----------------------------------------------------------------------===//
// Wrapper code for executing a bundle
//===----------------------------------------------------------------------===//
/// Statically allocate memory for constant weights (model weights) and
/// initialize.
GLOW_MEM_ALIGN(LENET_MNIST_MEM_ALIGN)
uint8_t constantWeight[LENET_MNIST_CONSTANT_MEM_SIZE] = {
#include "lenet_mnist.weights.txt"
};
/// Statically allocate memory for mutable weights (model input/output data).
GLOW_MEM_ALIGN(LENET_MNIST_MEM_ALIGN)
uint8_t mutableWeight[LENET_MNIST_MUTABLE_MEM_SIZE];
/// Statically allocate memory for activations (model intermediate results).
GLOW_MEM_ALIGN(LENET_MNIST_MEM_ALIGN)
uint8_t activations[LENET_MNIST_ACTIVATIONS_MEM_SIZE];
/// Bundle input data absolute address.
uint8_t *inputAddr = GLOW_GET_ADDR(mutableWeight, LENET_MNIST_data);
/// Bundle output data absolute address.
uint8_t *outputAddr = GLOW_GET_ADDR(mutableWeight, LENET_MNIST_softmax);
/// Copy the pre-processed images into the mutable region of the bundle.
static void initInputImages() {
size_t inputDims[4];
float *inputT{nullptr};
loadImagesAndPreprocess(inputImageFilenames, inputT, inputDims);
// Copy image data into the data input variable in the mutableWeightVars area.
size_t imageDataSizeInBytes =
inputDims[0] * inputDims[1] * inputDims[2] * inputDims[3] * sizeof(float);
printf("Copying image data into mutable weight vars: %lu bytes\n",
imageDataSizeInBytes);
memcpy(inputAddr, inputT, imageDataSizeInBytes);
free(inputT);
}
/// Dump the result of the inference by looking at the results vector and
/// finding the index of the max element.
static void printResults() {
int maxIdx = 0;
float maxValue = 0;
float *results = (float *)(outputAddr);
for (int i = 0; i < OUTPUT_LEN; ++i) {
if (results[i] > maxValue) {
maxValue = results[i];
maxIdx = i;
}
}
printf("Result: %u\n", maxIdx);
printf("Confidence: %f\n", maxValue);
}
int main(int argc, char **argv) {
parseCommandLineOptions(argc, argv);
// Initialize input images.
initInputImages();
// Perform the computation.
int errCode = lenet_mnist(constantWeight, mutableWeight, activations);
if (errCode != GLOW_SUCCESS) {
printf("Error running bundle: error code %d\n", errCode);
}
// Print results.
printResults();
}