Image Classification
timm
ONNX
Safetensors
ocr
script-identification
page-classification
document-ai
multi-label
glam
knowledge-distillation
hf_jobs
Instructions to use davanstrien/page-script-id with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- timm
How to use davanstrien/page-script-id with timm:
import timm model = timm.create_model("hf-hub:davanstrien/page-script-id", pretrained=True) - Notebooks
- Google Colab
- Kaggle
Make card standalone (remove v1/page-script-id-agent references)
Browse files
README.md
CHANGED
|
@@ -52,25 +52,18 @@ tags:
|
|
| 52 |
- hf_jobs
|
| 53 |
---
|
| 54 |
|
| 55 |
-
# Page script ID
|
| 56 |
|
| 57 |
-
A fast, compact image model that looks at a document page and predicts which writing script(s) it contains, without running OCR. It
|
| 58 |
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
|
| 63 |
> **Check the thresholds on your own pages.** The default cut-offs (`0.55` for a script, `0.7038` for `unknown`) were chosen on held-out web-PDF validation pages (95% coverage). On government documents this refuses ~8% and on photographed documents ~28%. Run on a few hundred of your own pages and tune `unknown_threshold` and `script_threshold` in `config.json` before bulk routing.
|
| 64 |
|
| 65 |
**21 scripts:** Arabic (`Arab`), Armenian (`Armn`), Bengali (`Beng`), Cyrillic (`Cyrl`), Devanagari (`Deva`), Ethiopic (`Ethi`), Georgian (`Geor`), Greek (`Grek`), Gujarati (`Gujr`), Han (`Hani`), Hangul (`Hang`), Hebrew (`Hebr`), Japanese (`Jpan`), Kannada (`Knda`), Khmer (`Khmr`), Latin (`Latn`), Malayalam (`Mlym`), Myanmar (`Mymr`), Tamil (`Taml`), Telugu (`Telu`), Thai (`Thai`).
|
| 66 |
|
| 67 |
-
## What changed from v1 (`page-script-id-agent`)
|
| 68 |
-
|
| 69 |
-
1. **Fixed a Japanese multi-label bug:** in v1, 95% of born-digital Japanese training pages were labeled with *both* `Jpan=1` and `Hani=1`, causing v1 to score 47.5% top-1 on MDPBench digital Japanese. Merging Kanji (`Hani`) into `Jpan` when Kana is present raised digital `jp` to **85.0%** (and government `r3_test` `Jpan` to **100%**).
|
| 70 |
-
2. **Dedicated script-only model:** v1 trained 33 heads (12 page-quality/blank heads + 21 scripts) and sliced off 12 at export. v2 trains exclusively on the 21 scripts, freeing 100% of the 2.52M trunk.
|
| 71 |
-
3. **3-family teacher ensemble distillation:** trained 3 diverse heavy teachers (`convnext_small@640`, `hgnetv2_b4@640`, `caformer_s36@512`; ensemble `r2_val = 92.3%`) and distilled their clean-page soft logits into heavily augmented student inputs (strong oblique perspective warps, cylindrical page curl, edge hand occlusion, textured backgrounds, 90Β°/180Β°/270Β° rotations, square-padded crops, and split-page mixed-script composites).
|
| 72 |
-
4. **Fresh real 2026 PDFs + cleanly shaped synthetic data:** added 3,794 real 2026 PDF pages rendered from `commoncrawl/commoncrawl` (`CC-MAIN-2026-47`) and 8,287 purity-filtered pages from `nvidia/OCR-Synthetic-Multilingual-v2` (including 4 properly shaped unseen negative scripts: Lao, Oriya, Meitei, Ol Chiki β holding out Gurmukhi and Sinhala for evaluation).
|
| 73 |
-
|
| 74 |
## Quick start (ONNX Runtime)
|
| 75 |
|
| 76 |
```bash
|
|
@@ -80,13 +73,13 @@ python example.py --pad crop.jpg # for wide text-block crops
|
|
| 80 |
python example.py --mixed page.jpg # 0.50 threshold for mixed-script search
|
| 81 |
```
|
| 82 |
|
| 83 |
-
Preprocessing matters: greyscale, bilinear squash to `640 Γ 640`, float32 values `0..255`. ImageNet normalisation, temperature (`1.15`), and sigmoid are baked into `model.onnx`, which
|
| 84 |
|
| 85 |
-
**Tip for text-block crops:** raw squashing of wide text-block strips (`~3.5:1`)
|
| 86 |
|
| 87 |
## Use with `timm`
|
| 88 |
|
| 89 |
-
`model.safetensors` holds the exact same weights for [timm](https://github.com/huggingface/pytorch-image-models) (`mobilenetv4_conv_small`, 21 outputs in `label_names` order). It
|
| 90 |
|
| 91 |
```python
|
| 92 |
import json
|
|
@@ -130,22 +123,22 @@ for path, p in zip(sys.argv[1:], probs):
|
|
| 130 |
|
| 131 |
All test benchmarks are strictly held out from training (never trained on test documents, MDPBench, OmniDocBench, Sarvam Indic OCR Bench, or held-out unseen scripts Gurmukhi/Sinhala). Top-1 accuracy is unthresholded (before the `unknown` rule). "2 seeds" is the range over two distilled runs of this recipe (`s0` selected by `r2_val`; `s1` shown for spread).
|
| 132 |
|
| 133 |
-
| Benchmark | Label | n |
|
| 134 |
-
|---|---|---:|---:|---:|---:|
|
| 135 |
-
| Web PDFs (`r2_test`), text layer | text layer | 2,056 |
|
| 136 |
-
| Web PDFs (`r2_test`), doc label | doc language | 2,695 |
|
| 137 |
-
| Government docs (`r3_test`, 21 scripts) | text layer / scan doc | 2,140 |
|
| 138 |
-
| β `r3_test` scans subset | scan doc label | 789 |
|
| 139 |
-
| [MDPBench](https://huggingface.co/datasets/Delores-Lin/MDPBench), digital | benchmark lang | 680 |
|
| 140 |
-
| β digital Japanese (`jp`) / Russian (`ru`) | benchmark lang | 40 / 40 |
|
| 141 |
-
| MDPBench, photographed | benchmark lang | 2,040 |
|
| 142 |
-
| β photographed `oblique` / `hold` | benchmark lang | 239 / 286 |
|
| 143 |
-
| Digital rotations `0Β° / 90Β° / 180Β° / 270Β°` | MDPBench digital | 200 |
|
| 144 |
-
| [OmniDocBench](https://huggingface.co/datasets/samiuc/OmniDocBench-parquet), EN / ZH | human page attr | 290 / 612 |
|
| 145 |
-
| β OmniDocBench `en_ch_mixed` exact set @ 0.50 | human page attr | 79 |
|
| 146 |
-
| β OmniDocBench handwritten `note` | human page attr | 116 |
|
| 147 |
-
| [Sarvam Indic OCR Bench](https://huggingface.co/datasets/sarvamai/indic-ocr-bench) crops (square-padded) | GT text script | 975 |
|
| 148 |
-
| Blank / zero-char old-book pages (BHL) | false positive script % | 768 |
|
| 149 |
|
| 150 |
**Decision rule** (`unknown_threshold = 0.7038`, `script_threshold = 0.55`):
|
| 151 |
|
|
@@ -155,12 +148,12 @@ All test benchmarks are strictly held out from training (never trained on test d
|
|
| 155 |
| Government docs (`r3_test`) | 2,140 | 87.9% (**91.2%**) | 4.1% | 8.0% |
|
| 156 |
| MDPBench, photographed | 2,040 | 70.3% (**93.7%**) | 2.0% | 27.7% |
|
| 157 |
|
| 158 |
-
*(Single-script spurious 2+ rate on `r2_test`: **2.1%** at `0.55`; mixed-script exact set on `r2_test` at `0.50` without gate: **46.6%**
|
| 159 |
|
| 160 |
**Unseen scripts:**
|
| 161 |
-
- **Real 2026 Sinhala (`Sinh`) PDFs** (46 full pages from `CC-MAIN-2026-47`, never trained): **73.9%** refused as `unknown` (2 seeds: 73.9β78.3%
|
| 162 |
-
- **Clean synthetic unseen (`Guru`, `Sinh`)** (NVIDIA v2 shard 00000, 160 pages): **51.9%** refused
|
| 163 |
-
- **Exact 18-class leave-out** (`Geor`, `Khmr`, `Telu` removed from training, 855 `r3` pages, undistilled
|
| 164 |
|
| 165 |
## Speed & Parity
|
| 166 |
|
|
@@ -178,13 +171,13 @@ Measured on a cloud `a10g-large` box (12 vCPU, 1Γ A10G), model forward only on
|
|
| 178 |
|
| 179 |
1. **Unseen scripts & look-alikes:** not an unseen-script detector. Unseen scripts that visually resemble trained scripts (e.g. Gurmukhi β Devanagari, Sinhala β Kannada/Telugu) or appear alongside secondary Latin headers are often accepted with moderate-to-high confidence.
|
| 180 |
2. **Weakest known scripts (`r3_test`):** Telugu (`60.0%`, 2 seeds 60β62%) and Gujarati (`72.4%`) remain the weakest classes.
|
| 181 |
-
3. **Photographed Cyrillic (`ru`):** while digital `ru`
|
| 182 |
-
4. **Crops:** raw squashed text-block crops are
|
| 183 |
5. **Not a "no-text" detector:** although blank BHL old-book pages had `0.00%` false positives here, pair with a dedicated no-text classifier (e.g. `small-models-for-glam/page-ocr-flags`) if blank detection is critical.
|
| 184 |
|
| 185 |
## Training & Attribution
|
| 186 |
|
| 187 |
- **Real PDFs:** born-digital & scanned pages from [FinePDFs](https://huggingface.co/datasets/HuggingFaceFW/finepdfs) (**ODC-By**; we thank its authors), 3,794 fresh 2026 PDFs from [Common Crawl](https://huggingface.co/commoncrawl) (`CC-MAIN-2026-47`, page-level Unicode text-layer labels), plus library/archive scans (BHL, US government docs, IDL) labelled by vision-language model agreement. No raw page images are redistributed.
|
| 188 |
- **Synthetic pages:** 8,287 purity-filtered pages from [nvidia/OCR-Synthetic-Multilingual-v2](https://huggingface.co/datasets/nvidia/OCR-Synthetic-Multilingual-v2) (**CC-BY-4.0**; positives across 21 scripts + 4 shaped negative scripts: Lao, Oriya, Meitei, Ol Chiki) and 300 Tibetan synthetic negatives from [WueNLP/Synthdog-Multilingual-100](https://huggingface.co/datasets/WueNLP/Synthdog-Multilingual-100) (**Apache-2.0**).
|
| 189 |
-
- **Teachers & Distillation:** 3-family ensemble of `timm/convnext_small.fb_in22k_ft_in1k_384`, `timm/hgnetv2_b4.ssld_stage2_ft_in1k`, and `timm/caformer_s36.sail_in22k_ft_in1k_384` (all Apache-2.0) distilled into `timm/mobilenetv4_conv_small.e2400_r224_in1k` (Apache-2.0) for 16 epochs with Asymmetric Loss + soft BCE KD (`alpha=0.50, T=1.5`), EMA weights, and `OneCycleLR`.
|
| 190 |
- **Compute:** trained on [Hugging Face Jobs](https://huggingface.co/docs/hub/jobs) (~52 min on `a10g-small` for the distilled student).
|
|
|
|
| 52 |
- hf_jobs
|
| 53 |
---
|
| 54 |
|
| 55 |
+
# Page script ID (MobileNetV4-Small)
|
| 56 |
|
| 57 |
+
A fast, compact image model that looks at a document page and predicts which writing script(s) it contains, without running OCR. It returns an independent calibrated probability for each of 21 scripts, so a page can have one script, multiple scripts (mixed-script pages), or none the model is confident about (`unknown`). Use it to route pages to the right OCR model or language pack, sort large collections by script, or flag pages whose script does not match their metadata.
|
| 58 |
|
| 59 |
+
Built on **MobileNetV4-conv-small** (2.52M parameters, 10.1 MB as ONNX or `timm` weights), distilled from a 3-family teacher ensemble (`convnext_small`, `hgnetv2_b4`, `caformer_s36`) on [Hugging Face Jobs](https://huggingface.co/docs/hub/jobs). Takes a greyscale page squashed to `640 Γ 640` and runs in **33 ms** on 1 CPU thread, **11 ms** on 4 threads, or **1,773 pages/s** on an A10G (`bf16`, batch 64).
|
| 60 |
|
| 61 |
+
Need higher accuracy on heavily photographed documents or scans and can spare ~2.4Γ CPU time? See the sibling model **[`davanstrien/page-script-id-v2-hgnet`](https://huggingface.co/davanstrien/page-script-id-v2-hgnet)** (HGNetV2-B0, 3.99M parameters, 16 MB, 79 ms 1t / 36 ms 4t, 88.0% MDPBench photographed).
|
| 62 |
|
| 63 |
> **Check the thresholds on your own pages.** The default cut-offs (`0.55` for a script, `0.7038` for `unknown`) were chosen on held-out web-PDF validation pages (95% coverage). On government documents this refuses ~8% and on photographed documents ~28%. Run on a few hundred of your own pages and tune `unknown_threshold` and `script_threshold` in `config.json` before bulk routing.
|
| 64 |
|
| 65 |
**21 scripts:** Arabic (`Arab`), Armenian (`Armn`), Bengali (`Beng`), Cyrillic (`Cyrl`), Devanagari (`Deva`), Ethiopic (`Ethi`), Georgian (`Geor`), Greek (`Grek`), Gujarati (`Gujr`), Han (`Hani`), Hangul (`Hang`), Hebrew (`Hebr`), Japanese (`Jpan`), Kannada (`Knda`), Khmer (`Khmr`), Latin (`Latn`), Malayalam (`Mlym`), Myanmar (`Mymr`), Tamil (`Taml`), Telugu (`Telu`), Thai (`Thai`).
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
## Quick start (ONNX Runtime)
|
| 68 |
|
| 69 |
```bash
|
|
|
|
| 73 |
python example.py --mixed page.jpg # 0.50 threshold for mixed-script search
|
| 74 |
```
|
| 75 |
|
| 76 |
+
Preprocessing matters: greyscale, bilinear squash to `640 Γ 640`, float32 values `0..255`. ImageNet normalisation, temperature (`1.15`), and sigmoid are baked into `model.onnx`, which returns the 21 calibrated probabilities directly.
|
| 77 |
|
| 78 |
+
**Tip for text-block crops:** raw squashing of wide text-block strips (`~3.5:1`) causes most crops to return `unknown` (`~30%` top-1), **but padding the crop onto a white square before resizing (`--pad`) raises top-1 on real Indic text-block crops to 86.7%**.
|
| 79 |
|
| 80 |
## Use with `timm`
|
| 81 |
|
| 82 |
+
`model.safetensors` holds the exact same weights for [timm](https://github.com/huggingface/pytorch-image-models) (`mobilenetv4_conv_small`, 21 outputs in `label_names` order). It returns raw logits, so apply `sigmoid(logits / 1.15)` yourself.
|
| 83 |
|
| 84 |
```python
|
| 85 |
import json
|
|
|
|
| 123 |
|
| 124 |
All test benchmarks are strictly held out from training (never trained on test documents, MDPBench, OmniDocBench, Sarvam Indic OCR Bench, or held-out unseen scripts Gurmukhi/Sinhala). Top-1 accuracy is unthresholded (before the `unknown` rule). "2 seeds" is the range over two distilled runs of this recipe (`s0` selected by `r2_val`; `s1` shown for spread).
|
| 125 |
|
| 126 |
+
| Benchmark | Label | n | **This model** | 2 seeds | Sibling `v2-hgnet` |
|
| 127 |
+
|---|---|---:|---:|---:|---:|
|
| 128 |
+
| Web PDFs (`r2_test`), text layer | text layer | 2,056 | **94.0%** | 93.9β94.0% | 94.1% |
|
| 129 |
+
| Web PDFs (`r2_test`), doc label | doc language | 2,695 | **94.3%** | 93.5β94.3% | 94.4% |
|
| 130 |
+
| Government docs (`r3_test`, 21 scripts) | text layer / scan doc | 2,140 | **86.8%** | 86.8β86.9% | 87.6% |
|
| 131 |
+
| β `r3_test` scans subset | scan doc label | 789 | **90.1%** | 89.1β90.1% | 90.9% |
|
| 132 |
+
| [MDPBench](https://huggingface.co/datasets/Delores-Lin/MDPBench), digital | benchmark lang | 680 | **95.6%** | 94.7β95.6% | 96.3% |
|
| 133 |
+
| β digital Japanese (`jp`) / Russian (`ru`) | benchmark lang | 40 / 40 | **85.0% / 90.0%** | 85β88% / 80β90% | 82.5% / 82.5% |
|
| 134 |
+
| MDPBench, photographed | benchmark lang | 2,040 | **84.8%** | 84.8β85.6% | 88.0% |
|
| 135 |
+
| β photographed `oblique` / `hold` | benchmark lang | 239 / 286 | **64.8% / 76.9%** | 65β66% / 77% | 69.9% / 81.5% |
|
| 136 |
+
| Digital rotations `0Β° / 90Β° / 180Β° / 270Β°` | MDPBench digital | 200 | **96.0 / 90.5 / 91.5 / 91.0%** | 95.5β96 / 90β91% | 96.0 / 91.0 / 92.0 / 91.0% |
|
| 137 |
+
| [OmniDocBench](https://huggingface.co/datasets/samiuc/OmniDocBench-parquet), EN / ZH | human page attr | 290 / 612 | **99.7% / 91.7%** | 99.7β100% / 91.5β91.7% | 99.3% / 93.8% |
|
| 138 |
+
| β OmniDocBench `en_ch_mixed` exact set @ 0.50 | human page attr | 79 | **74.7%** | 69.6β74.7% | 59.5% |
|
| 139 |
+
| β OmniDocBench handwritten `note` | human page attr | 116 | **94.0%** | 94.0β97.4% | 91.4% |
|
| 140 |
+
| [Sarvam Indic OCR Bench](https://huggingface.co/datasets/sarvamai/indic-ocr-bench) crops (square-padded) | GT text script | 975 | **86.7%** (`Knda` **80.4%**) | 84.4β86.7% | 86.0% (`Knda` **100%**) |
|
| 141 |
+
| Blank / zero-char old-book pages (BHL) | false positive script % | 768 | **0.00%** | 0.00% | 0.00% |
|
| 142 |
|
| 143 |
**Decision rule** (`unknown_threshold = 0.7038`, `script_threshold = 0.55`):
|
| 144 |
|
|
|
|
| 148 |
| Government docs (`r3_test`) | 2,140 | 87.9% (**91.2%**) | 4.1% | 8.0% |
|
| 149 |
| MDPBench, photographed | 2,040 | 70.3% (**93.7%**) | 2.0% | 27.7% |
|
| 150 |
|
| 151 |
+
*(Single-script spurious 2+ rate on `r2_test`: **2.1%** at `0.55`; mixed-script exact set on `r2_test` at `0.50` without gate: **46.6%**.)*
|
| 152 |
|
| 153 |
**Unseen scripts:**
|
| 154 |
+
- **Real 2026 Sinhala (`Sinh`) PDFs** (46 full pages from `CC-MAIN-2026-47`, never trained): **73.9%** refused as `unknown` (2 seeds: 73.9β78.3%).
|
| 155 |
+
- **Clean synthetic unseen (`Guru`, `Sinh`)** (NVIDIA v2 shard 00000, 160 pages): **51.9%** refused.
|
| 156 |
+
- **Exact 18-class leave-out** (`Geor`, `Khmr`, `Telu` removed from training, 855 `r3` pages, undistilled `mnv4s`): **61.6%** pooled refused (`Geor` 91.9%, `Telu` 59.5%, `Khmr` 44.6%). On real `r3` `Khmr` pages that also contain secondary Latin headers/URLs, multi-label Latin sensitivity fires `Latn` rather than `unknown`. This model is **not** a reliable detector for unseen scripts.
|
| 157 |
|
| 158 |
## Speed & Parity
|
| 159 |
|
|
|
|
| 171 |
|
| 172 |
1. **Unseen scripts & look-alikes:** not an unseen-script detector. Unseen scripts that visually resemble trained scripts (e.g. Gurmukhi β Devanagari, Sinhala β Kannada/Telugu) or appear alongside secondary Latin headers are often accepted with moderate-to-high confidence.
|
| 173 |
2. **Weakest known scripts (`r3_test`):** Telugu (`60.0%`, 2 seeds 60β62%) and Gujarati (`72.4%`) remain the weakest classes.
|
| 174 |
+
3. **Photographed Cyrillic (`ru`):** while digital `ru` reaches 90.0%, severely degraded photographed `ru` is `52.5%` top-1 (often confused with Latin or refused as unknown).
|
| 175 |
+
4. **Crops:** raw squashed text-block crops are mostly refused (`~30%` top-1). Use square white padding (`--pad`) if running on crops.
|
| 176 |
5. **Not a "no-text" detector:** although blank BHL old-book pages had `0.00%` false positives here, pair with a dedicated no-text classifier (e.g. `small-models-for-glam/page-ocr-flags`) if blank detection is critical.
|
| 177 |
|
| 178 |
## Training & Attribution
|
| 179 |
|
| 180 |
- **Real PDFs:** born-digital & scanned pages from [FinePDFs](https://huggingface.co/datasets/HuggingFaceFW/finepdfs) (**ODC-By**; we thank its authors), 3,794 fresh 2026 PDFs from [Common Crawl](https://huggingface.co/commoncrawl) (`CC-MAIN-2026-47`, page-level Unicode text-layer labels), plus library/archive scans (BHL, US government docs, IDL) labelled by vision-language model agreement. No raw page images are redistributed.
|
| 181 |
- **Synthetic pages:** 8,287 purity-filtered pages from [nvidia/OCR-Synthetic-Multilingual-v2](https://huggingface.co/datasets/nvidia/OCR-Synthetic-Multilingual-v2) (**CC-BY-4.0**; positives across 21 scripts + 4 shaped negative scripts: Lao, Oriya, Meitei, Ol Chiki) and 300 Tibetan synthetic negatives from [WueNLP/Synthdog-Multilingual-100](https://huggingface.co/datasets/WueNLP/Synthdog-Multilingual-100) (**Apache-2.0**).
|
| 182 |
+
- **Teachers & Distillation:** 3-family ensemble of `timm/convnext_small.fb_in22k_ft_in1k_384`, `timm/hgnetv2_b4.ssld_stage2_ft_in1k`, and `timm/caformer_s36.sail_in22k_ft_in1k_384` (all Apache-2.0) distilled into `timm/mobilenetv4_conv_small.e2400_r224_in1k` (Apache-2.0) for 16 epochs with Asymmetric Loss + soft BCE KD (`alpha=0.50, T=1.5`), oblique/curl/hand/rotation/padded-crop/CutMix augmentation, EMA weights, and `OneCycleLR`.
|
| 183 |
- **Compute:** trained on [Hugging Face Jobs](https://huggingface.co/docs/hub/jobs) (~52 min on `a10g-small` for the distilled student).
|