Skip to content

[models] Multilingual crop and page orientation models, fix the orientation training rotation - #2239

Open
felixdittrich92 wants to merge 2 commits into
mindee:mainfrom
felixdittrich92:crop-orientation-improvement
Open

felixdittrich92 wants to merge 2 commits into
mindee:mainfrom
felixdittrich92:crop-orientation-improvement

Conversation

@felixdittrich92

@felixdittrich92 felixdittrich92 commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

This PR:

  • fixes a label leak in references/classification/train_orientation.py that made the orientation models learn the padding position instead of the text orientation;
  • adds --class-prior to the same script, to train with a realistic share of upright samples;
  • updates the mobilenet_v3_small_crop_orientation weights: same architecture, now multilingual;
  • updates the mobilenet_v3_small_page_orientation weights: same architecture, fine-tuned on real pages;
  • adds two small library fixes needed for (or found by) the training.

The label leak

The samples were resized and symmetrically padded to the model input size first, and only then rotated. The odd remainder of the symmetric padding always goes to the top / left (e.g. 97 px top, 96 px bottom), so in training a 180° sample always had the extra row at the bottom, and a ±90° sample the extra column on the opposite side. At inference, the crop is rotated before it is padded, so this cue never appears and the model falls back to "upright" or "-90".

Measured on the same crops, through the training transforms vs. through crop_orientation_predictor:

model training transforms crop_orientation_predictor
current weights 96.8% 93.8%
first fine-tune with the old script 98.7% 87.9%

The script now rotates first (torch.rot90, exact), then resizes, pads and augments. 0° and 180° samples (and ±90°) now get identical padding.

Other changes

  • --class-prior p0 p-90 p180 p90: the training orientations are drawn with these probabilities (the validation set stays uniform). Most crops are upright in practice. With a prior of 0.4 / 0.2 / 0.2 / 0.2, ambiguous crops (digits, short tokens, single characters) stay upright instead of being rotated at random.
  • Resize: integer class targets are kept as is (they were treated as boxes when preserve_aspect_ratio=True).
  • OrientationDataset: the image names are stored in a single numpy array. With millions of images, the list of Python objects was copied by every DataLoader worker (refcount updates trigger copy-on-write), which took tens of GB.

New weights

mobilenet_v3_small_crop_orientation, fine-tuned from the current weights in 3 runs (2 discarded, see below):

  • 3.5M synthetic multilingual word crops (97 languages / scripts);
  • 300k real upright word crops from real documents (receipts, invoices, tables, books, newspapers, ID cards, forms, scene text);
  • the fixed rotation and --class-prior 0.4 0.2 0.2 0.2.

The first fine-tune with the old script had the leak. The second, with the fix and uniform orientations, was better on average but flipped more words on upright pages.

Evaluation

Crop level: 31k upright word crops (none used for training), each rotated by 0 / 90 / 180 / 270°:

subset current new
overall 85.8% 94.3%
crops correct at all 4 rotations 73.1% 88.3%
real documents (Latin) 96.2% 96.7%
FUNSD / CORD / SROIE 92.8% 92.8%
real multilingual documents (Greek, Cyrillic, Hebrew, Indic, Vietnamese, ...) 80.3% 92.0%
synthetic multilingual (97 scripts) 79.3% 95.8%
receipts / invoices / ID cards (real) 92.7 / 97.3 / 93.3% 97.3 / 98.0 / 94.3%
books / newspapers (real, multilingual) 53.3 / 76.8% 93.5 / 99.2%
Chinese documents 91.2% 90.9%
IIIT5K / SVT / IC13 (scene text) 90.9% 87.9%

Per-script examples (synthetic): Hebrew 41 → 98%, Hindi 52 → 100%, Bengali 52 → 99%, Arabic 67 → 98%, Odia 21 → 96%. Upside-down Hebrew and Indic text was mostly read as upright by the current model.

End-to-end: ocr_predictor(assume_straight_pages=False) (fast_base + multilingual crnn_vgg16_bn) on 296 real pages, each rotated by 0 / 90 / 180 / 270 / 15°. Same detection and recognition for both models, only the crop orientation model changes:

