KhmerClipper TrOCR (Khmer + English)
A TrOCR model fine-tuned to read single text lines in Khmer, English and mixed Khmer/English. It is the model behind KhmerClipper, an on-device screen OCR app.
These are the full PyTorch weights (safetensors), ready for inference or further fine-tuning. The Windows/Linux app uses the ONNX export at kcsheang/khmerclipper-trocr-khmer-onnx.
Usage
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image
repo = "kcsheang/khmerclipper-trocr-khmer"
processor = TrOCRProcessor.from_pretrained(repo)
model = VisionEncoderDecoderModel.from_pretrained(repo).eval()
image = Image.open("line.png").convert("RGB") # one line of text
pixel_values = processor(images=image, return_tensors="pt").pixel_values
ids = model.generate(pixel_values, max_new_tokens=128)
print(processor.batch_decode(ids, skip_special_tokens=True)[0])
The model reads one line at a time. For a paragraph or a screenshot, split it into lines first. KhmerClipper's line splitter is in the app source.
Fine-tuning
The checkpoint loads into the standard VisionEncoderDecoderModel / Seq2SeqTrainer workflow. The training scripts and the exact recipes for every version are in the repo: training/trocr and docs/PROJECT.md.
- Tokenizer: the byte-level BPE tokenizer from the base model, plus one added token per Khmer character (U+1780–17FF, U+19E0–19FF and the zero-width separators). Each Khmer character is one token instead of three bytes. Keep this tokenizer when you fine-tune.
- Labels were capped at 128 tokens.
- Each version was fine-tuned from the previous one at a lower learning rate (v1: 2e-5, 3 epochs; v2: 1e-5, 1 epoch; M2: 5e-6, 1 epoch from v3; M3: 5e-6, two 1-epoch runs from M2). If you add your own data, mix in some of the original sources so the model doesn't forget them.
Results
Character error rate (CER, lower is better). KHOB (EKYC Solutions, MIT, never trained on): 1,862 real Khmer lines cut from scanned and photographed pages, with the published boxes registered to each page first (they run about 2% too tall on the scans), pooled CER:
| Model | All | task 1 | task 2 | task 3 | task 4 |
|---|---|---|---|---|---|
| M2 | 3.3% | 2.4 | 3.3 | 3.7 | 3.7 |
| This model (M3) | 2.6% | 1.9 | 2.4 | 3.0 | 3.0 |
| Chakkhu-OCR-Khmer | 3.1% | 2.1 | 2.7 | 3.6 | 3.7 |
Frozen set of 200 line images. Numbers are on-device CoreML runs on Apple Silicon, including the app's Latin-line fallback.
| Engine | All | printed | ui | mixed |
|---|---|---|---|---|
Tesseract 5 khm+eng (stock) | 0.2580 | 0.5370 | 0.0199 | 0.0894 |
| TrOCR before fine-tuning on new data (v0) | 0.0593 | 0.0433 | 0.0662 | 0.0950 |
| v3 (model M1) | 0.0035 | 0.0036 | 0.0030 | 0.0046 |
| M2 | 0.0034 | 0.0033 | 0.0030 | 0.0046 |
| This model (M3) | 0.0043 | 0.0055 | 0.0030 | 0.0046 |
M3 trades 3 characters on the frozen set (a space, ្ដ written as ្ត, one bracket) for fewer errors on real scans. Through the app on noisy, busy captures it makes 9% fewer errors than M2 on the Mac (CER 5.94% to 5.41%) and 15% fewer with the int8 ONNX export; screen captures are level (1.50% to 1.49%). On the Thareah handwriting test pages, model alone, CER falls from 51.5% to 30.7%; on our own handwriting test lines it rises from 0.54% to 0.98% (14 of 307 lines change, both ways).
Earlier checkpoints saw the SoyVitou test split, which the printed fixtures come from, so the printed number is optimistic. The ui and mixed subsets were never in any training data. Full write-ups are in training/eval/results.
Training data
Public Hugging Face line-image datasets (listed in the metadata above), plus sets rendered locally with OFL Google Fonts and, from M2, the Khmer OS family (LGPL-2.1+):
synth: 400k multi-font Khmer and mixed linessynth_en: 150k clean English lines, with text from English Wikipedia (CC BY-SA 4.0)synth_screen: 204k small screen-text strips at 11–24 px- M2 renders: 100k general lines, 100k screen strips and 60k handwriting-font strips that add the Khmer OS faces
- M3
synth_doc: 120k Khmer Wikipedia lines (CC BY-SA 4.0) rendered at scan size with slivers of the neighbouring lines and a scan or phone look - M3
odc: 14,901 lines cut from Open Development Cambodia law and decree scans, labelled by Chakkhu-OCR-Khmer (Apache-2.0) and kept at confidence 0.97 or more
Every evaluation string was removed from the training text. The datasets come with their own licenses: some are MIT or CC-BY-4.0, and a few don't state one. See the dataset table for details. Thanks to all the dataset authors.
Training ran on a single RTX 3090 (bf16, Seq2SeqTrainer). This release is M3, from training run v8b. Model release numbers (M1 = v3, M2 = v7b, M3 = v8b) are separate from experiment names.
Limitations
- Single text lines only, with no layout analysis.
- Strongest on printed and screen text. Handwriting is supported, but less tested. Very small marks (one or two characters) are often misread.
- Lines much wider than about 12× their height should be decoded in pieces.