Skip to content

An OCR neural network coded from scratch using C, using stb_image.h for image handling

Notifications You must be signed in to change notification settings

Flawxd/OCR-NeuralNetwork

Repository files navigation

C-Neural-Engine OCR

A lightweight neural network for optical character recognition (OCR), written in pure C.

Features

  • Single hidden layer neural network with sigmoid activation
  • Mini-batch gradient descent training
  • Binary model serialization for fast loading

Optimizations

  • Flattened weight matrices: 2D weights stored as contiguous 1D arrays in row-major order for better CPU cache utilization.
  • Mini-batch gradient descent: Gradients accumulated across samples before weight updates, improving convergence.
  • Inlined activation functions: static inline eliminates function call overhead.
  • BLAS-ready kernels: Matrix operations abstracted for easy swap to optimized BLAS routines.
  • Zero-copy serialization: Weights written/read as raw binary, no parsing overhead.

Building

make

For debug builds with sanitizers:

make debug

Usage

Training

Place your training images in the letters/ directory:

letters/
  A/
    sample1.png
    sample2.png
  B/
    sample1.png
  ...

Each image must be 32x32 pixels in grayscale. Run:

./ocr-engine

The trained model is saved to model.bin.

Inference

./ocr-engine path/to/image.png

Outputs the predicted letter (A-Z) with confidence score.

About

An OCR neural network coded from scratch using C, using stb_image.h for image handling

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published