Skip to content

Upcast rotated box transforms - #9175

Merged
AntoineSimoulin merged 4 commits into
pytorch:mainfrom
AntoineSimoulin:upcast-rotated-boxes
Aug 14, 2025
Merged

AntoineSimoulin merged 4 commits into
pytorch:mainfrom
AntoineSimoulin:upcast-rotated-boxes

Conversation

@AntoineSimoulin

@AntoineSimoulin AntoineSimoulin commented Aug 11, 2025 •

Copy link
Copy Markdown
Member

Summary

This pull request addresses an issue where performing certain operations on rotated bounding boxes in float16 format could lead to NaN values due to overflow.

Issue Details

The functions _xyxyxyxy_to_xywhr and _parallelogram_to_bounding_boxes involve squaring operations on bounding box coordinates. When using float16 precision, boxes with absolute coordinate values above 256 can result in overflow, as the square of 257 ($257^2 = 66,049$) exceeds the maximum representable value for float16 ($65,504 = (2−2^{−10}) \times 2^{15}$). This leads to unreliable results and NaN values in subsequent computations.

Solution

To address this issue, this PR ensures that the floating point data is upcast to at least float32 for these operations. This increases the maximum representable value, allowing support for box coordinates up to $\sqrt{2^{31} - 1} \approx 46,341$, which should be sufficient for all common computer vision applications.

Additional Improvements

Simplified code related to rotated bounding box conversion by removing unnecessary integer-to-float conversions.
Rotated Bounding Boxes should not be constructed with integer dtype, so these conversions were redundant.

cc @vfdev-5

@pytorch-bot

pytorch-bot Bot commented Aug 11, 2025 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9175

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure

As of commit 5ec4903 with merge base c85f008 (image):

NEW FAILURE - The following job has failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the cla signed label Aug 11, 2025

@NicolasHug NicolasHug left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks a lot for noticing the issue and for the fix @AntoineSimoulin . I left a comment below but I'll approve to unblock

@AntoineSimoulin
AntoineSimoulin merged commit 8c9aaed into pytorch:main Aug 14, 2025
@github-actions

Copy link
Copy Markdown

Hey @AntoineSimoulin!

You merged this PR, but no labels were added.
The list of valid labels is available at https://github.com/pytorch/vision/blob/main/.github/process_commit.py

Nicholas022400701 added a commit to Nicholas022400701/vision that referenced this pull request Oct 6, 2026
…y to xywhr so the squares of the sides do not overflow

_box_xyxyxyxy_to_xywhr squares the side vectors in the input dtype, so a
float16 box with a side of 256 pixels or more comes back with w or h = inf
(and inf, nan for the centre when the target is cxcywhr), a uint8 box with
a side of 30 comes back with 11 and an int16 box with a side of 200 with 0.
The transforms v2 kernel for the same conversion has upcast to float32
since pytorch#9175; this does the same here, keeping the existing rounding for
integer input and casting the result back to the input dtype.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants