Repository navigation
[models] Multilingual crop and page orientation models, fix the orientation training rotation - #2239
Open
felixdittrich92 wants to merge 2 commits into
Open
felixdittrich92 wants to merge 2 commits into
felixdittrich92 wants to merge 2 commits into
Conversation
…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.
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…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
marked this pull request as ready for review
November 27, 2026 20:08
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR:
references/classification/train_orientation.pythat made the orientation models learn the padding position instead of the text orientation;--class-priorto the same script, to train with a realistic share of upright samples;mobilenet_v3_small_crop_orientationweights: same architecture, now multilingual;mobilenet_v3_small_page_orientationweights: same architecture, fine-tuned on real pages;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:crop_orientation_predictorThe 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 whenpreserve_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):--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°:
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:ONNX (OnnxTR)
fp32 export: same predictions as PyTorch. The static 8-bit file needs per-channel weights: per-tensor quantization drops to 54%.
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:
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
crop_orientation_mnv3s-3753d2cd.ptandpage_orientation_mnv3s-58e44dbb.pt(URLs updated indefault_cfgs).-4d1e6636/_static_8_bit-032d24f8, page-0d83aa30/_static_8_bit-66ef8a98(OnnxTR PR [models] Update the crop and page orientation models (multilingual) felixdittrich92/OnnxTR#133).