Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): broadcast dx_distort_points_affine over the points (#6091)

Saksham Sharmaafc961c2dc71
diff --git a/changelog.d/6091.fixed.md b/changelog.d/6091.fixed.md
new file mode 100644
index 000000000..9b73aa0b5
--- /dev/null
+++ b/changelog.d/6091.fixed.md
@@ -0,0 +1 @@
+`kornia.geometry.camera.dx_distort_points_affine` returns one Jacobian per point: its batch shape is the broadcast of the points' and the params' leading shapes, as documented and as `distort_points_affine` and `dx_distort_points_kannala_brandt` give. `N` points with one set of params returned a single `(2, 2)` Jacobian, and `(B, N, 2)` points with `(B, 1, 4)` params returned `(B, 1, 2, 2)`. A params batch that does not broadcast against the points now raises, where it was ignored.
diff --git a/kornia/geometry/camera/distortion_affine.py b/kornia/geometry/camera/distortion_affine.py
index 1fb23c9ad..f5d6e9d29 100644
--- a/kornia/geometry/camera/distortion_affine.py
+++ b/kornia/geometry/camera/distortion_affine.py
@@ -125,6 +125,8 @@ def dx_distort_points_affine(projected_points_in_camera_z1_plane: torch.Tensor,
- the result is the :math:`(..., 2, 2)` Jacobian of :func:`distort_points_affine` with respect to the
**point**: rows are the output components ``(u, v)``, columns the input components ``(x, y)``.
- the map is affine, so that Jacobian is ``diag(fx, fy)`` and does not depend on the point.
+ - ``...`` is the broadcast of the points' and the params' leading shapes, as in
+ :func:`distort_points_affine`: ``N`` points with one set of params give ``N`` Jacobians.
Args:
projected_points_in_camera_z1_plane: torch.Tensor representing the points to distort with shape (..., 2).
@@ -145,7 +147,8 @@ def dx_distort_points_affine(projected_points_in_camera_z1_plane: torch.Tensor,
KORNIA_CHECK_SHAPE(projected_points_in_camera_z1_plane, ["*", "2"])
KORNIA_CHECK_SHAPE(params, ["*", "4"])
- fx, fy = params[..., 0], params[..., 1]
+ # Broadcast against the points as well, so a batch of points with shared params gets one Jacobian per point.
+ fx, fy, _ = torch.broadcast_tensors(params[..., 0], params[..., 1], projected_points_in_camera_z1_plane[..., 0])
zeros = torch.zeros_like(fx)
diff --git a/tests/geometry/camera/test_distortion.py b/tests/geometry/camera/test_distortion.py
index 941e86d65..5cb985517 100644
--- a/tests/geometry/camera/test_distortion.py
+++ b/tests/geometry/camera/test_distortion.py
@@ -167,6 +167,49 @@ class TestDistortionAffine(BaseTester):
analytic, torch.tensor([[100.0, 0.0], [0.0, 100.0]], device=device, dtype=dtype), atol=0.0, rtol=0.0
)
+ def test_dx_distort_points_batched_points_shared_params_6090(self, device, dtype):
+ # The Jacobian's shape followed the params only, so N points with one set of params returned a single (2, 2).
+ points = torch.rand(5, 2, device=device, dtype=dtype)
+ params = torch.tensor([600.0, 610.0, 319.5, 239.5], device=device, dtype=dtype)
+
+ jacobian = dx_distort_points_affine(points, params)
+
+ assert jacobian.shape == (5, 2, 2)
+ expected = torch.tensor([[600.0, 0.0], [0.0, 610.0]], device=device, dtype=dtype).expand(5, 2, 2)
+ self.assert_close(jacobian, expected, atol=0.0, rtol=0.0)
+ autograd = torch.stack(
+ [torch.autograd.functional.jacobian(lambda q: distort_points_affine(q, params), point) for point in points]
+ )
+ self.assert_close(jacobian, autograd, atol=0.0, rtol=0.0)
+
+ def test_dx_distort_points_broadcasts_points_and_params_6090(self, device, dtype):
+ points = torch.rand(2, 5, 2, device=device, dtype=dtype)
+ params = torch.tensor(
+ [[[600.0, 610.0, 319.5, 239.5]], [[300.0, 310.0, 159.5, 119.5]]], device=device, dtype=dtype
+ ) # (2, 1, 4)
+
+ jacobian = dx_distort_points_affine(points, params)
+
+ assert jacobian.shape == (2, 5, 2, 2)
+ self.assert_close(jacobian[:, :, 0, 0], params[..., 0].expand(2, 5), atol=0.0, rtol=0.0)
+ self.assert_close(jacobian[:, :, 1, 1], params[..., 1].expand(2, 5), atol=0.0, rtol=0.0)
+ assert (jacobian[..., 0, 1] == 0).all()
+ assert (jacobian[..., 1, 0] == 0).all()
+ assert distort_points_affine(points, params).shape[:-1] == jacobian.shape[:-2]
+
+ def test_dx_distort_points_rejects_mismatched_batches_6090(self, device, dtype):
+ # The points' batch used to be ignored, so a params batch that does not match it went unnoticed.
+ points = torch.rand(5, 2, device=device, dtype=dtype)
+ params = torch.rand(3, 4, device=device, dtype=dtype)
+ with pytest.raises(RuntimeError):
+ dx_distort_points_affine(points, params)
+
+ def test_dx_distort_points_jit_batched_6090(self, device, dtype):
+ points = torch.rand(5, 2, device=device, dtype=dtype)
+ params = torch.tensor([600.0, 610.0, 319.5, 239.5], device=device, dtype=dtype)
+ op_script = torch.jit.script(dx_distort_points_affine)
+ self.assert_close(op_script(points, params), dx_distort_points_affine(points, params))
+
class TestDistortionKannalaBrandt(BaseTester):
def test_smoke(self, device, dtype) -> None: