- ThaOCR β Khmer + English document OCR for CPU, browser and phone (ONNX)
- How good is it
- Quick start
- Models
- Versions
- Where the files are
- How the models were trained
- Layout detector (Ultralytics YOLO11, imgsz 1024, AdamW, cosine LR, mosaic 0.6, no horizontal flip)
- Text-line detector (DB, PP-OCRv5 mobile backbone, 1.16 M params)
- Text recogniser (CRNN-CTC, arch "M": conv 48-96-192-256, proj 320, BiLSTM 2x256, 4.76 M params, line height 48)
- Font classifier (
font/font_cnn_v2.onnx, FontNet, ~0.45 M params) - Structure model (
structure/struct_v1.onnx, StructureNet, ~0.6 M params) - Structure v3 (
structure/struct_v3_pre.onnx,structure/struct_v3_tab.onnx, ~2.0 M params, preview) - Table structure (
table/tsr_yolo11n_mix.onnx, YOLO11n, 800 px table crops)
- Datasets
- Licence
- How good is it
ThaOCR β Khmer + English document OCR for CPU, browser and phone (ONNX)
A complete document OCR pipeline for Khmer (ααααα) and English in about 41 MB. It finds the regions of a page, detects every text line, reads it, and works out fonts, paragraphs and reading order. Every model is plain ONNX (opset 17) and runs with onnxruntime on CPU, in the browser (ONNX Runtime Web) or on a phone (ORT Mobile). No GPU needed.
- Reads printed Khmer and English, handwriting, and text under official stamps
- Understands the page: 22 layout classes (tables, stamps, signatures, handwriting, form fieldsβ¦), paragraphs, lists, reading order, font family and size; table rows and columns in preview
- Small: about 41 MB for the five default models, about 26 MB with the int8 recogniser
- Documented: every version, its training data and its scores are listed below
How good is it
Scores of the default models on their validation sets. Most of these sets are synthetic; the rows on real data say so.
| stage | score |
|---|---|
| Recognition, printed text | CER 0.22 % (synthetic Khmer print) Β· 0.29 % (real English lines) |
| Recognition, real documents | CER 12.7 % (held-out set of real documents) |
| Recognition, real handwriting | CER 9.8 % on SoyVitou/khmer-handwritten-dataset-4.2k Β· 21.3 % on Thareah/khmer-handwriting |
| Layout | mAP50 0.974 Β· mAP50-95 0.850 (22 classes) |
| Text-line detection | F1 0.894 synthetic Β· 0.880 DocLayNet Β· 0.697 real Khmer PDFs |
| Font | family 92.0 % Β· weight 98.0 % Β· slant 99.9 % |
| Structure | role accuracy 0.988 Β· reading order Kendall tau 0.943 |
Clean printed text is the strong case. Real handwriting and noisy scans are harder and still improving; the full numbers are in How the models were trained.
Quick start
Download the whole set, so the relative paths in models.json resolve:
pip install -U huggingface_hub onnxruntime numpy pillow
hf download Thareah/ThaOCR-models --local-dir ThaOCR
Read one cropped text line with the recogniser:
import json
import numpy as np
import onnxruntime as ort
from PIL import Image
sess = ort.InferenceSession("ThaOCR/rec/crnn_m_v2_stamp2_int8.onnx")
vocab = json.load(open("ThaOCR/rec/crnn_m_v2_stamp2.vocab.json", encoding="utf-8"))["itos"]
img = Image.open("line.png").convert("L") # one cropped text line, grey
img = img.resize((max(48, round(img.width * 48 / img.height)), 48))
x = np.asarray(img, dtype=np.float32)[None, None] / 255.0 # [1, 1, 48, W], values 0..1
logits = sess.run(None, {sess.get_inputs()[0].name: x})[0] # [time, batch, 195]
ids = logits.argmax(-1).reshape(-1)
keep = (ids != 0) & (ids != np.r_[-1, ids[:-1]]) # greedy CTC: drop blanks (id 0) and repeats
print("".join(vocab[i] for i in ids[keep]))
A full page goes through layout, text-line detection, recognition, font and structure in that order. models.json is the manifest the ThaOCR backend reads: file/vocab are relative to the root of this repo, defaults names the model used for each stage. Pre/post-processing (letterbox + NMS, DB unclip, width bucketing + CTC) is small numpy/OpenCV code in the ThaOCR backend (backend/app/services/stages/).
Models
Click a version to open its file. Bold = the default used by the app.
| model | what it does | versions |
|---|---|---|
| Layout | finds the regions of a page (text, title, table, figure, stamp, signatureβ¦) | V1 Β· V2 Β· V2 nano Β· V3 Β· V4 Β· V4 int8 |
| Text detection | finds every text line on the page | Original Β· V1 Β· V2 |
| Recognition | reads the text of each line (Khmer + English) | V1 Β· V2 Β· V2.1 Β· V2.2 Β· V2.3 Β· V2.4 |
| Font | tells the font family, weight, slant and size of each line | V1 Β· V2 |
| Structure | gives each line its role, paragraph, list level and reading order; v3 also tells document vs sheet and groups table lines into rows/columns/cells | V1 Β· V3 pre Β· V3 + tables |
| Table structure | finds rows, columns, header and spanning cells inside a table crop | V3 pre Β· V3 |
Versions
| release | git tag | what is in it |
|---|---|---|
| v3-tables (preview, 2026-11-14) | β | adds structure v3 (+ table heads) and the table-structure YOLO11n; models.json defaults unchanged |
| v3 (latest default set) | v3 |
layout v4 (default, plus int8) Β· textdet v2 Β· rec v2.3 Β· font v2 Β· structure v1 |
| v2 | β | first publication: layout v3 default, same textdet / rec / font / structure |
All model files are in this one repo; download the latest commit (the v3 git tag points at an index-only commit without the weights).
Where the files are
Everything is in this one repo, one folder per stage, together with the full models.json manifest.
| stage | folder |
|---|---|
| layout | layout/ |
| textdet | textdet/ |
| rec | rec/ |
| font | font/ |
| structure | structure/ |
| table structure | table/ |
Download the whole set so the relative paths in models.json resolve:
hf download Thareah/ThaOCR-models --local-dir ThaOCR
| stage | default model | file | size | what it does |
|---|---|---|---|---|
| layout | layout_yolo11n_v4_22cls (_int8: 3.1 MB, less exact) |
layout/layout_v4_n_22cls.onnx |
10.9 MB | 22 region classes: text, title, section_header, list, table, figure, caption, header, footer, page_number, footnote, formula, signature, stamp, logo, qr_code, barcode, photo, handwriting, checkbox, form_field, decoration. Input [1,3,1024,1024]. |
| textdet | db_ppocrv5_khmer_v2 |
textdet/db_v2.onnx |
4.8 MB | DB text-line detector (PP-OCRv5 mobile, fine-tuned for Khmer). Input [1,3,H,W], H and W multiples of 32. |
| rec | crnn_m_v2_stamp2 / _int8 |
rec/crnn_m_v2_stamp2*.onnx + .vocab.json |
19 MB / 4.8 MB | CRNN-CTC line recogniser, Khmer + English, 195 symbols, line height 48, dynamic width [N,1,48,W]. |
| font | font_cnn_v2 |
font/font_cnn_v2.onnx |
1.9 MB | per line: font family (29 Khmer/Latin families), weight, slant, em size. Input 32x256 grey. |
| structure | struct_v1 |
structure/struct_v1.onnx |
4.0 MB | small Transformer over the page's lines: role, paragraph start, list level, bullet, reading order. |
Older versions kept for comparison: layout v1 (YOLO11s, 12 classes; .onnx + .pt), layout v2 (YOLO11s round 2,
21 classes), v2 YOLO11n and v3 YOLO11n, textdet v1 and the original PP-OCRv5 detector, recogniser v1, v2, v2.1 (_hw),
v2.2 (_stamp), v2.4 (_stamp3), font v1.
How the models were trained
Layout detector (Ultralytics YOLO11, imgsz 1024, AdamW, cosine LR, mosaic 0.6, no horizontal flip)
- v1 β YOLO11s, 12 classes, trained in the earlier Khm-Dataset-Creation repo on synthetic Khmer pages, real Khmer PDF pages with heuristic labels and DocLayNet.
- v2 round 1 (
layout_r1) β YOLO11s from v1 weights, 21-class taxonomy, 30 epochs (stopped at 26 by the time budget), lr0 1e-3, batch 8. Val mAP50 0.964, mAP50-95 0.834. - v2 round 2 (
layout_r2,layout_v2_r2_21cls.onnx) β continued from round 1 on the round-2 set (32,290 train / 923 val pages), lr0 5e-4. mAP50 0.962, mAP50-95 0.834. - v2 nano (
layout_v2_n_21cls.onnx) β YOLO11n (2.6 M params) from the COCO-pretrained yolo11n.pt on the same round-2 set, 31 epochs, lr0 2e-3, batch 16. mAP50 0.956, mAP50-95 0.798. - v3 nano (
layout_v3_n_22cls.onnx, default) β fine-tune of v2 nano onlayout_v3(18,000 train / 1,100 val: 10,000 new docgen v3 pages with thedecorationclass + 8,000 pages replayed from round 2), 37 epochs, lr0 6e-4. mAP50 0.972, mAP50-95 0.842. - v4 nano (
layout_v4_n_22cls.onnx, default) β fine-tune of v3 nano onlayout_v4(19,000 train / 1,200 val: 9,000 new docgen v4 pages β Khmer government notices and letters with emblem letterheads and mottos, handwritten reference numbers and dates, label-value blocks, tables with spanning group rows, stamps printed over signatures and dates β + 10,000 pages replayed fromlayout_v3), 57 epochs in a 3 h budget, lr0 0.0003. On the v4 validation set: mAP50 0.974, mAP50-95 0.850 (v3 on the same set: 0.935 / 0.802). Largest gains: handwriting 0.42 β 0.75, header 0.70 β 0.93, text 0.72 β 0.87, stamp 0.76 β 0.82, signature 0.66 β 0.73 (mAP50-95).layout_v4_n_22cls_int8.onnxis the same network after onnxruntime dynamic weight quantisation (3.1 MB); boxes are visibly less tight on tables and forms, so the app does not pick it by default.
Layout training data (round 2 set): docgen v2 synthetic pages (12,000 Cambodian + US/English documents: letters, invoices, forms, ID cards, exam sheets, receipts, reportsβ¦), DocLayNet v2 (6,895 train / 647 val real pages, 11 classes + page_number, plus degraded scan/photo copies), real Khmer PDF pages with silver heuristic labels (548 train / 173 val, each also degraded), SignverOD signatures and stamp scans (round 3 of the v1 repo), D4LA real US scans (2,834 train, only form/invoice/letter/memo; missing classes filled by a teacher model, training/layout/pseudo_label.py), CommonForms (1,517 pages, checkbox boxes only), docgen exam answer sheets (3,000 pages). Taxonomy in training/taxonomy.py.
Text-line detector (DB, PP-OCRv5 mobile backbone, 1.16 M params)
det/train_textdet.py converts the original PP-OCRv5 mobile detector ONNX to PyTorch (onnx2torch) and fine-tunes the probability map with DB shrunk kernels (r = 0.4) so the original unclip post-processing still works.
- v1 β fine-tuned on synthetic Khmer pages + real Khmer PDF pages + DocLayNet text-layer line boxes.
- v2 (
db_v2.onnx, default) β 20,000 iterations, batch 16,--robustaugmentation, from thetextdet_robustcheckpoint; loss weights synth 0.45 / Khmer PDF 0.20 / DocLayNet 0.35. Data: 15,000 synthetic train pages (docgen v2 + exam sheets + handwriting lines) / 750 val, DocLayNet 6,895 / 647, Khmer PDF 548 / 173. Final F1: synthetic 0.894, DocLayNet 0.880, Khmer PDF 0.697 (0.690 low-res).
Text recogniser (CRNN-CTC, arch "M": conv 48-96-192-256, proj 320, BiLSTM 2x256, 4.76 M params, line height 48)
Trainer: training/rec/train.py (the Khm-Dataset-Creation trainer + packed line sets), --aug strong (GPU warp, slant, low resolution, polarity), greedy CTC decoding. Vocabulary 195 symbols (Khmer, Latin, digits, punctuation), text normalised with normalize_khmer.
Lineage (each step initialised from the previous best checkpoint):
- v1 (
crnn_m_v1) β 6 epochs onmix_print_scene: synthetic print sections S01βS08 (200k lines sampled; news corpus rendered with 21 open-licence Khmer fonts + Khmer OS fonts, print/scan effects) + synthetic scene/signboard sets (dataset_v2 scene/heavy/medium/clean, nf_mix, hard, nf_hard, long). 739k lines/epoch. - expA β 2 epochs adding real English lines from DocLayNet text layers (
rec_doclaynet, 90,860 train lines). - v2 (
crnn_m_v2) β 6 epochs, lr 1e-3, onfinal_v2_hand: everything above + docgen printed lines (99,869) + synthetic handwriting lines from docgen exam sheets (69,081, Khmer handwriting fonts) + docgen form handwriting (11,905). 1.008 M lines/epoch. Val CER: synthetic print 0.22 %, English real 0.30 %, synthetic handwriting 4.8 %. - v2.1 (
crnn_m_v2_hw) β 3 epochs, lr 5e-4, onfinal_v2_hand_real: + real Khmer handwriting from Thareah/khmer-handwriting (2,311 train lines, x8 oversampled) and SoyVitou/khmer-handwritten-dataset-4.2k (3,549, x4). 1.041 M lines/epoch. - v2.2 (
crnn_m_v2_stamp) β 2 epochs withSTAMP_AUG=0.12:training/rec/stamp_aug.pydraws grey ring arcs and stamp strokes over 12 % of the crops so text under official red stamps is still read. - v2.3 (
crnn_m_v2_stamp2, default) β 4 epochs, lr 1.2e-4, same data and stamp augmentation, from v2.1. Val CER: synthetic print 0.216 %, English real 0.29 %, docgen 7.5 %, synthetic exam handwriting 4.5 %, real handwriting Thareah 21.3 % / SoyVitou 9.8 %, owner's real document dev set 12.7 %. - v2.4 (
crnn_m_v2_stamp3) β 4 more epochs, lr 1e-4. Slightly better on every val set (print 0.213 %, real_dev 12.6 %); not yet made the default. int8 variants are dynamic-quantised exports of the same checkpoints (training/rec/export.py).
Font classifier (font/font_cnn_v2.onnx, FontNet, ~0.45 M params)
Trained entirely on synthetic line crops rendered on the fly (training/font/gen.py: Khmer news corpus + English sentences, PIL + HarfBuzz shaping, 29 font families: all Khmer OS faces, Noto Sans/Serif Khmer, Hanuman, Kantumruy Pro, Nokora, Odor Mean Chey, Chenla, Preahvihear, Suwannaphum, the Khmer display fonts (Koulen, Dangrek, Bayon, Angkor, Taprom) and Latin serif / sans; synthetic bold by dilation and italic by shear; neighbour-line intrusion, blur, noise, JPEG, paper tint). v1: 40 epochs x 400 steps x batch 256. v2: 24 epochs from v1 adding the em-size regression head. Val (8,000 held-out crops): family 92.0 %, weight 98.0 %, slant 99.9 %, size error median 1.7 %.
Structure model (structure/struct_v1.onnx, StructureNet, ~0.6 M params)
4-layer Transformer (d 160) over per-line geometric + text features, with a pointer head for reading order. Trained 30 epochs, batch 16, on docgen v3 pages with struct labels (10,000 train / 500 val synthetic pages). Val: role accuracy 0.988, paragraph-start F1 0.971, successor accuracy 0.964, Kendall tau 0.943.
Structure v3 (structure/struct_v3_pre.onnx, structure/struct_v3_tab.onnx, ~2.0 M params, preview)
6-layer Transformer (d 192) over the same per-line features as v1, plus heads for page kind (document / sheet / mixed), in-table lines and same-row / same-column / same-cell line pairs. struct_v3_pre was trained on docgen pages + phone-pipeline output of synthetic pages (photo-style val: role 0.95, successor 0.92, Kendall 0.94); its table heads are untrained. struct_v3_tab continues from it on docgen v5 table/sheet pages (6,846 train pages + 846 pipeline pages, 17 epochs). Val: role 0.997, page kind 1.00, in-table F1 1.00, same-row F1 0.84, same-column F1 0.98, same-cell F1 0.69; photo-style val: role 0.96, Kendall 0.94. Not the app default yet.
Table structure (table/tsr_yolo11n_mix.onnx, YOLO11n, 800 px table crops)
Classes: row, column, column_header, spanning_cell, projected_row_header. tsr_yolo11n_real_pre is a warm-up on PubTables-1M crops; tsr_yolo11n_mix fine-tunes it on PubTables-1M + FinTabNet + TabRecSet phone photos (incl. homography-rectified copies) + synthetic Khmer/English sheets (docgen v5), 23 epochs. Best val mAP50 0.836, mAP50-95 0.574.
Datasets
| data | type | used by | licence |
|---|---|---|---|
| Khmer news corpus (5,601 articles, 257k lines; scraped by the owner for research) rendered with open Khmer fonts | synthetic | rec, font | text: research use; fonts OFL / LGPL |
| docgen v2 / v3 / v4 / exam synthetic pages (Project Gutenberg English, Faker identities, procedural logos/stamps/figures) | synthetic | layout, textdet, rec, structure | generated |
| DocLayNet v2 | real | layout, textdet, rec (English lines) | CDLA-Permissive 1.0 |
| D4LA (form/invoice/letter/memo) | real | layout | Apache-2.0 (mirror), page content third-party |
| CommonForms (checkbox boxes) | real | layout | Apache-2.0 |
| Real Khmer PDF pages (government/NGO PDFs crawl, heuristic labels) | real | layout, textdet | research use |
| SignverOD signatures, mapo80 stamps | real | layout | Apache-2.0 / CC-BY-4.0 |
| Thareah/khmer-handwriting, SoyVitou/khmer-handwritten-dataset-4.2k | real | rec | MIT |
| docgen v5 tables / sheets (synthetic Khmer + English tables, invoices, spreadsheets) | synthetic | structure v3, table structure | generated |
| PubTables-1M | real | table structure | CDLA-Permissive 2.0 |
| FinTabNet | real | table structure | CDLA-Permissive 1.0 |
| TabRecSet (phone photos of tables) | real | table structure | CC BY 4.0 (figshare) / CC BY-SA 4.0 (README) |
| cTDaR | real | evaluation only (no licence) | β |
| FUNSD, KhmerEduDoc | real | evaluation only (non-commercial) | NC |
Base weights: Ultralytics YOLO11 (AGPL-3.0 β the layout models inherit it), PaddleOCR PP-OCRv5 mobile detector (Apache-2.0). The owner's private exam photos were never used for training.
Licence
The repo mixes licences, so the Hub tag is license: other. Per stage:
| Stage | Files | Licence |
|---|---|---|
| Layout | layout/*.onnx, layout/layout_v1_12cls.pt |
AGPL-3.0 β fine-tuned from Ultralytics YOLO11 weights and inherits that licence |
| Text detection | textdet/db_v1.onnx, textdet/db_v2.onnx, textdet/db_ppocrv5_orig.onnx |
Apache-2.0 β derived from (or identical to, for db_ppocrv5_orig) the PaddleOCR PP-OCRv5 mobile detector |
| Recognition | rec/* |
owner's own models, trained from scratch |
| Font | font/* |
owner's own models, trained from scratch on synthetic data |
| Structure | structure/*.onnx |
owner's own models, trained from scratch on synthetic data |
| Table structure | table/*.onnx |
AGPL-3.0 β fine-tuned from Ultralytics YOLO11 weights; training data incl. TabRecSet (CC BY / BY-SA 4.0, attribution required) |
Training data licences are listed in the Datasets table above. FUNSD and KhmerEduDoc were used for evaluation only (non-commercial); no model weights were trained on them.
- Downloads last month
- 26
Model tree for Thareah/ThaOCR-models
Base model
PaddlePaddle/PP-OCRv5_mobile_det