Bina 0.4 Microscopy · ONNX (browser, Node, any ONNX Runtime)
Bina 0.4 Microscopy (9.55M parameters, Persian page OCR) as plain ONNX graphs, with a complete JavaScript pipeline that runs in browsers on WebGPU or WebAssembly and in Node. Nothing is uploaded: the page is read on the user's device.
Open index.html (it loads the model from this repo), or serve this folder locally.
Files
| File | What | Size |
|---|---|---|
reader.onnx |
CTC line reader, FP32 (exact) | 32.6 MB |
reader.fp16.onnx |
the same reader in FP16, for WebGPU and GPUs | 16.3 MB |
reader.int8.onnx |
INT8 convolutions (static, per-channel), fastest on CPU and WASM | 8.9 MB |
det_main.onnx, det_print.onnx |
the two 0.71M line detectors, FP32 | 2.9 MB each |
vocab.json, vocab_bidi.json |
1,624-symbol vocabulary; bidi classes used to put right-to-left text in logical order | |
microscopy.js |
the whole pipeline (detection, router, reading order, CTC decoding, clean-up), about 400 lines, no dependencies besides ONNX Runtime | |
index.html |
a browser demo: image or PDF (pdf.js) in, text and line boxes out |
reader.fp16.onnx needs a GPU whose WebGPU adapter offers the shader-f16 feature (most recent desktop GPUs and Apple silicon); the demo checks this and otherwise uses reader.onnx. On WASM (CPU), reader.int8.onnx is the fastest.
All graphs have dynamic shapes and no control flow, so they also load in other ONNX runtimes (tract, ORT mobile, DirectML, TensorRT, OpenVINO).
- Reader input:
x(B,1,64,W) float ink map (ink = 1), W a multiple of 8, pluswidths(B,) int64 true widths. Output:logprobs(B, W/4, 1625). - Detector input:
x(1,3,H,W) RGB in [−1, 1], H and W multiples of 32. Output:prob(1,1,H/2,W/2).
Accuracy and speed
Same pipeline as the main repo, scored the same way (no-space CER %, lower is better).
| Runtime | Reader | 193 crops | dev50 handwriting | s/page (dev50) |
|---|---|---|---|---|
| Python, onnxruntime (main repo) | FP32 | 19.09 | 14.46 | 0.55 |
| Python, onnxruntime (main repo) | INT8 | 19.50 | 14.66 | 0.42 |
Node, onnxruntime-node + microscopy.js |
FP32 | 19.06 | 14.42 | 0.99 |
Node, onnxruntime-node + microscopy.js |
FP16 | 19.08 | 14.42 | slower on CPU |
CPU times: 4 threads on an x86 desktop CPU, full handwritten pages. FP16 is meant for WebGPU; on a CPU it is slower than FP32. The INT8 reader quantizes only the convolutions (Add/Mul stay FP32); quantizing the residual adds too costs about 7 CER points.
By default every page is read twice, once with the lines of each detector, and the more confident reading is kept. ocr.run(image, { single: true }) uses only the main detector: about twice as fast and just as good on handwriting, prose, forms and screenshots, but weaker on tables (70.8 vs 58.0 CER on the 193 table crops). The demo pages expose this as a "Page has tables" checkbox.
Serve the page cross-origin isolated (Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: credentialless), so ONNX Runtime Web can use several WebAssembly threads. On an Apple M2 this took one page from 670 ms to 246 ms on WASM (INT8 reader) and from 380 ms to 166 ms on WebGPU (FP16), main detector only. On a Hugging Face static Space, set both under custom_headers in the README.
Use it
Browser:
<script type="module">
import * as ort from "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.22.0/dist/ort.webgpu.min.mjs";
import { Microscopy } from "https://huggingface.co/Reza2kn/Bina-0.4-Microscopy-ONNX/resolve/main/microscopy.js";
ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.22.0/dist/";
const ocr = await Microscopy.create(ort, {
base: "https://huggingface.co/Reza2kn/Bina-0.4-Microscopy-ONNX/resolve/main/",
reader: "reader.fp16.onnx", // or reader.onnx / reader.int8.onnx
executionProviders: ["webgpu", "wasm"],
});
const img = document.querySelector("canvas").getContext("2d").getImageData(0, 0, w, h);
const { text, lines } = await ocr.run(img); // lines: [{box: [x0,y0,x1,y1], text, confidence}]
</script>
Node:
import * as ort from "onnxruntime-node";
import fs from "node:fs";
import { Microscopy } from "./microscopy.js";
const ocr = await Microscopy.create(ort, { load: async (f) => new Uint8Array(fs.readFileSync(f)) });
const result = await ocr.run({ width, height, data }); // RGB or RGBA bytes
License
CC BY-NC 4.0, as the main model.