From 89b851e64c7af3a13766462280597a9d06bf9ae7 Mon Sep 17 00:00:00 2001 From: Roman Shapovalov Date: Mon, 6 Jan 2025 04:17:57 -0800 Subject: [PATCH] Refactor a utility function for bbox conversion Summary: This function makes it easier to extend FrameData class with new channels; brushing it up a bit. Reviewed By: bottler Differential Revision: D67816470 fbshipit-source-id: 6575415c864d0f539e283889760cd2331bf226a7 --- pytorch3d/implicitron/dataset/utils.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/pytorch3d/implicitron/dataset/utils.py b/pytorch3d/implicitron/dataset/utils.py index cb8828eb..b4443584 100644 --- a/pytorch3d/implicitron/dataset/utils.py +++ b/pytorch3d/implicitron/dataset/utils.py @@ -134,7 +134,15 @@ T = TypeVar("T", bound=torch.Tensor) def bbox_xyxy_to_xywh(xyxy: T) -> T: wh = xyxy[2:] - xyxy[:2] xywh = torch.cat([xyxy[:2], wh]) - return xywh # pyre-ignore + return xywh # pyre-ignore[7] + + +def bbox_xywh_to_xyxy(xywh: T, clamp_size: float | int | None = None) -> T: + wh = xywh[2:] + if clamp_size is not None: + wh = wh.clamp(min=clamp_size) + xyxy = torch.cat([xywh[:2], xywh[:2] + wh]) + return xyxy # pyre-ignore[7] def get_clamp_bbox( @@ -180,16 +188,6 @@ def rescale_bbox( return bbox * rel_size -def bbox_xywh_to_xyxy( - xywh: torch.Tensor, clamp_size: Optional[int] = None -) -> torch.Tensor: - xyxy = xywh.clone() - if clamp_size is not None: - xyxy[2:] = torch.clamp(xyxy[2:], clamp_size) - xyxy[2:] += xyxy[:2] - return xyxy - - def get_1d_bounds(arr: np.ndarray) -> Tuple[int, int]: nz = np.flatnonzero(arr) return nz[0], nz[-1] + 1