InviolusOCR: tiny Turkish line OCR for microcontrollers

A compact convolutional CTC recognizer for one cropped line of printed Turkish text, as full-int8 TensorFlow Lite files in three sizes:

Model Params int8 file Held-out TS-TR scene text Donut documents ESP32-P4, one line
InviolusOCR-Base 560,954 598 KB 77.3% 56.2% 46.5% 1404 ms
InviolusOCR-Small 274,450 298 KB 72.3% 46.0% 33.2% 760 ms
InviolusOCR-Nano 144,330 171 KB 65.4% 34.6% 21.9% about 420 ms

Scores are the share of lines the int8 file reads exactly right.

Keywords: Turkish OCR, Türkçe OCR, tiny OCR, text recognition, TFLite, int8, TinyML, ESP32, microcontroller.

Türkçe özet: Mikrodenetleyiciler için tek satır Türkçe metin okuyan küçük bir OCR modeli, üç boyutta: Base (598 KB), Small (298 KB) ve Nano (171 KB). Base, ayrılmış test satırlarının %77,3'ünü, gerçek sahne metni (TS-TR) kırpıntılarının %56,2'sini ve basılı belge satırlarının %46,5'ini harfi harfine doğru okuyor. Small ve Nano, Base'den kanal budanarak elde edildi. ESP32-P4'te bir satır Small ile 760 ms, Nano ile yaklaşık 420 ms sürüyor. Ticari olmayan kullanım içindir (CC BY-NC-SA 4.0).

Renamed on 2026-11-08. inviolusocr.keras / inviolusocr_int8.tflite are now inviolusocr_base.h5 / inviolusocr_base_int8.tflite (the same model, the int8 file is byte for byte unchanged). The earlier 171 KB tiny files are replaced by Nano, which is better on all three test sets. The old files stay available in this repository's history.

This replaces inviolus/erdem-ocr-tr-tinyml (2026-10-09), whose int8 file lost most of its accuracy.

Files

File What it is
inviolusocr_base_int8.tflite, inviolusocr_small_int8.tflite, inviolusocr_nano_int8.tflite Original PTQ int8 files. Input [1, 32, 160, 1] int8, output [1, 40, 90] int8 (kept unchanged)
inviolusocr_base_qat_int8.tflite, inviolusocr_small_qat_int8.tflite, inviolusocr_nano_qat_int8.tflite Recommended QAT int8 files (2026-11-24). Same input/output shape and sizes (590 / 304 / 173 KB), quantization-aware trained via Conv2D((5,1)) + StandaloneBNQuantizeConfig: +1.6 to +4.9 points on TS-TR and +3.4 to +6.1 points on Donut
inviolusocr_base.h5, inviolusocr_small.h5, inviolusocr_nano.h5 Float Keras models, input (batch, 32, 160, 1), output (batch, 40, 90) softmax
alphabet.json Character set used to decode the output (the same for all three)
benchmark.json Exact-line accuracy and character error rate of all three, float and int8, plus the paired test of Nano against the earlier tiny

The float models are .h5 because Keras 2.15 .keras files saved on Windows do not load on Linux and the other way round; .h5 loads on both. On Windows, load .h5 files from a path without non-ASCII characters.

Model

  • Input: one horizontal text line, grayscale, resized to 160×32, scaled to [-1, 1] (pixel / 127.5 - 1).
  • Architecture: 4 conv + batch-norm + pool blocks, a dense projection, then four residual dilated 1-D convolutions (dilation 1, 2, 4, 1) and a dense softmax. Only ops that quantize cleanly to int8. Small and Nano are the same architecture at 70% and 50% width.
  • How Small and Nano were made: structured channel pruning. Small keeps the 70% of Base's channels with the largest BatchNorm |gamma| and was fine-tuned; Nano is Small pruned again to 50% width and fine-tuned for one GPU hour. A same-budget model trained from scratch scored lower in both cases (paired test, p < 0.01).
  • Output: 40 timesteps × 90 classes. Class 0 is padding, 1..88 are the characters of alphabet.json in order, 89 is the CTC blank.
  • Character set: digits, ASCII letters, ç ğ ı İ ö ş ü Ç Ğ Ö Ş Ü, space and .,:;!?-_/()'%. Up to 24 characters per line.
  • Not included: text detection. The line must already be cropped.

