Skip to content

[transforms] Handle samples without boxes in RandomRotate and RandomCrop - #2163

Merged
felixdittrich92 merged 2 commits into
mindee:mainfrom
raashish1601:fix/random-rotate-empty-targets
Oct 7, 2026
Merged

felixdittrich92 merged 2 commits into
mindee:mainfrom
raashish1601:fix/random-rotate-empty-targets

Conversation

@raashish1601

Copy link
Copy Markdown
Contributor

RandomRotate and RandomCrop did not handle a sample without any box (a page with no text, or a negative sample):

import numpy as np, torch
from doctr.transforms import RandomRotate, RandomCrop
from doctr.utils.common_types import Sample

img = torch.rand(3, 40, 100)
RandomRotate(10)(Sample(image=img, target=np.zeros((0, 4, 2), dtype=np.float32)))
# ValueError: zero-size array to reduction operation maximum which has no identity
RandomCrop()(Sample(image=img, target=np.zeros((0, 4), dtype=np.float32)))
# same error

mask = torch.ones(40, 100, dtype=torch.bool)
out = RandomRotate(30, expand=True)(Sample(image=img, mask=mask, target={"words": np.zeros((0, 4, 2), dtype=np.float32)}))
out.image.shape, out.mask.shape  # (3, 40, 100) vs (1, 66, 108)

The dict-target paths already skip empty classes, but plain array targets went straight into the min/max reductions. And when every class of a dict target was empty, RandomRotate returned the original image while the mask had been rotated, so the image and mask no longer matched.

Changes:

  • RandomRotate: an empty array target, or a dict target with only empty classes, now rotates the image (like the target=None path) and keeps the empty target. A small _rotate_image_only helper, mirroring RandomCrop._crop_image_only, is shared with the target=None path.
  • RandomCrop: an empty array target keeps the sample as is, the same as a dict target without any box does today.

Tests: added test_random_rotate_empty_target (box, polygon and dict targets, with a mask) and test_random_crop_empty_target. They fail on main and pass here; pytest tests/pytorch/test_transforms_pt.py passes (45 tests). ruff check, ruff format --check and mypy on the changed module are clean.

RandomRotate and RandomCrop raised 'zero-size array to reduction operation' for an empty (0, 4) or (0, 4, 2) target. With a dict target whose classes are all empty, RandomRotate rotated the mask but returned the unrotated image, so the two no longer had the same size with expand=True.
@felixdittrich92 felixdittrich92 self-assigned this Oct 7, 2026
@felixdittrich92 felixdittrich92 added type: bug Something isn't working ext: tests Related to tests folder module: transforms Related to doctr.transforms labels Oct 7, 2026
@felixdittrich92 felixdittrich92 added this to the 1.2.0 milestone Oct 7, 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.

Hi @raashish1601 👋,

Thanks for the PR only a small change and we are good to merge 👍

Comment thread tests/pytorch/test_transforms_pt.py
@codecov

codecov Bot commented Oct 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.55%. Comparing base (b72ca69) to head (39c603d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2163      +/-   ##
==========================================
+ Coverage   97.54%   97.55%   +0.01%     
==========================================
  Files         170      170              
  Lines       10658    10665       +7     
==========================================
+ Hits        10396    10404       +8     
+ Misses        262      261       -1     
Flag Coverage Δ
unittests 97.55% <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 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 @raashish1601 LGTM 👍

@felixdittrich92
felixdittrich92 merged commit fac9585 into mindee:main Oct 7, 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