Skip to content

[transforms] Crop the whole crop_box region in crop_detection - #2174

Merged
felixdittrich92 merged 1 commit into
mindee:mainfrom
MohammadHijjawi97:fix-crop-detection-off-by-one
Oct 19, 2026
Merged

felixdittrich92 merged 1 commit into
mindee:mainfrom
MohammadHijjawi97:fix-crop-detection-off-by-one

Conversation

@MohammadHijjawi97

Copy link
Copy Markdown
Contributor

crop_detection projected the relative crop_box onto (w - 1, h - 1) instead of (w, h). The image crop therefore lost its last row and column and no longer matched the region the boxes are rescaled to: relative boxes are divided by the crop's real size, while the image came out one pixel smaller and slightly shifted. Absolute boxes were also clipped to w - 1.

import numpy as np, torch
from doctr.transforms import RandomCrop
from doctr.transforms.functional import crop_detection
from doctr.utils import Sample

img = torch.arange(24, dtype=torch.float32).reshape(1, 4, 6)
crop_detection(img, np.array([[0.0, 0.0, 1.0, 1.0]]), (0.0, 0.0, 1.0, 1.0))[0].shape  # (1, 3, 5)
crop_detection(img, np.array([[0.0, 0.0, 1.0, 1.0]]), (0.5, 0.5, 1.0, 1.0))[0].shape  # (1, 1, 3), expected (1, 2, 3)
RandomCrop(scale=(1, 1), ratio=(1, 1))(Sample(image=torch.rand(3, 1024, 1024), target=np.array([[0.1, 0.1, 0.9, 0.9]]))).image.shape
# (3, 1023, 1023)

With this PR they return (1, 4, 6), (1, 2, 3) and (3, 1024, 1024). RandomCrop is used in the detection and layout reference training scripts, so their random crops now line up with the boxes and masks.

Tests:

  • test_crop_detection: the expected shapes were (3, 26, 37) for a 0.54 x 0.76 crop of a 50x50 image; they are now (3, 27, 38). Added exact-pixel checks for a full crop and a quarter crop, with relative and absolute boxes.
  • test_random_crop: a full-size RandomCrop keeps the image, the mask and the relative target unchanged.

Both fail on main and pass with the fix. ruff check, ruff format --check and mypy doctr/ pass.

Any feedback is welcome 🤗

crop_detection projected the relative crop box onto (w - 1, h - 1) instead of (w, h), so the image crop lost its last row and column and no longer matched the region the boxes are rescaled to. A full crop of a 4x6 image came out 3x5, and RandomCrop(scale=(1, 1)) shrank a 1024x1024 image to 1023x1023.
@codecov

codecov Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.62%. Comparing base (11941d2) to head (b1e0aa3).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2174   +/-   ##
=======================================
  Coverage   97.61%   97.62%           
=======================================
  Files         170      170           
  Lines       10740    10740           
=======================================
+ Hits        10484    10485    +1     
+ Misses        256      255    -1     
Flag Coverage Δ
unittests 97.62% <100.00%> (+<0.01%) ⬆️

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.

@felixdittrich92 felixdittrich92 self-assigned this Oct 19, 2026
@felixdittrich92 felixdittrich92 added this to the 1.2.0 milestone Oct 19, 2026
@felixdittrich92 felixdittrich92 added type: bug Something isn't working ext: tests Related to tests folder module: transforms Related to doctr.transforms labels Oct 19, 2026

@felixdittrich92 felixdittrich92 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @MohammadHijjawi97 👍

@felixdittrich92
felixdittrich92 merged commit 4e29d3f into mindee:main Oct 19, 2026
57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ext: tests Related to tests folder module: transforms Related to doctr.transforms type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants