A lightweight neural network for optical character recognition (OCR), written in pure C.
- Single hidden layer neural network with sigmoid activation
- Mini-batch gradient descent training
- Binary model serialization for fast loading
- 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 inlineeliminates 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.
makeFor debug builds with sanitizers:
make debugPlace 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-engineThe trained model is saved to model.bin.
./ocr-engine path/to/image.pngOutputs the predicted letter (A-Z) with confidence score.