Usage

import json
import numpy as np
import tensorflow as tf

alphabet = json.load(open("alphabet.json", encoding="utf-8"))["alphabet"]
BLANK = len(alphabet) + 1

def preprocess(path):
    image = tf.io.decode_image(tf.io.read_file(path), channels=1, expand_animations=False)
    image = tf.image.resize(image, [32, 160])
    return (tf.cast(image, tf.float32) / 127.5 - 1.0).numpy()

def decode(ids):
    text, previous = [], None
    for i in ids:
        if i != previous and 0 < i < BLANK:
            text.append(alphabet[i - 1])
        previous = i
    return "".join(text)

# int8 TFLite (the file meant for devices); use _base_, _small_ or _nano_
interpreter = tf.lite.Interpreter(model_path="inviolusocr_small_int8.tflite")
interpreter.allocate_tensors()
inp, out = interpreter.get_input_details()[0], interpreter.get_output_details()[0]
scale, zero = inp["quantization"]
x = np.clip(np.round(preprocess("line.png") / scale + zero), -128, 127).astype(np.int8)
interpreter.set_tensor(inp["index"], x[np.newaxis])
interpreter.invoke()
print(decode(interpreter.get_tensor(out["index"])[0].argmax(-1)))

Results

Exact-line accuracy (the whole line correct), all models scored on the same rows: 12,093 held-out lines (synthetic, Zenodo, TS-TR, esengul3, STS-TR), the 2,078-crop official TS-TR test split (real scene text, never trained on), and 2,563 printed-document rows from the donut 200k test list.

Exact-line accuracy of the int8 files on three test sets

"tiny (replaced)" is the earlier 171 KB file that Nano replaces.

Character error rate of the int8 files:

Model Held-out TS-TR Donut
Base 10.0% 18.8% 12.6%
Small 11.8% 23.9% 18.1%
Nano 14.1% 31.4% 24.7%

QAT int8 update (2026-11-24)

Quantization-aware training (scripts/qat_conv2d.py) on the exact same tstr_test (2,078 real scene crops) and donut_test (2,563 documents) sets, keeping all original PTQ files alongside the new QAT files:

Model, int8 PTQ → QAT size TS-TR exact (PTQ → QAT) TS-TR CER Donut exact (PTQ → QAT) Donut CER
Base (inviolusocr_base_qat_int8.tflite) 598 → 590 KB 56.2% → 57.8% (+1.6) 18.8% → 17.6% 46.5% → 49.9% (+3.4) 12.6% → 10.8%
Small (inviolusocr_small_qat_int8.tflite) 298 → 304 KB 46.0% → 49.4% (+3.4) 23.9% → 21.5% 33.2% → 39.3% (+6.1) 18.1% → 14.9%
Nano (inviolusocr_nano_qat_int8.tflite) 171 → 173 KB 34.6% → 39.5% (+4.9) 31.4% → 27.3% 21.9% → 27.4% (+5.5) 24.7% → 19.8%

Nano against the earlier tiny (same size and architecture), paired test on the same rows: held-out 65.4% vs 64.4% (p = 0.001), TS-TR 34.6% vs 32.6% (p = 0.016), donut 21.9% vs 20.1% (p = 0.010). Nano reads more whole lines exactly; its character error rate is the same as tiny's.

int8 accuracy at 171 KB: Nano, the replaced tiny and a from-scratch control

St is the same 171 KB architecture trained from scratch for the same GPU hour as Nano; it lands at tiny's level, so Nano's gain comes from pruning.

int8 file size against held-out accuracy for every measured version

The October LSTM models were larger (665 KB) and lost 26 to 48 points when converted to int8; the convolutional models lose about 1 to 4 points. The full version history is in the GitHub README.

On-device measurement (ESP32-P4)

