GitHunt
IT

ItsMeSamey/cnn_digit_recognition_zig

A non-allocating Convolutional Neural Network written in zig (CPU based).

Convolutional Neural Network Project

This project is a basic implementation of a convolutional neural network from scratch in Zig, final model has accuracy of 99.24% on MNIST test dataset.

There is a Python port available too with (almost) same features but worse performances.

Usage

  1. Ensure you have Zig installed.
  2. Clone the repository or download the project files using
git clone --depth=1 https://github.com/ItsMeSamey/cnn_digit_recognition_zig.git

Testing accuracy on MNIST dataset

Run the main script to test the network on the MNIST dataset.

zig run main.zig -OReleaseFast

this will provide you with a menu to choose from (r)etrain / (c)ontinue / (u)nsettle / (t)est if a model is already present or will start training if none is found.

Demo Application

  1. Build and run the server using:
zig run server.zig -OReleaseFast
  1. Open the demo.html file in your web browser to launch the interactive demo.

File Structure

  • nn/cnn.zig: Contains the main CNN structure and related functions.

  • nn/layer.zig: Defines the different layer types and their forward/backward logic.

  • nn/functions_activate.zig: Implements various activation functions.

  • nn/functions_loss.zig: Implements different loss functions.

  • nn/functions_reset.zig: Implements different variants to initialize/modify weights and biases.

  • nn/logger.zig: Implements a logging functionality that can be enabled on per file basis. Also includes a function to print test set results.

  • nn/meta.zig: Contains functionality for saving/loading models, creating hashes/offset json for structs etc. and more!

  • read_mnist.zig: Handles reading the MNIST dataset files and includes iterator wrappers for training.

  • image_operations.zig: Contains functions for image augmentation and operations.

  • main.zig: Contains the main entry point for the application, handling test and train modes.

  • server.zig: A web server to handle requests from the demo application.

  • demo.html: The frontend HTML and JavaScript for the web demo.

License

This project is licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 - see the LICENSE file for details.

Languages

Zig94.5%HTML5.5%

Contributors

GNU Affero General Public License v3.0
Created April 7, 2025
Updated May 2, 2025
ItsMeSamey/cnn_digit_recognition_zig | GitHunt