Skip to content
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

Fix convert_bounding_box_format when passing strings #8265

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions test/test_transforms_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3398,6 +3398,23 @@ def test_transform(self, old_format, new_format, format_type):
make_bounding_boxes(format=old_format),
)

@pytest.mark.parametrize(("old_format", "new_format"), old_new_formats)
def test_strings(self, old_format, new_format):
Copy link
Member Author

Choose a reason for hiding this comment

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

I opted to add a whole new test instead of re-purposing some of the existing ones, because in order for it to be a proper non-regression test, we need to validate the output values here. In main, we can pass strings, there's no failure: but the results are wrong, and the current existing tests cannot assert that properly.

# Non-regression test for https://github.com/pytorch/vision/issues/8258
input = tv_tensors.BoundingBoxes(torch.tensor([[10, 10, 20, 20]]), format=old_format, canvas_size=(50, 50))
expected = self._reference_convert_bounding_box_format(input, new_format)

old_format = old_format.name
new_format = new_format.name

out_functional = F.convert_bounding_box_format(input, new_format=new_format)
out_functional_tensor = F.convert_bounding_box_format(
input.as_subclass(torch.Tensor), old_format=old_format, new_format=new_format
)
out_transform = transforms.ConvertBoundingBoxFormat(new_format)(input)
for out in (out_functional, out_functional_tensor, out_transform):
assert_equal(out, expected)

def _reference_convert_bounding_box_format(self, bounding_boxes, new_format):
return tv_tensors.wrap(
torchvision.ops.box_convert(
Expand Down
4 changes: 1 addition & 3 deletions torchvision/transforms/v2/_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ class ConvertBoundingBoxFormat(Transform):

def __init__(self, format: Union[str, tv_tensors.BoundingBoxFormat]) -> None:
super().__init__()
if isinstance(format, str):
format = tv_tensors.BoundingBoxFormat[format]
self.format = format

def _transform(self, inpt: tv_tensors.BoundingBoxes, params: Dict[str, Any]) -> tv_tensors.BoundingBoxes:
return F.convert_bounding_box_format(inpt, new_format=self.format) # type: ignore[return-value]
return F.convert_bounding_box_format(inpt, new_format=self.format) # type: ignore[return-value, arg-type]


class ClampBoundingBoxes(Transform):
Expand Down
5 changes: 5 additions & 0 deletions torchvision/transforms/v2/functional/_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ def convert_bounding_box_format(
if not torch.jit.is_scripting():
_log_api_usage_once(convert_bounding_box_format)

if isinstance(old_format, str):
Copy link
Member Author

@NicolasHug NicolasHug Feb 8, 2024

Choose a reason for hiding this comment

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

I didn't update the type annotations of convert_bounding_box_format because it would make torchscript unhappy. If mypy fails, I'll silence it. I think it means that torchscript won't support passing strings as the format, but who cares

old_format = BoundingBoxFormat[old_format.upper()]
if isinstance(new_format, str):
new_format = BoundingBoxFormat[new_format.upper()]

if torch.jit.is_scripting() or is_pure_tensor(inpt):
if old_format is None:
raise ValueError("For pure tensor inputs, `old_format` has to be passed.")
Expand Down
Loading