Measured on 2026-11-08 on one ESP32-P4 board (rev v1.3, 360 MHz, 32 MB PSRAM) with ESP-IDF 5.3.5 and esp-tflite-micro 1.4.2. Mean of 20 runs after 2 warm-up runs, arena in internal SRAM. One inference is one 160x32 line.

Model File Arena used Latency Exact lines, PC Exact lines, device
Base 598 KB 339 KB 1404 ms 56.7% 55.4%
Small 298 KB 235 KB 760 ms 47.5% 45.8%
Nano (measured with the earlier tiny, same architecture) 171 KB 172 KB 419 ms 33.8% 35.0%

Accuracy columns: 240 real TS-TR test photos run on the device and on the PC interpreter with the same int8 inputs. They agree within 1.7 points, but 16 to 25% of the decoded lines differ, on lines the model is unsure about, because the device's int8 kernels round differently. The Base and Small files here are the measured files byte for byte. Only model inference is timed. One board; not measured on an ESP32-S3. Source and raw output: benchmarks/esp32-p4-ocr-check.

Memory budget on ESP32 chips

The arena (the RAM a model needs while it runs) was measured on the ESP32-P4 and is almost the same on other ESP32 chips. Free RAM on the P4 was measured in the benchmark firmware; free RAM on the S3 and C6 are estimates for typical ESP-IDF projects, and the real value depends on the firmware. The C6 has no PSRAM, so everything must fit in its internal SRAM. Not included: camera frame buffers and the application's own memory.

Arena as a share of the chip's internal SRAM:

Model Arena ESP32-S3 (512 KB) ESP32-C6 (512 KB) ESP32-P4 (768 KB)
Nano 168 KB 33% 33% 22%
Small 229 KB 45% 45% 30%
Base 331 KB 65% 65% 43%

Arena as a share of the RAM that is actually free (✅ fits comfortably, ⚠️ fits with little room left, ❌ does not fit):

Model P4, 569 KB free (measured) S3, Wi-Fi off, ~350 KB (est.) S3, Wi-Fi on, ~250 KB (est.) C6, Wi-Fi off, ~400 KB (est.) C6, Wi-Fi + BLE on, ~200 KB (est.)
Nano 30% ✅ 48% ✅ 67% ✅ 42% ✅ 84% ⚠️
Small 40% ✅ 65% ✅ 92% ⚠️ 57% ✅ 115% ❌
Base 58% ✅ 95% ❌* 132% ❌* 83% ⚠️ 166% ❌

* With PSRAM on the S3 the arena can move there and the model runs, more slowly.

Flash is not the limit: even Base takes 15% of a 4 MB flash. On an ESP32-S3 (240 MHz) the latency is estimated at roughly 1.2 to 2.5 times the P4's (Nano about 0.5 to 1.0 s per line), not measured. In short: the P4 runs all three; the S3 runs Nano and Small (Base only with PSRAM); the C6 runs only Nano, and only when Wi-Fi and BLE leave enough RAM.

Roadmap

InviolusOCR roadmap, each step with its gate

The device model will not grow beyond 598 KB; larger models are only used as teachers. Each step must pass its gate before anything new is published here.

  • Quantization-aware training: blocked for now; the TF-MOT toolkit does not quantize the BatchNorm that follows a Conv1D.
  • Feeding the GPU faster for longer training runs.
  • More real scene-text data with a licence that allows it.

Limitations

  • Printed text only; no handwriting, curved text or full pages.
  • Real scene text is still the weak spot (56% exact for Base).
  • Licence plates are not read (2% exact on 250 plate crops in a first, unchecked test).
  • Text detection and line cropping must be done before this model runs.

Training data

About 106,000 training crops; the orkungedik word dataset (no licence) is excluded.

License

Because of the non-commercial (TS-TR, STS-TR) and share-alike (esengul3, donut) terms of the training data, all three models are released under CC BY-NC-SA 4.0: non-commercial use only, credit this model and the datasets above, and share adaptations under the same license.

Training code (Apache License 2.0 with a NOTICE file; earlier commits MIT): ErdemWilkinson/InviolusOCR.

Downloads last month
546
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support