current new
words read in the correct direction, all angles 76.2% 89.3%
at 0° / 90° / 180° / 270° / 15° 87 / 82 / 63 / 63 / 88% 94 / 89 / 83 / 86 / 95%
text F1 vs. the upright page read with straight pages 0.481 0.535
word F1 vs. ground truth (Latin documents, all angles) 0.730 0.734

ONNX (OnnxTR)

fp32 export: same predictions as PyTorch. The static 8-bit file needs per-channel weights: per-tensor quantization drops to 54%.

ONNX file accuracy (16k crops)
new fp32 94.6%
new static 8-bit (per-channel, percentile calibration) 93.2%
current fp32 / static 8-bit 85.4% / 77.7%

Page orientation model

mobilenet_v3_small_page_orientation, fine-tuned from the current weights with the fixed script (30 epochs, --class-prior 0.4 0.2 0.2 0.2) on 13k upright real pages: receipts, invoices, tables, books, newspapers, ID cards, forms and scene photos in many languages. A test set of 384 pages was checked by hand.

Each page was rotated by 0 / 90 / 180 / 270°, straight and with a 10° skew:

current new
test pages (384, reviewed by hand) 78.2% 90.0%
validation pages (1,536) 72.0% 94.0%
upright pages kept upright (test / val) 76.7 / 72.2% 91.7 / 96.1%
receipts / tables / ID cards (test) 77 / 74 / 75% 95 / 95 / 91%
scene photos (test) 52% 73%

End-to-end, ocr_predictor(assume_straight_pages=False, straighten_pages=True) on 150 test pages × 4 rotations: the text F1 against the upright page read with straight pages goes from 0.520 to 0.536. Most errors of the page model are corrected by the crop orientation model in the pipeline anyway.

Weights

…training rotation

- train_orientation: rotate the samples before resizing / padding them, like the inputs at inference.
  Rotating the padded image leaked the label (the odd remainder of the symmetric padding always goes to the
  top / left, a rotation moves it to the bottom / right), so the models learned the padding position instead of
  the text orientation: hence a strong bias to "upright" at inference.
- train_orientation: `--class-prior` to draw the training orientations with a prior (e.g. 0.4 0.2 0.2 0.2).
- Resize: integer class targets are kept as is (not resized as boxes).
- OrientationDataset: the image names in a single numpy array (the DataLoader workers copied a list of
  millions of Python objects).
- New mobilenet_v3_small_crop_orientation weights: fine-tuned on 3.5M multilingual synthetic word crops and
  300k real word crops, with the fixed rotation and an upright prior of 0.4.
@felixdittrich92 felixdittrich92 self-assigned this Nov 27, 2026
@felixdittrich92 felixdittrich92 added this to the 1.3.0 milestone Nov 27, 2026
@felixdittrich92 felixdittrich92 added type: enhancement Improvement module: models Related to doctr.models ext: tests Related to tests folder ext: references Related to references folder topic: character classification Related to the task of character classification labels Nov 27, 2026
@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.26%. Comparing base (656ea4e) to head (bc115d5).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2239      +/-   ##
==========================================
- Coverage   98.28%   98.26%   -0.02%     
==========================================
  Files         182      182              
  Lines       13165    13172       +7     
==========================================
+ Hits        12939    12944       +5     
- Misses        226      228       +2     
Flag Coverage Δ
unittests 98.26% <100.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…l pages with the fixed rotation

Fine-tuned from the current weights on 13k upright real pages (receipts, invoices, tables, books, newspapers,
ID cards, forms, scene photos, multilingual), with the fixed orientation training and an upright prior of 0.4.
@felixdittrich92 felixdittrich92 changed the title [models] Multilingual crop orientation model and fix the orientation training rotation [models] Multilingual crop and page orientation models, fix the orientation training rotation Nov 27, 2026
@felixdittrich92
felixdittrich92 marked this pull request as ready for review November 27, 2026 20:08

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ext: references Related to references folder ext: tests Related to tests folder module: models Related to doctr.models topic: character classification Related to the task of character classification type: enhancement Improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant