π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(camera): Keep Kannala-Brandt center gradients finite (#5666)
A Aswanth Raj5948cd796090
diff --git a/changelog.d/5666.fixed.md b/changelog.d/5666.fixed.mdnew file mode 100644index 000000000..d1ac88448--- /dev/null+++ b/changelog.d/5666.fixed.md@@ -0,0 +1 @@+`distort_points_kannala_brandt` now returns finite point and camera-parameter gradients at the optical axis, including in mixed batches.diff --git a/kornia/geometry/camera/distortion_kannala_brandt.py b/kornia/geometry/camera/distortion_kannala_brandt.pyindex 7958e1f81..5fb3c1d60 100644--- a/kornia/geometry/camera/distortion_kannala_brandt.py+++ b/kornia/geometry/camera/distortion_kannala_brandt.py@@ -27,6 +27,7 @@ def _distort_points_kannala_brandt_impl(projected_points_in_camera_z1_plane: torch.Tensor,params: torch.Tensor,radius_sq: torch.Tensor,+ nonlinear_mask: torch.Tensor,) -> torch.Tensor:# https://github.com/farm-ng/sophus-rs/blob/20f6cac68f17fe1ac41d0aa8a27489e2b886806f/# src/sensor/kannala_brandt.rs#L51-L67@@ -41,9 +42,12 @@ def _distort_points_kannala_brandt_impl(k2 = params[..., 6]k3 = params[..., 7]- radius = radius_sq.sqrt()+ # Keep the lanes the caller discards finite: torch.where still differentiates both branch tensors.+ # Their radius is moved to about 1 and their angle zeroed. The active lanes add 0 and multiply by 1,+ # so they are unchanged, and this avoids two torch.where calls (slow on MPS).+ radius = (radius_sq + ~nonlinear_mask).sqrt()radius_inverse = 1.0 / radius- theta = radius.atan2(torch.ones_like(radius))+ theta = radius.atan2(torch.ones_like(radius)) * nonlinear_masktheta2 = theta**2theta4 = theta2**2theta6 = theta2 * theta4@@ -95,13 +99,15 @@ def distort_points_kannala_brandt(y = projected_points_in_camera_z1_plane[..., 1]radius_sq = x**2 + y**2+ nonlinear_mask = radius_sq > 1e-8return torch.where(- radius_sq[..., None] > 1e-8,+ nonlinear_mask[..., None],_distort_points_kannala_brandt_impl(projected_points_in_camera_z1_plane,params,radius_sq,+ nonlinear_mask,),distort_points_affine(projected_points_in_camera_z1_plane, params[..., :4]),)diff --git a/tests/geometry/camera/test_distortion.py b/tests/geometry/camera/test_distortion.pyindex 97a6c7e82..f5b1e54ba 100644--- a/tests/geometry/camera/test_distortion.py+++ b/tests/geometry/camera/test_distortion.py@@ -247,6 +247,54 @@ class TestDistortionKannalaBrandt(BaseTester):with pytest.raises(ShapeError):distort_points_kannala_brandt(points, params)+ @pytest.mark.parametrize("point", [(0.0, 0.0), (1e-5, -2e-5)])+ @pytest.mark.parametrize(+ "calibration",+ [+ (100.0, 50.0, 4.0, 3.0, 0.1, 0.01, 0.001, 0.0001),+ (60000.0, 30000.0, 4.0, 3.0, 10.0, 0.0, 0.0, 0.0),+ (65504.0, 65504.0, 4.0, 3.0, 65504.0, 65504.0, 65504.0, 65504.0),+ ],+ )+ def test_distort_affine_branch_gradients(self, device, dtype, point, calibration) -> None:+ points = torch.tensor(point, device=device, dtype=dtype)+ params = torch.tensor(calibration, device=device, dtype=dtype)+ point_jacobian = torch.autograd.functional.jacobian(lambda p: distort_points_kannala_brandt(p, params), points)+ params_jacobian = torch.autograd.functional.jacobian(lambda p: distort_points_kannala_brandt(points, p), params)++ expected_points = torch.diag(params[:2])+ expected_params = torch.zeros(2, 8, device=device, dtype=dtype)+ expected_params[0, 0] = points[0]+ expected_params[1, 1] = points[1]+ expected_params[0, 2] = 1.0+ expected_params[1, 3] = 1.0+ self.assert_close(point_jacobian, expected_points, atol=0.0, rtol=0.0)+ self.assert_close(params_jacobian, expected_params, atol=0.0, rtol=0.0)++ @pytest.mark.parametrize("shared_params", [True, False])+ def test_gradcheck_distort_mixed_optical_axis_batch(self, device, shared_params) -> None:+ points = torch.tensor([[0.0, 0.0], [1e-5, -2e-5], [0.5, 0.25]], device=device, dtype=torch.float64)+ params = torch.tensor([[100.0, 50.0, 4.0, 3.0, 0.1, 0.01, 0.001, 0.0001]], device=device, dtype=torch.float64)+ if not shared_params:+ params = params.repeat(3, 1)+ self.gradcheck(distort_points_kannala_brandt, (points, params))++ def test_dynamo_optical_axis_gradients(self, device, dtype, torch_optimizer) -> None:+ points = torch.zeros(2, device=device, dtype=dtype, requires_grad=True)+ params = torch.tensor(+ [100.0, 50.0, 4.0, 3.0, 0.1, 0.01, 0.001, 0.0001],+ device=device,+ dtype=dtype,+ requires_grad=True,+ )+ output = torch_optimizer(distort_points_kannala_brandt)(points, params)+ point_grad, params_grad = torch.autograd.grad(output.sum(), (points, params))+ self.assert_close(output, params[2:4])+ self.assert_close(point_grad, params[:2])+ self.assert_close(+ params_grad, torch.tensor([0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0], device=device, dtype=dtype)+ )+def _test_gradcheck_distort(self, device):points = torch.tensor([1.0, 2.0], device=device, dtype=torch.float64)params = torch.tensor(