Reza2kn's picture
Core ML packages and page pipeline
8ec2dce verified
|
Raw History Blame Contribute Delete
4.93 kB
---
language:
- fa
license: cc-by-nc-4.0
base_model: Reza2kn/Bina-0.4-Microscopy
base_model_relation: quantized
pipeline_tag: image-to-text
library_name: coreml
tags:
- bina
- ocr
- persian
- farsi
- coreml
- apple-silicon
- ios
- macos
- on-device
---
# Bina 0.4 Microscopy · Core ML (iPhone, iPad, Mac)
[Bina 0.4 Microscopy](https://huggingface.co/Reza2kn/Bina-0.4-Microscopy) (9.55M parameters, Persian page OCR) as Core ML packages for Apple devices, with a reference page pipeline. On an M2 Mac it reads a page more than twice as fast as ONNX Runtime on the same machine, with the same text.
| Runtime on an Apple M2 | Detectors | Reader | s/page | Text vs ONNX FP32 (CER) |
|---|---|---|---:|---:|
| ONNX Runtime (main repo), FP32 | CPU | CPU | 1.18 | — |
| ONNX Runtime (main repo), INT8 reader | CPU | CPU | 0.78 | |
| **Core ML (this repo, default)**: FP16 | **GPU** | **CPU** | **0.49** | **0.000** |
| Core ML, `--fp32` (bit-exact) | GPU | CPU | 0.65 | 0.000 |
| Core ML, INT8 reader | GPU | CPU | 0.47 | 0.039 |
| **Core ML, `--single`**, FP16 | GPU | CPU | **0.24** | 0.000 |
Three pages (handwriting and print, 86 lines), long side 1024 px, both detector branches unless `--single`, Python + coremltools 9 on macOS. No-space CER between the two outputs; 0.000 means identical text.
Per network, on the 193 benchmark crops and three pages (M2):
| File | Best compute unit | Time | Agreement with ONNX |
|---|---|---:|---|
| `reader.fp32.mlpackage` | CPU_ONLY | 6.5 ms/line | identical text |
| `reader.fp16.mlpackage` | CPU_ONLY | 4.8 ms/line | 0.017 CER apart (truth CER 2.20 vs 2.22) |
| `reader.int8.mlpackage` | CPU_ONLY | 4.7 ms/line | 0.10 CER apart |
| `det_main.mlpackage`, `det_print.mlpackage` | CPU_AND_GPU | 53 ms/page | identical masks |
| `det_main.fp16.mlpackage`, `det_print.fp16.mlpackage` | CPU_AND_GPU | 40 ms/page | 99.99% of mask pixels |
The reader has a dynamic width, which Core ML runs on the CPU (with Apple's fast FP16 path); a fixed-width-bucket version that targets the Neural Engine was slower (36 ms/line) and was not shipped. Letting Core ML choose (`ALL`) was slower for every file than the units above, so the pipeline sets them explicitly.
## Files
| File | Input → output |
|---|---|
| `reader.{fp16,fp32,int8}.mlpackage` | `x` (1,1,64,W) float ink map (ink = 1), W a multiple of 8 (8…8192), `widths` (1,) int32 true width → `logprobs` (1, W/4, 1625) |
| `det_main{,.fp16}.mlpackage`, `det_print{,.fp16}.mlpackage` | `x` (1,3,H,W) RGB in [−1, 1], H and W multiples of 32 (32…1024) → `prob` (1,1,H/2,W/2) |
| `microscopy_coreml.py` | the reference page pipeline on Core ML (port this to Swift) |
| `microscopy.py`, `bina_rtl.py`, `vocab.json` | shared crop, ordering and right-to-left helpers, and the vocabulary |
| `recipe/convert_coreml.py` | how the packages were made (coremltools, from the PyTorch weights) |
Minimum deployment target: iOS 16 / macOS 13 (ML Program).
## Run (macOS)
```bash
pip install -r requirements.txt # onnxruntime only for shared helpers; Python 3.9–3.13 (coremltools has no native build for 3.14 yet)
python microscopy_coreml.py examples/demo.png letter.pdf --out results # FP16, both detectors
python microscopy_coreml.py page.jpg --single # fastest, no tables
python microscopy_coreml.py page.jpg --fp32 # bit-exact with ONNX
```
Each page gives `results/<name>.txt` and `results/<name>.json` (lines with boxes and confidences), the same format as the main repo.
## In a Swift app
1. Load `det_main.fp16` and `det_print.fp16` with `MLModelConfiguration.computeUnits = .cpuAndGPU`, and the reader with `.cpuOnly`.
2. 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.
3. 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).
4. Order the boxes right to left: columns at gaps wider than 0.4 line heights, then rows.
5. Crop each line with 20% vertical and 30% horizontal margins, scale to 64 px high, invert to ink = 1, pad the width to a multiple of 8, read, greedy CTC decode, and put the right-to-left text in logical order (`bina_rtl.flip`).
6. Drop lines with confidence below 0.5, tiny boxes and duplicate reads, and keep the detector whose confident text is larger.
Run the two detector branches on two threads: in Swift both really run at once (in Python the gain is small). For pages without tables, use only the main detector (`--single`): as good on handwriting, prose, forms and screenshots, and twice as fast.
Tested on an Apple M2 Mac. iPhone and iPad were not tested on devices.
## License
CC BY-NC 4.0, as the main model.