π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): make bbox_to_mask3d preserve the input dtype (#4250) (#4376)
li-lizhe5bcc205ed3d7
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c8b22d68c..a0f1511f3 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -404,6 +404,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Bug fixes+* `bbox_to_mask3d` now preserves the input dtype in the returned mask, matching `bbox_to_mask`+ and the `Boxes3D.to_mask` contract: the earlier implementation downcast every result to+ `float32`, so a `float64` or half-precision box produced a mask that silently lost precision.+ The mask keeps the input box's dtype end to end. (#4376)* Fixed `RandomPlasmaBrightness`, `RandomPlasmaContrast`, and `RandomPlasmaShadow` to replay deterministically from stored `params=`. (#4462)* `ManyToManyAugmentationDispather` now rejects mismatched numbers of input bundles andaugmentations before applying any transformation, instead of silently dropping surplus inputsdiff --git a/kornia/geometry/bbox.py b/kornia/geometry/bbox.pyindex a48f8d2d0..f7a4c98bb 100644--- a/kornia/geometry/bbox.py+++ b/kornia/geometry/bbox.py@@ -387,9 +387,9 @@ def bbox_to_mask3d(boxes: torch.Tensor, size: tuple[int, int, int]) -> torch.Tenr"""Convert 3D bounding boxes to masks. Covered area is 1. and the remaining is 0.Convention:- ``size`` is ``(depth, height, width)`` and the mask comes back as :math:`(B, 1, depth, height, width)` in- ``float32`` whatever the input dtype, unlike :func:`bbox_to_mask`, which keeps the input dtype and has no- channel axis, and :meth:`kornia.geometry.boxes.Boxes3D.to_mask`, which keeps the box dtype and returns+ ``size`` is ``(depth, height, width)`` and the mask comes back as :math:`(B, 1, depth, height, width)` in the+ input dtype, like :func:`bbox_to_mask`, which keeps the input dtype and has no channel axis, and+ :meth:`kornia.geometry.boxes.Boxes3D.to_mask`, which keeps the box dtype and returns:math:`(N, depth, height, width)`. After :func:`validate_bbox3d`, which raises ``AssertionError`` for aninvalid box, the bounds are read from fixed vertex positions, truncated toward zero with ``.long()``, andcompared inclusively, which reads the vertices as inclusive: pass the ``'vertices_plus'`` export. The@@ -400,8 +400,7 @@ def bbox_to_mask3d(boxes: torch.Tensor, size: tuple[int, int, int]) -> torch.TenThe truncation differs from the inclusiveraw-float comparison of :func:`bbox_to_mask` and the rounding of:meth:`~kornia.geometry.boxes.Boxes3D.to_mask` for fractional coordinates and is tracked in- `#4015 <https://github.com/kornia/kornia/issues/4015>`_. The ``float32`` output with a channel axis is- tracked in `#4250 <https://github.com/kornia/kornia/issues/4250>`_. Validation raises rather than returning+ `#4015 <https://github.com/kornia/kornia/issues/4015>`_. Validation raises rather than returning``False`` for a shape or extent failure, `#4013 <https://github.com/kornia/kornia/issues/4013>`_; anon-finite coordinate is reported as ``False`` by :func:`validate_bbox3d` and raised here too.Batched :math:`(B, N, 8, 3)` input passes@@ -416,7 +415,7 @@ def bbox_to_mask3d(boxes: torch.Tensor, size: tuple[int, int, int]) -> torch.Tensize: depth, height and width of the masked image.Returns:- the output mask tensor, shape of :math:`(B, 1, depth, height, width)` and dtype ``float32``.+ the output mask tensor, shape of :math:`(B, 1, depth, height, width)` and dtype of ``boxes``.Examples:>>> boxes = torch.tensor([[@@ -484,7 +483,7 @@ def bbox_to_mask3d(boxes: torch.Tensor, size: tuple[int, int, int]) -> torch.Ten& ((y[None, :] >= y_min[:, None]) & (y[None, :] <= y_max[:, None]))[:, None, None, :, None]& ((x[None, :] >= x_min[:, None]) & (x[None, :] <= x_max[:, None]))[:, None, None, None, :]) # Shape: (N, 1, D0, D1, D2)- return m.float()+ return m.to(boxes.dtype)def bbox_generator(diff --git a/tests/geometry/test_bbox.py b/tests/geometry/test_bbox.pyindex 11e3f5659..f358b3a01 100644--- a/tests/geometry/test_bbox.py+++ b/tests/geometry/test_bbox.py@@ -747,19 +747,19 @@ class TestBbox3D(BaseTester):device=device,dtype=dtype,)- expected = torch.zeros(1, 1, 5, 5, 6, device=device, dtype=torch.float32)+ expected = torch.zeros(1, 1, 5, 5, 6, device=device, dtype=dtype)expected[0, 0, 1:3, 1:3, 1:4] = 1.0self.assert_close(bbox_to_mask3d(boxes, (5, 5, 6)), expected, atol=0.0, rtol=0.0)- def test_wart_bbox_to_mask3d_returns_float32_with_a_channel_axis_4250(self, device, dtype):- # Wart pin for kornia#4250: the 3D free function returns float32 (B, 1, D, H, W) whatever the input dtype,- # while bbox_to_mask keeps the input dtype and returns (B, H, W). Neither carries a gradient.+ def test_bbox_to_mask3d_preserves_input_dtype_4250(self, device, dtype):+ # kornia#4250: bbox_to_mask3d now returns a mask in the input dtype with a (B, 1, D, H, W) channel axis,+ # matching bbox_to_mask (which keeps the input dtype, no channel axis). Neither carries a gradient.cuboid = self._unit_cuboid(device, dtype)mask = bbox_to_mask3d(cuboid, (4, 5, 6))assert mask.shape == (1, 1, 4, 5, 6)- assert mask.dtype == torch.float32+ assert mask.dtype == dtypeassert bbox_to_mask3d(cuboid.repeat(2, 1, 1), (4, 5, 6)).shape == (2, 1, 4, 5, 6)- assert bbox_to_mask3d(cuboid.to(torch.int64), (4, 5, 6)).dtype == torch.float32+ assert bbox_to_mask3d(cuboid.to(torch.int64), (4, 5, 6)).dtype == torch.int64assert not bbox_to_mask3d(cuboid.clone().requires_grad_(), (4, 5, 6)).requires_gradsquare = torch.tensor([[[1.0, 1.0], [3.0, 1.0], [3.0, 2.0], [1.0, 2.0]]], device=device, dtype=dtype)assert bbox_to_mask(square, 6, 5).dtype == dtype@@ -793,7 +793,7 @@ class TestBbox3D(BaseTester):boxes = Boxes3D.from_tensor(torch.tensor([[0.0, 1.0, 1.0, 4.0, 2.0, 2.0]], device=device, dtype=dtype), mode="xyzxyz_plus")- expected = torch.zeros(1, 1, 4, 4, 5, device=device, dtype=torch.float32)+ expected = torch.zeros(1, 1, 4, 4, 5, device=device, dtype=dtype)expected[0, 0, 1:3, 1:3, :] = 1.0self.assert_close(bbox_to_mask3d(boxes.data, (4, 4, 5)), expected, atol=0.0, rtol=0.0)