Instructions to use Reza2kn/Bina-0.4-Microscopy-LiteRT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LiteRT
How to use Reza2kn/Bina-0.4-Microscopy-LiteRT with LiteRT:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
Bina 0.4 Microscopy · LiteRT (TensorFlow Lite)
Bina 0.4 Microscopy (9.55M parameters, Persian page OCR) for LiteRT / TensorFlow Lite: Android, iOS, Linux boards and anything else with the LiteRT interpreter. The full page pipeline gives the same results as the main repo.
| Runtime | 193 crops CER | dev50 handwriting CER | s/page (dev50) |
|---|---|---|---|
| main repo, ONNX Runtime FP32 | 19.09 | 14.46 | 0.55 |
| this repo, LiteRT FP32 (XNNPack) | 19.09 | 14.46 | 0.47 |
Full handwritten pages on an x86 desktop CPU, 4 threads per detector branch; the two branches (main and print detector) run in parallel, each with its own interpreters. No-space CER %, lower is better. --single reads with the main detector only (about twice as fast on one core; weaker on tables).
A dynamic-range INT8 reader was tried and not shipped: it cost 1.1 CER on the 193 crops for a 9% speed-up.
Files
| File | Input → output | Size |
|---|---|---|
reader.tflite |
signatures w256, w512, w1024, w2048, w4096: args_0 (1,1,64,Wb) float ink map (ink = 1), zero-padded to the bucket width, and args_1 (1,) int32 true width → log-probabilities (1, Wb/4, 1625). The width mask makes the padding inert, so results equal the unpadded line. Weights are shared between signatures. |
33 MB |
det_main.tflite, det_print.tflite |
NHWC page (1,H,W,3) in [−1, 1], H and W multiples of 32, any size (resize the input tensor) → line probability map (1,H/2,W/2,1) | 3.3 MB each |
det_main.fp16.tflite, det_print.fp16.tflite |
the same detectors with FP16 weights, for GPU delegates | 1.7 MB each |
microscopy_litert.py |
the reference page pipeline on the LiteRT interpreter (port this to Kotlin/Swift) | |
microscopy.py, bina_rtl.py, vocab.json |
shared crop, ordering and right-to-left helpers, and the vocabulary | |
recipe/ |
how these files were made (convert_litert.py for the reader, keras_det.py rebuilds the detectors in Keras with dynamic page size) |
Run
pip install -r requirements.txt
python microscopy_litert.py examples/demo.png page.jpg letter.pdf --out results [--single] [--threads 4]
The pipeline, as an app would implement it:
- Scale the page so the long side is at most 1024 px and both sides are multiples of 32 (area interpolation), map RGB to [−1, 1], run both detectors.
- Threshold the maps (main 0.3, print 0.2), take 8-connected components with at least 4 pixels, fit a minimum-area rectangle to each, and expand it into a line box (height × 1.6 for main, × 1.9 for print, plus 0.6 line heights of width).
- Order the boxes right to left: split into columns at gaps wider than 0.4 line heights, then rows.
- Crop each line with 20% vertical and 30% horizontal margins (in line heights), scale to 64 px high, invert to ink = 1 (light-on-dark lines are read as they are), read with the reader, greedy CTC decode, and put the right-to-left text in logical order (
bina_rtl.flip). - Drop lines with confidence below 0.5, tiny boxes and duplicate reads, and keep the detector whose confident text is larger.
--single runs only the main detector: as good on handwriting, prose, forms and screenshots, weaker on tables. On a phone with few big cores, use --single for pages without tables.
Notes
- Lines wider than 4096 px at 64 px height (more than about 250 characters) are cut at 4096 px.
- The reader was converted with litert-torch (static width buckets, because its dynamic-shape export froze the time axis). The detectors were rebuilt in Keras with the PyTorch weights (max difference 1e-6) to keep the page size dynamic. Nearest-neighbour upsampling is written as stride-k transposed convolutions with identity kernels, which is exactly equivalent and keeps every shape known to the TFLite converter.
- Tested with the Python LiteRT interpreter on Linux x86 (XNNPack). Android and iOS were not tested on devices.
License
CC BY-NC 4.0, as the main model.
- Downloads last month
- 18