Skip to content

Allow users to choose the bbox clamping mode #9128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

NicolasHug
Copy link
Member

@NicolasHug NicolasHug commented Jun 27, 2025

This PR:

  • Adds a clamping_mode parameter to to the BoundingBoxes() constructor which is stored as a metadata attribute. The default is "hard" for now because this is what happens in main. We will change it to "soft" as soon as Adjust clamping for rotated bboxes #9112 is merged.
  • Adds the clamping_mode parameter to all necessary kernels.
  • Adds the clamping_mode parameter to the ClampBoundingBoxes() method. The default here is None, which defaults to the bbox's clamping_mode attribute. @AntoineSimoulin @scotts that's a different default from what we had originally discussed, but I think it makes sense?
  • Adds the SetClampingMode() transform which just sets the clamping_mode attribute of a bboc and can safely be used within a Compose() pipeline.
  • For now, only "hard" and "none" are implemented. Next step will be to merge Adjust clamping for rotated bboxes #9112 to support "soft" mode and make it the default.

I left a bunch of non-critical TODOBB todos in the code, which I'll address later, but before the release. The type checker is failing, I'll address before merging, please review regardless :)

Copy link

pytorch-bot bot commented Jun 27, 2025

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 1 New Failure

As of commit faffedf with merge base 6aee5ed (image):

NEW FAILURE - The following job has failed:

  • Lint / python-types / linux-job (gh)
    RuntimeError: Command docker exec -t e141c348a93313e42031288ff3099040d7c1dc6d1ac7fb2bc8a0f7b111e5734c /exec failed with exit code 1

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

Comment on lines +69 to +72
def transform(self, inpt: tv_tensors.BoundingBoxes, params: dict[str, Any]) -> tv_tensors.BoundingBoxes:
out = inpt.clone()
out.clamping_mode = self.clamping_mode
return out
Copy link
Member Author

Choose a reason for hiding this comment

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

@AntoineSimoulin @scotts in our chat I raised a potential problem with the clamping_mode leaking out of this transform. I think I made our conversation a lot more complicated that it needed to be. We can avoid the "leak" by just calling clone(), as we should, which returns a copy.
No need to make Compose() work like a context manager. I added corresponding tests.

@@ -46,6 +46,14 @@ def is_rotated_bounding_format(format: BoundingBoxFormat) -> bool:
)


# TODOBB consider making this a Literal instead. Tried briefly and got
# torchscript errors, leaving to str for now.
# CLAMPING_MODE_TYPE = Literal["hard", "soft", "none"]
Copy link
Member Author

Choose a reason for hiding this comment

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

Regarding the acceptable values, I'm not a fan of "none", it might be confused with None. Maybe "no-clamping" is better? Regardless, if that's OK with you I think we can leave this (nonetheless important!) bikeshedding for later PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed that None and "none" are bound (ha!) to get confused and that we should definitely choose something else.

Copy link
Member

Choose a reason for hiding this comment

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

Should we consider having possible values be either "hard", "soft" and None (no value provided) instead of having the "none" value as a string?

@scotts
Copy link
Contributor

scotts commented Jun 27, 2025

Adds the clamping_mode parameter to the ClampBoundingBoxes() method. The default here is None, which defaults to the bbox's clamping_mode attribute. @AntoineSimoulin @scotts that's a different default from what we had originally discussed, but I think it makes sense?

As long as the box itself must have a value (and I think that's the case), then I think that makes sense.

Copy link
Contributor

@scotts scotts left a comment

Choose a reason for hiding this comment

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

I think it's a testament to the transform's design that this was so easy to add! This also makes me even more comfortable we made the right design decision, since we don't need to touch the individual transforms.

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.

4 participants