Repository navigation
[models] Keep the objectness scores aligned with the boxes when empty crops are dropped - #2164
Open
MohammadHijjawi97 wants to merge 1 commit into
Open
MohammadHijjawi97 wants to merge 1 commit into
MohammadHijjawi97 wants to merge 1 commit into
Conversation
… crops are dropped `_prepare_crops` drops the boxes whose crop is empty before recognition, but the objectness scores were detached earlier and kept their original length. Every word after a dropped box was then built with the score of an earlier box (the dropped one, for the first word after it), in both `OCRPredictor` and `KIEPredictor`. The scores are now filtered with the same mask as the boxes.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2164 +/- ##
=======================================
Coverage 97.54% 97.54%
=======================================
Files 170 170
Lines 10658 10659 +1
=======================================
+ Hits 10396 10397 +1
Misses 262 262
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:
|
felixdittrich92
requested changes
Oct 7, 2026
felixdittrich92
left a comment
Collaborator
There was a problem hiding this comment.
Thanks @MohammadHijjawi97 LGTM 👍
But one thing we should do here is moving the hook after "Crop images".
Let's include this in the PR.
# Apply hooks to loc_preds if any
# for hook in self.hooks:
# loc_preds = hook(loc_preds)
# Crop images
crops, loc_preds, objectness_scores = self._prepare_crops(
pages,
loc_preds,
objectness_scores,
assume_straight_pages=self.assume_straight_pages,
assume_horizontal=self._page_orientation_disabled,
)
# Apply hooks to loc_preds if any
for hook in self.hooks:
loc_preds = hook(loc_preds)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:
Fixes the objectness scores of
OCRPredictor/KIEPredictorgetting shifted when a detected box yields an empty crop._prepare_cropsdrops such boxes before recognition, but the objectness scores were detached from the boxes beforehand and kept their original length, so every word after a dropped box was built with the score of an earlier box (the dropped box's own score for the first word after it)._prepare_cropsnow filters the scores with the same mask as the boxes, and both predictors use the filtered scores.Before:
[(0.1, 0.9), (0.5, 0.1)](the word at x=0.5 carries the dropped box's score); after:[(0.1, 0.9), (0.5, 0.8)]. Same forKIEPredictor.Empty crops come from degenerate boxes (clipped onto the page border, sub-pixel wide, or produced by a custom detection model), so this is an edge case, but the scores were silently wrong when it happened.
Adds
test_predictors_keep_objectness_scores_aligned(OCR and KIE), which fails without the fix.Checks:
pytest tests/pytorch/test_models_zoo_pt.py tests/common/test_models_builder.py tests/common/test_models.py(all tests that don't need to download pretrained weights / test assets pass; I ran offline),ruff check/ruff format --check,mypy doctr/.Any feedback is welcome 🤗