π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
perf(geometry): reduce SVD storage for point solvers (#5786)
Karnajeet Gosavid914ec5d95f0
diff --git a/changelog.d/5786.fixed.md b/changelog.d/5786.fixed.mdnew file mode 100644index 000000000..7e46bcd34--- /dev/null+++ b/changelog.d/5786.fixed.md@@ -0,0 +1 @@+`solve_pnp_dlt` with more than six points, and `run_5point` and `find_essential` with more than nine, no longer build the full square matrix of left singular vectors of their linear system, which they never used (2N-by-2N for N points in `solve_pnp_dlt`, N-by-N in the other two). Their memory now grows linearly with N instead of quadratically, with or without autograd, and large point sets run much faster.diff --git a/kornia/geometry/calibration/pnp.py b/kornia/geometry/calibration/pnp.pyindex fd637e3e0..3a3acd513 100644--- a/kornia/geometry/calibration/pnp.py+++ b/kornia/geometry/calibration/pnp.py@@ -241,7 +241,8 @@ def solve_pnp_dlt(system = weights_expanded * system# Getting the solution vectors.- _, _, v = _torch_svd_cast(system)+ # Only the right singular vectors are used; reduced SVD avoids saving the unused (2N, 2N) U.+ _, _, v = _torch_svd_cast(system, full_matrices=False)solution = v[..., -1]# Reshaping the solution vectors to the correct shape.diff --git a/kornia/geometry/epipolar/essential.py b/kornia/geometry/epipolar/essential.pyindex 85f0b1c5c..356fe5c1a 100644--- a/kornia/geometry/epipolar/essential.py+++ b/kornia/geometry/epipolar/essential.py@@ -241,7 +241,8 @@ def _least_squares_basis(design: torch.Tensor) -> torch.Tensor:if design.shape[-2] < 9:return _NullSpaceBasis.apply(design)[0]# every right singular vector has a gradient in torch.linalg.svd itself- _, _, V = _torch_svd_cast(design)+ # N >= 9 here, so reduced SVD still returns all 9 right singular vectors. The full U is unused.+ _, _, V = _torch_svd_cast(design, full_matrices=False)return V[:, :, -4:]diff --git a/tests/geometry/calibration/test_pnp.py b/tests/geometry/calibration/test_pnp.pyindex 64962c103..9b5100cd7 100644--- a/tests/geometry/calibration/test_pnp.py+++ b/tests/geometry/calibration/test_pnp.py@@ -117,6 +117,23 @@ class TestSolvePnpDlt(BaseTester):intrinsics, _, world_points, img_points = self._get_test_data(num_points, device, torch.float64)self.gradcheck(kornia.geometry.solve_pnp_dlt, (world_points, img_points, intrinsics))+ def test_backward_storage_scales_with_points(self, device):+ dtype = torch.float32 if device.type == "mps" else torch.float64+ intrinsics, _, world_points, img_points = self._get_test_data(64, device, dtype)+ world_points.requires_grad_()+ saved_sizes = []++ def pack(tensor):+ saved_sizes.append(tensor.numel())+ return tensor++ with torch.autograd.graph.saved_tensors_hooks(pack, lambda tensor: tensor):+ pose = kornia.geometry.solve_pnp_dlt(world_points, img_points, intrinsics)+ pose.square().sum().backward()++ assert torch.isfinite(world_points.grad).all()+ assert max(saved_sizes) <= 16 * world_points.numel()+@pytest.mark.parametrize("num_points", [8])def test_gradcheck_weights(self, num_points, device):intrinsics, _, world_points, img_points = self._get_test_data(num_points, device, torch.float64)diff --git a/tests/geometry/epipolar/test_essential.py b/tests/geometry/epipolar/test_essential.pyindex c110b1189..6c433a7fd 100644--- a/tests/geometry/epipolar/test_essential.py+++ b/tests/geometry/epipolar/test_essential.py@@ -68,6 +68,22 @@ class TestScaledPolynomialPowers(BaseTester):class TestFindEssential(BaseTester):+ def test_least_squares_basis_backward_storage_scales_with_points(self, device):+ dtype = torch.float32 if device.type == "mps" else torch.float64+ design = torch.rand(1, 128, 9, device=device, dtype=dtype, requires_grad=True)+ saved_sizes = []++ def pack(tensor):+ saved_sizes.append(tensor.numel())+ return tensor++ with torch.autograd.graph.saved_tensors_hooks(pack, lambda tensor: tensor):+ basis = epi.essential._least_squares_basis(design)+ basis.square().sum().backward()++ assert torch.isfinite(design.grad).all()+ assert max(saved_sizes) <= 4 * design.numel()+def test_smoke(self, device, dtype):points1 = torch.rand(1, 5, 2, device=device, dtype=dtype)points2 = torch.rand(1, 5, 2, device=device, dtype=dtype)