diff --git a/examples/mnist/main.cpp b/examples/mnist/main.cpp index b78ac460c..6ef3dd253 100644 --- a/examples/mnist/main.cpp +++ b/examples/mnist/main.cpp @@ -198,6 +198,41 @@ int mnist_eval( return prediction; } +#ifdef __cplusplus +extern "C" { +#endif + +int wasm_eval(uint8_t *digitPtr) +{ + mnist_model model; + if (!mnist_model_load("models/mnist/ggml-model-f32.bin", model)) { + fprintf(stderr, "error loading model\n"); + return -1; + } + std::vector digit(digitPtr, digitPtr + 784); + int result = mnist_eval(model, 1, digit); + ggml_free(model.ctx); + return result; +} + +int wasm_random_digit(char *digitPtr) +{ + auto fin = std::ifstream("models/mnist/t10k-images.idx3-ubyte", std::ios::binary); + if (!fin) { + fprintf(stderr, "failed to open digits file\n"); + return 0; + } + srand(time(NULL)); + // Seek to a random digit: 16-byte header + 28*28 * (random 0 - 10000) + fin.seekg(16 + 784 * (rand() % 10000)); + fin.read(digitPtr, 784); + return 1; +} + +#ifdef __cplusplus +} +#endif + int main(int argc, char ** argv) { ggml_time_init(); diff --git a/examples/mnist/web/index.html b/examples/mnist/web/index.html new file mode 100644 index 000000000..d62beadc5 --- /dev/null +++ b/examples/mnist/web/index.html @@ -0,0 +1,126 @@ + + + + + + MNIST with GGML + + + +

MNIST digit recognizer with GGML

+

Draw a single digit on the canvas below:

+ + Your browser does not support the HTML canvas tag. + +
+ + +
+
+

+
+ + +