An MNIST dataset classifier neural network in C++ without any libraries :) (Machine Learning stuff for school)
Find a file
Arija A. 16cc6c9988
Train model to exactly 99% accuracy.
Signed-off-by: Arija A. <ari@ari.lt>
2025-05-22 23:04:53 +03:00
mnist-ext Improve model accuracy and generalisation. 2025-05-18 16:55:28 +03:00
presentations Add presentation. 2025-05-20 21:04:51 +03:00
src Train model to truer accuracy. 2025-05-22 19:54:59 +03:00
tests Improve model accuracy and generalisation. 2025-05-18 16:55:28 +03:00
.clang-format init 2025-05-14 20:46:10 +03:00
.editorconfig init 2025-05-14 20:46:10 +03:00
.gitignore Add MinGW toolchain for cross-comp. 2025-05-15 20:45:23 +03:00
CMakeLists.txt Fix AI comments from CMakeLists. 2025-05-20 22:51:00 +03:00
CONTRIBUTING.md Fix typo in CONTRIBUTING. 2025-05-20 22:21:48 +03:00
CONTRIBUTORS.md Add CONTRIBUTING and CONTRIBUTORS. 2025-05-20 22:20:31 +03:00
LICENSE init 2025-05-14 20:46:10 +03:00
Makefile Update flags. 2025-05-18 18:35:30 +03:00
model-traning-stats-98.3p.txt Add visualisation. 2025-05-19 21:21:00 +03:00
modelis.txt Train model to exactly 99% accuracy. 2025-05-22 23:04:53 +03:00
mokymo_statistika.gif Add visualisation. 2025-05-19 21:21:00 +03:00
ppm.html Make the model more optimised. 2025-05-18 00:22:21 +03:00
README.md Msvc compiler support added 2025-05-20 22:04:10 +03:00
toolchain-mingw64.cmake Add MinGW toolchain for cross-comp. 2025-05-15 20:45:23 +03:00
visualise.py Add visualisation. 2025-05-19 21:21:00 +03:00

MNIST classifier in C++

This is a simple MNIST classifier I wrote for school in C++ from scratch.

Compiling and Running

  1. Download the MNIST dataset from https://archive.org/download/mnist-dataset
  2. Convert all images in MNIST_Dataset/trainingSet/* to the PPM P6 format:
for d in ./*; do
    cd "$d"
    parallel -j 8 'ffmpeg -y -i {} {.}.ppm && rm {}' ::: *.jpg
    cd ..
done

(or something like that, parallel makes it a lot faster)

  1. Extend the MNIST dataset using mnist-ext by cp -r mnist-ext/* MNIST_Dataset/trainingSet
  2. make clean && make && ./mnist-classify
  3. watch the magic happen :)

You may use the pre-built model at modelis.txt with 99% or so accuracy. (sorry for the unoptimised code, I only had time til today's evening)

If you want to classify own images, feel free to use the https://ari.lt/ppm tool to draw your own, ensure:

  1. Width and height are both 28 pixels, press "save canvas" before anything.
  2. Set the brush size to 2
  3. Draw your number :)

... or open the ppm.html file in your browser.

Cross-Compiling to Windows

rm -rf build
mkdir build
cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchain-mingw64.cmake ..
cmake --build .

Compiling on Windows using MSVC

Open the Developer Command Prompt and navigate to the project's root directory, then execute the following commands:

rm -rf build
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . -j4 --config Release