Skip to content

Commit eee56d8

Browse files
Update pad_bounding_boxes for rotated boxes
Test Plan: ```bash pytest test/test_transforms_v2.py -vvv -k "TestPad and test_kernel_bounding_boxes" pytest test/test_transforms_v2.py -vvv -k "TestPad and test_bounding_boxes_correctness" ```
1 parent a28fa39 commit eee56d8

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

test/test_transforms_v2.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,16 @@ def affine_rotated_bounding_boxes(bounding_boxes):
608608
output, old_format=tv_tensors.BoundingBoxFormat.XYXYXYXY, new_format=format
609609
)
610610

611+
if torch.is_floating_point(output) and dtype in (
612+
torch.uint8,
613+
torch.int8,
614+
torch.int16,
615+
torch.int32,
616+
torch.int64,
617+
):
618+
# it is better to round before cast
619+
output = torch.round(output)
620+
611621
if clamp:
612622
# It is important to clamp before casting, especially for CXCYWHR format, dtype=int64
613623
output = F.clamp_bounding_boxes(
@@ -3934,7 +3944,7 @@ def test_kernel_image(self, param, value, dtype, device):
39343944
),
39353945
)
39363946

3937-
@pytest.mark.parametrize("format", SUPPORTED_BOX_FORMATS)
3947+
@pytest.mark.parametrize("format", list(tv_tensors.BoundingBoxFormat))
39383948
def test_kernel_bounding_boxes(self, format):
39393949
bounding_boxes = make_bounding_boxes(format=format)
39403950
check_kernel(
@@ -4054,12 +4064,15 @@ def _reference_pad_bounding_boxes(self, bounding_boxes, *, padding):
40544064
height = bounding_boxes.canvas_size[0] + top + bottom
40554065
width = bounding_boxes.canvas_size[1] + left + right
40564066

4057-
return reference_affine_bounding_boxes_helper(
4058-
bounding_boxes, affine_matrix=affine_matrix, new_canvas_size=(height, width)
4067+
helper = (
4068+
reference_affine_rotated_bounding_boxes_helper
4069+
if tv_tensors.is_rotated_bounding_format(bounding_boxes.format)
4070+
else reference_affine_bounding_boxes_helper
40594071
)
4072+
return helper(bounding_boxes, affine_matrix=affine_matrix, new_canvas_size=(height, width))
40604073

40614074
@pytest.mark.parametrize("padding", CORRECTNESS_PADDINGS)
4062-
@pytest.mark.parametrize("format", SUPPORTED_BOX_FORMATS)
4075+
@pytest.mark.parametrize("format", list(tv_tensors.BoundingBoxFormat))
40634076
@pytest.mark.parametrize("dtype", [torch.int64, torch.float32])
40644077
@pytest.mark.parametrize("device", cpu_and_cuda())
40654078
@pytest.mark.parametrize("fn", [F.pad, transform_cls_to_functional(transforms.Pad)])
@@ -4069,7 +4082,7 @@ def test_bounding_boxes_correctness(self, padding, format, dtype, device, fn):
40694082
actual = fn(bounding_boxes, padding=padding)
40704083
expected = self._reference_pad_bounding_boxes(bounding_boxes, padding=padding)
40714084

4072-
assert_equal(actual, expected)
4085+
torch.testing.assert_close(actual, expected)
40734086

40744087

40754088
class TestCenterCrop:

torchvision/transforms/v2/functional/_geometry.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,11 @@ def pad_bounding_boxes(
14581458

14591459
left, right, top, bottom = _parse_pad_padding(padding)
14601460

1461-
if format == tv_tensors.BoundingBoxFormat.XYXY:
1461+
if format == tv_tensors.BoundingBoxFormat.XYXYXYXY:
1462+
pad = [left, top, left, top, left, top, left, top]
1463+
elif format == tv_tensors.BoundingBoxFormat.XYWHR or format == tv_tensors.BoundingBoxFormat.CXCYWHR:
1464+
pad = [left, top, 0, 0, 0]
1465+
elif format == tv_tensors.BoundingBoxFormat.XYXY:
14621466
pad = [left, top, left, top]
14631467
else:
14641468
pad = [left, top, 0, 0]

0 commit comments

Comments
 (0)