Skip to content

Commit aac8e1e

Browse files
Fix failing tests and answer comments
1 parent dffd5ae commit aac8e1e

File tree

4 files changed

+186
-214
lines changed

4 files changed

+186
-214
lines changed

test/common_utils.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from torch.testing._comparison import BooleanPair, NonePair, not_close_error_metas, NumberPair, TensorLikePair
2222
from torchvision import io, tv_tensors
2323
from torchvision.transforms._functional_tensor import _max_value as get_max_value
24-
from torchvision.transforms.v2.functional import to_image, to_pil_image
24+
from torchvision.transforms.v2.functional import clamp_bounding_boxes, to_image, to_pil_image
2525

2626

2727
IN_OSS_CI = any(os.getenv(var) == "true" for var in ["CIRCLECI", "GITHUB_ACTIONS"])
@@ -461,9 +461,20 @@ def sample_position(values, max_value):
461461
parts = (x1, y1, x2, y2, x3, y3, x4, y4)
462462
else:
463463
raise ValueError(f"Format {format} is not supported")
464-
return tv_tensors.BoundingBoxes(
465-
torch.stack(parts, dim=-1).to(dtype=dtype, device=device), format=format, canvas_size=canvas_size
466-
)
464+
out_boxes = torch.stack(parts, dim=-1).to(dtype=dtype, device=device)
465+
if tv_tensors.is_rotated_bounding_format(format):
466+
# The rotated bounding boxes are not guaranteed to be within the canvas by design,
467+
# so we apply clamping. We also add a 2 buffer to the canvas size to avoid
468+
# numerical issues during the testing
469+
buffer = 4
470+
out_boxes = clamp_bounding_boxes(
471+
out_boxes, format=format, canvas_size=(canvas_size[0] - buffer, canvas_size[1] - buffer)
472+
)
473+
if format is tv_tensors.BoundingBoxFormat.XYWHR or format is tv_tensors.BoundingBoxFormat.CXCYWHR:
474+
out_boxes[:, :2] += buffer // 2
475+
elif format is tv_tensors.BoundingBoxFormat.XYXYXYXY:
476+
out_boxes[:, :] += buffer // 2
477+
return tv_tensors.BoundingBoxes(out_boxes, format=format, canvas_size=canvas_size)
467478

468479

469480
def make_detection_masks(size=DEFAULT_SIZE, *, num_masks=1, dtype=None, device="cpu"):

0 commit comments

Comments
 (0)