π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): refuse 3D box crops that no affine map relates (#6517)
Rahul Seerviff6774d332d9
diff --git a/changelog.d/6517.fixed.md b/changelog.d/6517.fixed.mdnew file mode 100644index 000000000..f54b0793b--- /dev/null+++ b/changelog.d/6517.fixed.md@@ -0,0 +1 @@+`crop_and_resize3d` and `crop_by_boxes3d` now raise `AssertionError` for a pair of boxes that no affine map relates, such as a box that is not a parallelepiped in `crop_and_resize3d`, instead of cropping it with a mapping that misses its corners. Every pair they accept crops as before.diff --git a/kornia/geometry/transform/crop3d.py b/kornia/geometry/transform/crop3d.pyindex ba96a82f7..b98c033f6 100644--- a/kornia/geometry/transform/crop3d.py+++ b/kornia/geometry/transform/crop3d.py@@ -26,7 +26,8 @@ starts at voxel ``(0, 0, 0)``. Pass ``align_corners=True``: at ``False`` the matvoxels while ``grid_sample`` reads half-voxel ones, so the samples miss the box, and even a box of the output size isinterpolated rather than copied (`#4503 <https://github.com/kornia/kornia/issues/4503>`_). :func:`crop_and_resize3d`and :func:`crop_by_boxes3d` default to ``False``. In eager mode the box crops reject, with ``AssertionError``, a box-whose four edges along an axis span different extents on it, or one with a non-finite coordinate. They raise+whose four edges along an axis span different extents on it, one with a non-finite coordinate, and a pair of boxes+that no affine map relates, such as a box that is not a parallelepiped in :func:`crop_and_resize3d`. They raise``torch.linalg.LinAlgError`` for a box or an output size with a one-voxel axis(`#4747 <https://github.com/kornia/kornia/issues/4747>`_).@@ -44,6 +45,7 @@ from typing import Tupleimport torch+from kornia.core.utils import is_compilingfrom kornia.geometry.bbox import _validate_bbox3d, infer_bbox_shape3dfrom .imgwarp import get_perspective_transform3d, warp_affine3d@@ -62,9 +64,8 @@ def crop_and_resize3d(``patches = crop_and_resize3d(tensor, boxes, size)`` takes volumes ``(B, C, D, H, W)`` and boxes ``(B, 8, 3)``, oneper volume or ``(1, 8, 3)`` for all of them, and returns the ``(B, C, d, h, w)`` patches for ``size = (d, h, w)``.- With ``align_corners=True`` the corners of a parallelepiped box land on the corner voxels of the patch; the warp- keeps only the top three rows of the 3D homography between the two, so the corners of any other box miss- (`#6436 <https://github.com/kornia/kornia/issues/6436>`_). Pass ``align_corners=True``, since the default+ Each box must be a parallelepiped, so that an affine map takes it onto the patch; with ``align_corners=True`` its+ corners land on the corner voxels of the patch. Pass ``align_corners=True``, since the default``False`` samples off the box (`#4503 <https://github.com/kornia/kornia/issues/4503>`_). For images, use:func:`crop_and_resize`; to place the crop by a second box, :func:`crop_by_boxes3d`.@@ -95,8 +96,8 @@ def crop_and_resize3d(tensor containing the patches with shape ``(B, C, d, h, w)``.Raises:- AssertionError: if a box has a non-finite coordinate, or edges along one axis that span different extents on- it.+ AssertionError: if a box has a non-finite coordinate, edges along one axis that span different extents on+ it, or is not a parallelepiped.See Also:- :func:`crop_and_resize`: the same for images and ``(B, 4, 2)`` boxes, where, along an axis longer than one@@ -340,12 +341,10 @@ def crop_by_boxes3d(r"""Crop volumes by mapping a source box onto a destination box, whose size sets the output size.``patches = crop_by_boxes3d(tensor, src_box, dst_box)`` takes volumes ``(B, C, D, H, W)`` and two boxes per- volume, ``(B, 8, 3)`` each (both ``(1, 8, 3)`` for all volumes), warps each volume by the top three rows of the- 3D homography computed from the corners of ``src_box`` and ``dst_box``, which takes every corner onto its- destination only when an affine map relates the two boxes, as it relates any two parallelepipeds- (`#6436 <https://github.com/kornia/kornia/issues/6436>`_), and returns the ``(B, C, d, h, w)`` window that- starts at voxel ``(0, 0, 0)``, where- ``(d, h, w)`` is the inclusive size of ``dst_box``; every ``dst_box`` in the batch must have that size. Pass+ volume, ``(B, 8, 3)`` each (both ``(1, 8, 3)`` for all volumes), warps each volume by the affine map that takes+ the corners of ``src_box`` onto those of ``dst_box``, which must exist, as it does for any two parallelepipeds or+ two translated copies of one box, and returns the ``(B, C, d, h, w)`` window that starts at voxel ``(0, 0, 0)``,+ where ``(d, h, w)`` is the inclusive size of ``dst_box``; every ``dst_box`` in the batch must have that size. Pass``align_corners=True``: the default ``False`` samples off the box(`#4503 <https://github.com/kornia/kornia/issues/4503>`_). When the output size is known, :func:`crop_and_resize3d`takes it instead of a second box; for images, use :func:`crop_by_boxes`.@@ -375,8 +374,8 @@ def crop_by_boxes3d(the patches, with shape ``(B, C, d, h, w)`` for the size ``(d, h, w)`` of ``dst_box``.Raises:- AssertionError: if the boxes of ``dst_box`` differ in size, or a box has a non-finite coordinate or edges along- one axis that span different extents on it.+ AssertionError: if the boxes of ``dst_box`` differ in size, a box has a non-finite coordinate or edges along+ one axis that span different extents on it, or no affine map takes a ``src_box`` onto its ``dst_box``.See Also:- :func:`crop_by_boxes`: the same for images and ``(B, 4, 2)`` boxes.@@ -459,7 +458,9 @@ def _crop_by_boxes3d_to_size(# compute transformation between points and warp# Note: Tensor.dtype must be float. "solve_cpu" not implemented for 'Long'- dst_trans_src: torch.Tensor = get_perspective_transform3d(src_box.to(tensor.dtype), dst_box.to(tensor.dtype))+ src_box, dst_box = src_box.to(tensor.dtype), dst_box.to(tensor.dtype)+ dst_trans_src: torch.Tensor = get_perspective_transform3d(src_box, dst_box)+ _check_boxes_related_by_affine(src_box, dst_box, dst_trans_src)# simulate broadcastingdst_trans_src = dst_trans_src.expand(tensor.shape[0], -1, -1).type_as(tensor)@@ -470,6 +471,33 @@ def _crop_by_boxes3d_to_size(return patches+def _check_boxes_related_by_affine(src_box: torch.Tensor, dst_box: torch.Tensor, dst_trans_src: torch.Tensor) -> None:+ # The crops warp by the top three rows of the box homography, an affine map, so they place the 8 corners of+ # ``src_box`` on those of ``dst_box`` only when an affine map relates the two boxes, as one relates any two+ # parallelepipeds. Refuse every other pair instead of cropping it with a wrong mapping. Mapping a corner sums+ # terms of up to ``scale`` voxels, rounded to the matrix dtype, so a pair passes when no corner misses by more+ # than ``sqrt(eps)`` of that, with the float32 ``eps`` for float64: a parallelepiped whose corners are rounded to+ # the dtype misses by up to several hundred ``eps`` of it far from the origin, where the fitted homography+ # amplifies that rounding, still a few times below the tolerance; boxes off a parallelepiped by as much as the+ # extents ``_validate_bbox3d`` lets differ (``rtol=1e-5``), such as a gradcheck step, still pass, and moving a+ # single corner of a small box by one voxel misses by far more. Like the value checks of ``_validate_bbox3d``,+ # this one reads the data back and is skipped under graph capture.+ if not torch.jit.is_scripting() and is_compiling():+ return+ work_dtype = torch.promote_types(dst_trans_src.dtype, torch.float32)+ mat = dst_trans_src[:, :3].detach().to(work_dtype)+ src, dst = src_box.detach().to(work_dtype), dst_box.detach().to(work_dtype)+ linear, translation = mat[..., :3].transpose(-1, -2), mat[..., 3:].transpose(-1, -2)+ miss = (src @ linear + translation - dst).abs().amax((-2, -1))+ scale = (src.abs() @ linear.abs() + translation.abs() + dst.abs()).amax((-2, -1))+ tolerance = max(torch.finfo(dst_trans_src.dtype).eps, torch.finfo(torch.float32).eps) ** 0.5 * scale+ if bool((miss > tolerance).any()):+ raise AssertionError(+ "Boxes must be related by an affine map, as two parallelepipeds are: the affine map the crop warps by "+ f"misses the corners of dst_box by up to {miss.max().item():.3g} voxels."+ )++def crop_by_transform_mat3d(tensor: torch.Tensor,transform: torch.Tensor,diff --git a/tests/geometry/transform/test_crop3d.py b/tests/geometry/transform/test_crop3d.pyindex 61f40934a..3f015b1ce 100644--- a/tests/geometry/transform/test_crop3d.py+++ b/tests/geometry/transform/test_crop3d.py@@ -531,3 +531,80 @@ class TestCrop3DSizeValidation:inp = torch.rand(1, 1, 4, 4, 4, device=device, dtype=dtype)with pytest.raises(ValueError, match="tuple/list of length 3"):kornia.geometry.transform.center_crop3d(inp, (2, 2))+++class TestCrop3DAffineBoxes(BaseTester):+ # The box crops warp by an affine map, so they take only box pairs that an affine map relates (#6436).++ @staticmethod+ def _coordinate_volume(device, dtype):+ # each voxel stores its own (x, y, z), so a patch voxel reads the source point it was sampled at+ z, y, x = torch.meshgrid(*(torch.arange(10, device=device, dtype=dtype) for _ in range(3)), indexing="ij")+ return torch.stack([x, y, z])[None]++ @staticmethod+ def _box(origin, edges, device, dtype):+ # the parallelepiped origin + i * edges[0] + j * edges[1] + k * edges[2], in bbox_generator3d corner order+ unit = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 0, 1), (1, 0, 1), (1, 1, 1), (0, 1, 1)]+ origin_t = torch.tensor(origin, device=device, dtype=dtype)+ edges_t = torch.tensor(edges, device=device, dtype=dtype)+ weights = torch.tensor(unit, device=device, dtype=dtype)+ return (origin_t + weights @ edges_t)[None]++ def _skewed_box(self, device, dtype, corners=(1, 3), shift=1.0):+ # the box of #6436: an axis-aligned box with corners 1 and 3 raised by one voxel in y. Its edges along each+ # axis still span equal extents, so _validate_bbox3d accepts it, but it is not a parallelepiped. Raising+ # corners 4 and 6 instead keeps the corners the homography is solved from, 0, 1, 2, 5 and 7, on a cube.+ box = self._box((2, 2, 2), ((3, 0, 0), (0, 3, 0), (0, 0, 3)), device, dtype)+ for corner in corners:+ box[0, corner, 1] += shift+ return box++ @pytest.mark.parametrize("corners", [(1, 3), (4, 6)])+ def test_crop_and_resize3d_rejects_a_box_that_is_not_a_parallelepiped_6436(self, corners, device, dtype):+ volume = self._coordinate_volume(device, dtype)+ skewed = self._skewed_box(device, dtype, corners)+ with pytest.raises(AssertionError, match="affine map"):+ kornia.geometry.transform.crop_and_resize3d(volume, skewed, (5, 5, 5), align_corners=True)++ # one such box in a batch refuses the call+ cube = self._box((2, 2, 2), ((3, 0, 0), (0, 3, 0), (0, 0, 3)), device, dtype)+ with pytest.raises(AssertionError, match="affine map"):+ kornia.geometry.transform.crop_and_resize3d(+ volume.expand(2, -1, -1, -1, -1), torch.cat([cube, skewed]), (5, 5, 5), align_corners=True+ )++ @pytest.mark.parametrize("corners", [(1, 3), (4, 6)])+ def test_crop_and_resize3d_rejects_a_sub_voxel_skew_6436(self, corners, device, dtype):+ if dtype in (torch.float16, torch.bfloat16):+ pytest.skip("the half-precision tolerance admits a 0.1-voxel skew of a box this size")+ volume = self._coordinate_volume(device, dtype)+ skewed = self._skewed_box(device, dtype, corners, shift=0.1)+ with pytest.raises(AssertionError, match="affine map"):+ kornia.geometry.transform.crop_and_resize3d(volume, skewed, (4, 4, 4), align_corners=True)++ def test_crop_and_resize3d_takes_a_sheared_parallelepiped_onto_the_patch_corners_6436(self, device, dtype):+ volume = self._coordinate_volume(device, dtype)+ # _validate_bbox3d takes a parallelepiped whose first edge has no y component+ box = self._box((2, 2, 2), ((4, 0, 1), (1, 4, 0), (0, 1, 3)), device, dtype)+ patch = kornia.geometry.transform.crop_and_resize3d(volume, box, (4, 5, 5), align_corners=True)[0]+ corners = [(0, 0, 0), (0, 0, -1), (0, -1, -1), (0, -1, 0), (-1, 0, 0), (-1, 0, -1), (-1, -1, -1), (-1, -1, 0)]+ sampled = torch.stack([patch[:, k, j, i] for k, j, i in corners])+ self.assert_close(sampled, box[0])++ def test_crop_by_boxes3d_takes_translated_copies_of_any_box_6436(self, device, dtype):+ volume = self._coordinate_volume(device, dtype)+ skewed = self._skewed_box(device, dtype)+ patch = kornia.geometry.transform.crop_by_boxes3d(+ volume, skewed, skewed - 2, interpolation="nearest", align_corners=True+ )+ # a translation by (-2, -2, -2): the window of the size crop_by_boxes3d infers from dst_box, (4, 4, 4), read+ # from the source starting at voxel (2, 2, 2)+ self.assert_close(patch, volume[..., 2:6, 2:6, 2:6])++ def test_crop_by_boxes3d_rejects_boxes_no_affine_map_relates_6436(self, device, dtype):+ volume = self._coordinate_volume(device, dtype)+ cube = self._box((2, 2, 2), ((3, 0, 0), (0, 3, 0), (0, 0, 3)), device, dtype)+ skewed = self._skewed_box(device, dtype)+ with pytest.raises(AssertionError, match="affine map"):+ kornia.geometry.transform.crop_by_boxes3d(volume, cube, skewed - 2, align_corners=True)