Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): clarify unimplemented Keypoints error messages (#5122)

Atikul Islam Munna55a6e2077c10
diff --git a/changelog.d/5122.fixed.md b/changelog.d/5122.fixed.md
new file mode 100644
index 000000000..d700724a2
--- /dev/null
+++ b/changelog.d/5122.fixed.md
@@ -0,0 +1,6 @@
+The unimplemented `Keypoints` and `Keypoints3D` paths now raise `NotImplementedError` with a message naming what
+is unsupported instead of a bare one: building either class from a list of tensors,
+`to_tensor(as_padded_sequence=True)` on both, and `Keypoints3D.pad`, `unpad`, `transform_keypoints` and
+`transform_keypoints_`. `Keypoints3D.transform_keypoints` points at
+`kornia.geometry.linalg.transform_points(M, keypoints.data)`, and its `Args` block now documents `M` as
+:math:`(4, 4)` or :math:`(B, 4, 4)` rather than the 2D :math:`(3, 3)` shape. The paths remain unimplemented.
diff --git a/kornia/geometry/keypoints.py b/kornia/geometry/keypoints.py
index 9c02d4181..e016af680 100644
--- a/kornia/geometry/keypoints.py
+++ b/kornia/geometry/keypoints.py
@@ -26,7 +26,10 @@ __all__ = ["Keypoints", "Keypoints3D"]
def _merge_keypoint_list(keypoints: List[torch.torch.Tensor]) -> torch.torch.Tensor:
- raise NotImplementedError
+ raise NotImplementedError(
+ "Building `Keypoints` or `Keypoints3D` from a list of tensors is not implemented; pass one tensor of "
+ "shape `(N, 2)` or `(B, N, 2)` for `Keypoints`, `(N, 3)` or `(B, N, 3)` for `Keypoints3D` (kornia#5023)."
+ )
class Keypoints:
@@ -244,7 +247,11 @@ class Keypoints:
"""
if as_padded_sequence:
- raise NotImplementedError
+ raise NotImplementedError(
+ "`Keypoints.to_tensor(as_padded_sequence=True)` is not implemented; it needs the keypoint-list "
+ "input that `Keypoints` does not support. Call `to_tensor()` for the `(N, 2)` or `(B, N, 2)` "
+ "tensor instead (kornia#5023)."
+ )
return self._data
def clone(self) -> "Keypoints":
@@ -374,7 +381,7 @@ class Keypoints3D:
padding_size: (B, 6)
"""
- raise NotImplementedError
+ raise NotImplementedError("`Keypoints3D.pad` is not implemented (kornia#5023).")
def unpad(self, padding_size: torch.torch.Tensor) -> "Keypoints3D":
"""Pad a bounding keypoints.
@@ -383,23 +390,27 @@ class Keypoints3D:
padding_size: (B, 6)
"""
- raise NotImplementedError
+ raise NotImplementedError("`Keypoints3D.unpad` is not implemented (kornia#5023).")
def transform_keypoints(self, M: torch.Tensor, inplace: bool = False) -> "Keypoints3D":
- r"""Apply a transformation matrix to the 2D keypoints.
+ r"""Apply a transformation matrix to the 3D keypoints.
Args:
- M: The transformation matrix to be applied, shape of :math:`(3, 3)` or :math:`(B, 3, 3)`.
+ M: The transformation matrix to be applied, shape of :math:`(4, 4)` or :math:`(B, 4, 4)`.
inplace: do transform in-place and return self.
Returns:
The transformed keypoints.
"""
- raise NotImplementedError
+ raise NotImplementedError(
+ "`Keypoints3D.transform_keypoints` is not implemented; use "
+ "`kornia.geometry.linalg.transform_points(M, keypoints.data)` with `M` of shape `(B, 4, 4)` matching "
+ "the batch of `data`, or `(1, 4, 4)` when `data` is `(N, 3)` (kornia#5023)."
+ )
def transform_keypoints_(self, M: torch.Tensor) -> "Keypoints3D":
- """Inplace version of :func:`Keypoints.transform_keypoints`."""
+ """Inplace version of :func:`Keypoints3D.transform_keypoints`."""
return self.transform_keypoints(M, inplace=True)
@classmethod
@@ -429,7 +440,11 @@ class Keypoints3D:
"""
if as_padded_sequence:
- raise NotImplementedError
+ raise NotImplementedError(
+ "`Keypoints3D.to_tensor(as_padded_sequence=True)` is not implemented; it needs the keypoint-list "
+ "input that `Keypoints3D` does not support. Call `to_tensor()` for the `(N, 3)` or `(B, N, 3)` "
+ "tensor instead (kornia#5023)."
+ )
return self._data
def clone(self) -> "Keypoints3D":
diff --git a/tests/geometry/test_keypoints.py b/tests/geometry/test_keypoints.py
index 4734cae34..62a7ca8b5 100644
--- a/tests/geometry/test_keypoints.py
+++ b/tests/geometry/test_keypoints.py
@@ -183,6 +183,13 @@ class TestKeypoints(BaseTester):
kp.type(torch.float64)
assert kp.dtype == torch.float64
+ def test_not_implemented(self, device, dtype):
+ kp = Keypoints(torch.rand(2, 5, 2, device=device, dtype=dtype))
+ with pytest.raises(NotImplementedError, match="from a list of tensors is not implemented"):
+ Keypoints([torch.rand(3, 2, device=device, dtype=dtype), torch.rand(5, 2, device=device, dtype=dtype)])
+ with pytest.raises(NotImplementedError, match=r"`Keypoints\.to_tensor\(as_padded_sequence=True\)`"):
+ kp.to_tensor(as_padded_sequence=True)
+
def test_exception(self, device, dtype):
with pytest.raises(TypeError):
Keypoints("not a tensor")
@@ -369,12 +376,18 @@ class TestKeypoints3D(BaseTester):
def test_not_implemented(self, device, dtype):
kp = Keypoints3D(torch.rand(5, 3, device=device, dtype=dtype))
- with pytest.raises(NotImplementedError):
+ with pytest.raises(NotImplementedError, match=r"`Keypoints3D\.pad` is not implemented"):
kp.pad(torch.zeros(1, 6, device=device, dtype=dtype))
- with pytest.raises(NotImplementedError):
+ with pytest.raises(NotImplementedError, match=r"`Keypoints3D\.unpad` is not implemented"):
kp.unpad(torch.zeros(1, 6, device=device, dtype=dtype))
- with pytest.raises(NotImplementedError):
+ with pytest.raises(NotImplementedError, match="transform_points"):
kp.transform_keypoints(torch.eye(4, device=device, dtype=dtype))
+ with pytest.raises(NotImplementedError, match="transform_points"):
+ kp.transform_keypoints_(torch.eye(4, device=device, dtype=dtype))
+ with pytest.raises(NotImplementedError, match="from a list of tensors is not implemented"):
+ Keypoints3D([torch.rand(3, 3, device=device, dtype=dtype), torch.rand(5, 3, device=device, dtype=dtype)])
+ with pytest.raises(NotImplementedError, match=r"`Keypoints3D\.to_tensor\(as_padded_sequence=True\)`"):
+ kp.to_tensor(as_padded_sequence=True)
def test_exception(self, device, dtype):
with pytest.raises(TypeError):