π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fix scalar indexing of unbatched keypoints (#6460)
A Aswanth Rajc18f3d303a28
diff --git a/changelog.d/6460.fixed.md b/changelog.d/6460.fixed.mdnew file mode 100644index 000000000..c1993d082--- /dev/null+++ b/changelog.d/6460.fixed.md@@ -0,0 +1 @@+Selecting one point of an unbatched `Keypoints` or `Keypoints3D` with an integer, including a negative one, or a 0-d integer tensor returns a one-point container of shape `(1, 2)` or `(1, 3)`, a view of the stored tensor. It used to raise a `ValueError`, because tensor indexing dropped the point axis.diff --git a/kornia/geometry/keypoints.py b/kornia/geometry/keypoints.pyindex a9c821d4d..954894353 100644--- a/kornia/geometry/keypoints.py+++ b/kornia/geometry/keypoints.py@@ -53,6 +53,8 @@ class Keypoints:- The constructor and :meth:`from_tensor` wrap a floating-point tensor without copying it. :meth:`pad`,:meth:`unpad`, item assignment and ``index_put(inplace=True)`` write into the stored tensor, so theychange the caller's tensor; call :meth:`clone` first to keep it.+ - Integer indexing selects one batch for batched input, or one point with shape ``(1, 2)`` for unbatched+ input. Scalar integer tensors follow the same rule.- Known defects: list input and ``to_tensor(as_padded_sequence=True)`` raise ``NotImplementedError``(`#5023 <https://github.com/kornia/kornia/issues/5023>`_).@@ -86,7 +88,11 @@ class Keypoints:self._data = keypointsdef __getitem__(self, key: Union[slice, int, torch.torch.Tensor]) -> "Keypoints":- return type(self)(self._data[key], False)+ data = self._data[key]+ if data.ndim == 1 and not isinstance(key, tuple):+ # an integer or 0-d integer tensor selects one point of unbatched keypoints: keep the point axis+ data = data.unsqueeze(0)+ return type(self)(data, False)def __setitem__(self, key: Union[slice, int, torch.torch.Tensor], value: "Keypoints") -> "Keypoints":self._data[key] = value._data@@ -382,6 +388,8 @@ class Keypoints3D:it to ``float32``, or ``float64`` when that is the default dtype. Floating-point input keeps its dtype.Convention:+ - Integer indexing selects one batch for batched input, or one point with shape ``(1, 3)`` for unbatched+ input. Scalar integer tensors follow the same rule.- Known defects: list input and ``to_tensor(as_padded_sequence=True)`` raise ``NotImplementedError``;:meth:`pad` and :meth:`unpad` are deprecated and also raise it(`#5023 <https://github.com/kornia/kornia/issues/5023>`_).@@ -416,7 +424,11 @@ class Keypoints3D:self._data = keypointsdef __getitem__(self, key: Union[slice, int, torch.Tensor]) -> "Keypoints3D":- return type(self)(self._data[key], False)+ data = self._data[key]+ if data.ndim == 1 and not isinstance(key, tuple):+ # an integer or 0-d integer tensor selects one point of unbatched keypoints: keep the point axis+ data = data.unsqueeze(0)+ return type(self)(data, False)def __setitem__(self, key: Union[slice, int, torch.Tensor], value: "Keypoints3D") -> "Keypoints3D":self._data[key] = value._datadiff --git a/tests/geometry/test_keypoints.py b/tests/geometry/test_keypoints.pyindex 4bc927e45..d3176f3e4 100644--- a/tests/geometry/test_keypoints.py+++ b/tests/geometry/test_keypoints.py@@ -17,6 +17,7 @@import pytestimport torch+from torch._dynamo.exc import BackendCompilerFailed, Unsupported, UserErrorimport kornia.augmentation as Kfrom kornia.geometry.keypoints import Keypoints, Keypoints3D, VideoKeypoints@@ -25,6 +26,96 @@ from kornia.geometry.linalg import transform_pointsfrom testing.base import BaseTester, supports_bilinear_2d_grid_sample[email protected]("container,coordinates", [(Keypoints, 2), (Keypoints3D, 3)])+class TestKeypointsIndexing(BaseTester):+ @pytest.mark.parametrize("num_points", [1, 4])+ @pytest.mark.parametrize("key_kind", ["first", "last", "scalar_tensor"])+ @pytest.mark.parametrize("strided", [False, True])+ def test_scalar_unbatched(self, container, coordinates, num_points, key_kind, strided, device, dtype):+ data = torch.arange(num_points * coordinates, device=device, dtype=dtype).reshape(num_points, coordinates)+ if strided:+ data = data.t().contiguous().t()+ key = {"first": 0, "last": -1, "scalar_tensor": torch.tensor(-1, device=device)}[key_kind]+ keypoints = container(data)++ selected = keypoints[key]++ assert type(selected) is container+ assert selected.shape == (1, coordinates)+ assert selected.data.dtype == dtype+ assert selected.data.device == device+ self.assert_close(selected.to_tensor(), data[key].unsqueeze(0), atol=0, rtol=0)+ selected.data.add_(1)+ self.assert_close(keypoints.data[key], selected.data[0], atol=0, rtol=0)+ keypoints[key] = container(torch.full((1, coordinates), 7.0, device=device, dtype=dtype))+ self.assert_close(data[key], torch.full((coordinates,), 7.0, device=device, dtype=dtype))++ @pytest.mark.parametrize("batched", [False, True])+ def test_existing_selection(self, container, coordinates, batched, device, dtype):+ shape = (2, 4, coordinates) if batched else (4, coordinates)+ data = torch.arange(8 * coordinates if batched else 4 * coordinates, device=device, dtype=dtype).reshape(shape)+ keypoints = container(data)+ for key in [+ slice(None, 1),+ slice(0, 0),+ torch.tensor([1, 0], device=device),+ torch.arange(data.shape[0], device=device) % 2 == 0,+ ]:+ self.assert_close(keypoints[key].data, data[key], atol=0, rtol=0)+ if batched:+ self.assert_close(keypoints[0].data, data[0], atol=0, rtol=0)+ empty = container(data[:0])+ assert empty[:].shape == data[:0].shape+ with pytest.raises(IndexError):+ empty[0]++ @pytest.mark.parametrize("key", [(slice(None), 0), (Ellipsis, 0)])+ def test_coordinate_selection_raises(self, container, coordinates, key, device, dtype):+ # one coordinate of every point is not a point, even when there are as many points as coordinates+ data = torch.arange(coordinates * coordinates, device=device, dtype=dtype).reshape(coordinates, coordinates)+ with pytest.raises(ValueError, match="Keypoints shape must be"):+ container(data)[key]++ def test_gradcheck_scalar(self, container, coordinates, device):+ data = torch.arange(3 * coordinates, device=device, dtype=torch.float64).reshape(3, coordinates)+ self.gradcheck(lambda x: container(x)[-1].data, (data.requires_grad_(),))++ @pytest.mark.parametrize("scalar_tensor", [False, True])+ def test_dynamo_scalar(+ self, container, coordinates, scalar_tensor, device, dtype, torch_optimizer, optimizer_backend+ ):+ data = torch.arange(4 * coordinates, device=device, dtype=dtype).reshape(4, coordinates)+ key = torch.tensor(-1, device=device) if scalar_tensor else -1++ def select(x, index):+ return container(x)[index].data++ expected = data[-1:]+ self.assert_close(select(data, key), expected, atol=0, rtol=0)+ compiled = torch_optimizer(select, fullgraph=True)+ try:+ actual = compiled(data, key)+ except UserError as exc:+ if (+ scalar_tensor+ and device.type == "cuda"+ and optimizer_backend == "inductor"+ and "Could not extract specialized integer from data-dependent expression" in str(exc)+ ):+ pytest.xfail("Dynamo cannot specialize a CUDA scalar tensor index, including raw tensor indexing")+ raise+ except Unsupported as exc:+ if torch.__version__.startswith("2.5.") and "UserDefinedClassVariable" in str(exc):+ pytest.xfail("PyTorch 2.5 cannot compile the keypoint type(self) constructor")+ raise+ except BackendCompilerFailed as exc:+ if scalar_tensor and optimizer_backend == "inductor" and "GuardOnDataDependentSymNode" in str(exc):+ pytest.xfail("Inductor cannot specialize a scalar tensor index, including raw tensor indexing")+ raise+ assert actual.shape == (1, coordinates)+ self.assert_close(actual, expected, atol=0, rtol=0)++class TestKeypoints(BaseTester):def test_smoke(self, device, dtype):data = torch.rand(10, 2, device=device, dtype=dtype)