Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(epipolar): guard only a zero leading coefficient in the 5-point companion matrix (#4848)

Leonardo Vannib291e6919739
diff --git a/changelog.d/4848.breaking.md b/changelog.d/4848.breaking.md
new file mode 100644
index 000000000..1cc8b43bc
--- /dev/null
+++ b/changelog.d/4848.breaking.md
@@ -0,0 +1,10 @@
+`find_essential` and `run_5point` recover the pose again for the roughly half of all samples whose
+degree-10 polynomial has a negative leading coefficient. Since #3458, first released in 0.8.3, that
+coefficient was clamped with `clamp_min(1e-8)`, which turns a negative coefficient into `+1e-8`, so
+the companion matrix had the wrong sign and scale and none of the 10 candidates was the true
+essential matrix. On noise-free scenes the pose was recovered for 992 of 2000 samples at float64 and
+880 at float32; it is now 1979 and 1767. At float32 with more than five points, the input gradients
+of those samples were also NaN, and now they are finite. Only an exactly zero coefficient is replaced
+now, as before #3458. Samples whose coefficient is at least `1e-8` are unchanged bit for bit; the
+candidates of any other sample can change, including degenerate ones whose coefficient is positive
+but below `1e-8`. Fixes #4847.
diff --git a/kornia/geometry/epipolar/essential.py b/kornia/geometry/epipolar/essential.py
index 37e34286e..8fc6c8ede 100644
--- a/kornia/geometry/epipolar/essential.py
+++ b/kornia/geometry/epipolar/essential.py
@@ -320,7 +320,9 @@ def _null_to_Nister_solution_script(
C = torch.zeros(B, 10, 10, device=device, dtype=dtype)
C[:, 0:9, 1:10] = torch.eye(9, device=device, dtype=dtype)
- cs_de = cs[:, -1].clamp_min(1e-8)
+ # Guard only an exactly zero leading coefficient. The roots do not depend on the polynomial's sign,
+ # and a floor such as clamp_min would turn a negative coefficient positive and change them.
+ cs_de = torch.where(cs[:, -1] == 0, torch.full_like(cs[:, -1], 1e-8), cs[:, -1])
C[:, -1, :] = -cs[:, :-1] / cs_de.unsqueeze(-1)
# A companion matrix that is not finite has no usable roots, and torch.linalg.eigvals aborts on it,
diff --git a/tests/geometry/epipolar/test_essential.py b/tests/geometry/epipolar/test_essential.py
index 595a065e8..cc6174ba2 100644
--- a/tests/geometry/epipolar/test_essential.py
+++ b/tests/geometry/epipolar/test_essential.py
@@ -71,7 +71,7 @@ class TestFindEssential(BaseTester):
mean_error = distance.mean(-1).min()
self.assert_close(mean_error, torch.tensor(0.0, device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
- def test_synthetic_sampson(self, device, dtype):
+ def test_synthetic_sampson(self, device, dtype, monkeypatch):
calibrated_x1 = torch.tensor(
[[[0.0640, 0.7799], [-0.2011, 0.2836], [-0.1355, 0.2907], [0.0520, 1.0086], [-0.0361, 0.6533]]],
device=device,
@@ -94,6 +94,55 @@ class TestFindEssential(BaseTester):
rtol=1e-4,
)
+ # Noise-free minimal samples of random scenes: the pose is recovered for nearly all of them. About
+ # half have a negative leading coefficient in the degree-10 polynomial, which a floor on that
+ # coefficient such as clamp_min turns into the wrong roots (#4847).
+ g = torch.Generator().manual_seed(0)
+ n = 64
+ points = torch.rand(n, 5, 3, generator=g, dtype=torch.float64) * torch.tensor(
+ [2.0, 2.0, 4.0], dtype=torch.float64
+ )
+ points = points + torch.tensor([-1.0, -1.0, 3.0], dtype=torch.float64)
+ axis_angle = (torch.rand(n, 3, generator=g, dtype=torch.float64) - 0.5) * 0.4
+ trans = torch.nn.functional.normalize(torch.rand(n, 3, generator=g, dtype=torch.float64) - 0.5, dim=-1)
+ points, axis_angle, trans = (v.to(device=device, dtype=dtype) for v in (points, axis_angle, trans))
+ R = kornia.geometry.conversions.axis_angle_to_rotation_matrix(axis_angle)
+ points2 = points @ R.transpose(-1, -2) + trans[:, None]
+ x1, x2 = points[..., :2] / points[..., 2:], points2[..., :2] / points2[..., 2:]
+ E_gt = epi.essential_from_Rt(
+ torch.eye(3, device=device, dtype=dtype).expand(n, 3, 3),
+ torch.zeros(n, 3, 1, device=device, dtype=dtype),
+ R,
+ trans[..., None],
+ )
+ weights = torch.ones(n, 5, device=device, dtype=dtype)
+ E_est = epi.essential.find_essential(x1, x2, weights)
+
+ def unit(M):
+ return M / M.flatten(-2).norm(dim=-1)[..., None, None]
+
+ err = torch.minimum(
+ (unit(E_est) - unit(E_gt)[:, None]).flatten(-2).norm(dim=-1),
+ (unit(E_est) + unit(E_gt)[:, None]).flatten(-2).norm(dim=-1),
+ )
+ best = torch.nan_to_num(err, nan=1.0).min(dim=-1).values
+ tol, at_least = (1e-8, 60) if dtype == torch.float64 else (1e-3, 48)
+ assert int((best < tol).sum()) >= at_least
+
+ # The roots of a polynomial do not depend on its sign or scale, and IEEE division makes that exact
+ # for a power-of-two scale, so negating every coefficient, or scaling them all by 2**-40, must leave
+ # the candidates unchanged bit for bit. This holds on every platform, whichever sign the SVD's
+ # null-space basis gives a sample. The scale takes every leading coefficient here below 1e-8, so it
+ # also pins that only an exactly zero one is replaced, not a small one.
+ determinant = epi.essential._determinant_to_polynomial_jit
+ for factor in (-1.0, 2.0**-40):
+ monkeypatch.setattr(
+ epi.essential, "_determinant_to_polynomial_jit", lambda A, *args, f=factor: f * determinant(A, *args)
+ )
+ E_scaled = epi.essential.find_essential(x1, x2, weights)
+ assert torch.equal(torch.isnan(E_scaled), torch.isnan(E_est))
+ self.assert_close(torch.nan_to_num(E_scaled), torch.nan_to_num(E_est), atol=0.0, rtol=0.0)
+
@pytest.mark.parametrize("batch_size, num_points", [(5, 5), (10, 5)])
def test_degenerate_case(self, batch_size, num_points, device, dtype, monkeypatch):
B, N = batch_size, num_points