open model · neuromorphic

Versalit SNN-OCR

OCR with a spiking neural network instead of a CNN: neurons that accumulate charge and fire, like the brain's — written by hand in pure PyTorch, with no SNN library.

Apache-2.0spiking network (SNN)94.7% accuracyAPI included
The SNN reading the letter K: the character on the left; in the middle, the first layer's spikes at each timestep; on the right, the potential of the most likely classes rising to the answer.

What it does

You pass in an image of a line of printed text and get the text back. The whole OCR is in the repository — binarization, letter segmentation, classification and word assembly — and each character is read by a spiking neural network.

It recognizes digits and capital letters (0–9 and A–Z). On the repository's example, hello.png comes out as HELLO2028, with an average confidence of 0.86.

How a spiking network works

In a regular network, each neuron outputs a number on every pass. In an SNN, the neuron accumulates charge over time and only fires a spike when it crosses a threshold — then resets to zero. This is the LIF model (Leaky Integrate-and-Fire), the same principle behind neuromorphic chips, which only spend energy when there is a spike.

The character enters as a constant current for 12 timesteps. Two convolutions and a dense layer fire spikes; the last layer never fires, it only accumulates potential, and the chosen letter is the one with the highest mean potential. Since a spike is a step with no derivative, training uses a surrogate gradient (the derivative of arctan).

image 28×28 ── constant current, 12 timesteps ──>
  Conv 3×3 (16) + LIF → MaxPool
  Conv 3×3 (32) + LIF → MaxPool
  Dense (256) + LIF
  Dense (36), accumulates potential only → mean over time → character

Metrics

MetricValue
Overall accuracy94.7%
Hardest classesO 85% · 0 87% · B 89% · S 90%
Parameters415,716

How to read it: measured on 3,600 freshly generated synthetic characters (100 per class) the model never saw. The most common mistakes are the classic ones for OCR without context: letter O and digit 0, I and 1, S and 5.

How to run it

git clone https://huggingface.co/delvek/versalit-snn-ocr
cd versalit-snn-ocr
pip install -r requirements.txt
python -m src.infer demo/hello.png        # → 'HELLO2028' (conf=0.86)
python -m uvicorn src.api:app --port 8000  # API: POST /ocr

Train it again

python -m src.train --epochs 5            # on-the-fly synthetic data
python -m src.train --emnist --epochs 10  # or the public EMNIST
python -m src.visualize demo/hello.png    # animates each layer's spikes

Honest limitations

  • Only 0–9 and capital A–Z — no lowercase, no accents.
  • Segmentation expects printed text on one line with separated letters. Handwriting or touching letters will not work.
  • Trained on synthetic characters: for real photos (shadows, perspective, low resolution), fine-tune on your own images.
  • No language model: it will not fix 0 into O from the context of the word.