|
21 | 21 | from torch.testing._comparison import BooleanPair, NonePair, not_close_error_metas, NumberPair, TensorLikePair |
22 | 22 | from torchvision import io, tv_tensors |
23 | 23 | 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 |
25 | 25 |
|
26 | 26 |
|
27 | 27 | IN_OSS_CI = any(os.getenv(var) == "true" for var in ["CIRCLECI", "GITHUB_ACTIONS"]) |
@@ -400,6 +400,12 @@ def make_image_pil(*args, **kwargs): |
400 | 400 | return to_pil_image(make_image(*args, **kwargs)) |
401 | 401 |
|
402 | 402 |
|
| 403 | +def make_keypoints(canvas_size=DEFAULT_SIZE, *, num_points=4, dtype=None, device="cpu"): |
| 404 | + y = torch.randint(0, canvas_size[0], size=(num_points, 1), dtype=dtype, device=device) |
| 405 | + x = torch.randint(0, canvas_size[1], size=(num_points, 1), dtype=dtype, device=device) |
| 406 | + return tv_tensors.KeyPoints(torch.cat((x, y), dim=-1), canvas_size=canvas_size) |
| 407 | + |
| 408 | + |
403 | 409 | def make_bounding_boxes( |
404 | 410 | canvas_size=DEFAULT_SIZE, |
405 | 411 | *, |
@@ -461,9 +467,20 @@ def sample_position(values, max_value): |
461 | 467 | parts = (x1, y1, x2, y2, x3, y3, x4, y4) |
462 | 468 | else: |
463 | 469 | 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 | | - ) |
| 470 | + out_boxes = torch.stack(parts, dim=-1).to(dtype=dtype, device=device) |
| 471 | + if tv_tensors.is_rotated_bounding_format(format): |
| 472 | + # The rotated bounding boxes are not guaranteed to be within the canvas by design, |
| 473 | + # so we apply clamping. We also add a 2 buffer to the canvas size to avoid |
| 474 | + # numerical issues during the testing |
| 475 | + buffer = 4 |
| 476 | + out_boxes = clamp_bounding_boxes( |
| 477 | + out_boxes, format=format, canvas_size=(canvas_size[0] - buffer, canvas_size[1] - buffer) |
| 478 | + ) |
| 479 | + if format is tv_tensors.BoundingBoxFormat.XYWHR or format is tv_tensors.BoundingBoxFormat.CXCYWHR: |
| 480 | + out_boxes[:, :2] += buffer // 2 |
| 481 | + elif format is tv_tensors.BoundingBoxFormat.XYXYXYXY: |
| 482 | + out_boxes[:, :] += buffer // 2 |
| 483 | + return tv_tensors.BoundingBoxes(out_boxes, format=format, canvas_size=canvas_size) |
467 | 484 |
|
468 | 485 |
|
469 | 486 | def make_detection_masks(size=DEFAULT_SIZE, *, num_masks=1, dtype=None, device="cpu"): |
|
0 commit comments