π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): divide by exactly w in convert_points_from_homogeneous (#3976)
Chandan P816e2c698e6e
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.pyindex 7b3e92110..42d0ae764 100644--- a/kornia/geometry/conversions.py+++ b/kornia/geometry/conversions.py@@ -253,33 +253,21 @@ def convert_points_from_homogeneous(points: torch.Tensor, eps: float = 1e-8) ->coordinate ``w`` and is dropped from the output- requires rank :math:`\geq 2`: a bare :math:`(D,)` point raises``ValueError``- - when ``abs(w) > eps`` the remaining components are divided by ``w``- with its sign kept (``[[2., 4., -2.]]`` gives ``[[-1., -2.]]``), up to- the bias described in the warning below+ - when ``abs(w) > eps`` the remaining components are divided by exactly+ ``w``, with its sign kept (``[[2., 4., -2.]]`` gives ``[[-1., -2.]]``)- when ``abs(w) <= eps`` (default ``eps = 1e-8``; the test is a strict``>``) the numerator is instead returned **unchanged**, followingOpenCV: ``[[2., 4., 0.]]`` gives ``[[2., 4.]]``-- .. warning::- The division is by ``w + eps`` rather than by ``w``, and ``eps`` is- added without regard to the sign of ``w``, so the **signed** relative- error of the result is exactly ``-eps / (w + eps)``. At ``w = 2e-8``- that is ``-1/3``: the exact result ``[1e8, 2e8]`` comes out as- ``[6.67e7, 1.33e8]`` (33 % low). At ``w = -2e-8`` it is ``+1``:- ``[-1e8, -2e8]`` comes out as ``[-2e8, -4e8]`` (100 % high). Only for- ``abs(w)`` much larger than ``eps`` does it reduce to the familiar- ``-eps / w``, and there it is usually below the rounding of the working- dtype β at ``w = 2`` the measured error is ``-5.0e-09`` in ``float64``,- while in ``float32`` ``2 + eps`` rounds back to ``2`` and the result is- exact. The numbers above assume ``eps`` is representable: in- ``float16`` both the default ``eps`` and ``w = 2e-8`` underflow to- ``0``, so ``[[2., 4., 2e-8]]`` takes the ``abs(w) <= eps`` pass-through- branch and returns ``[[2., 4.]]``. Tracked in- `#3938 <https://github.com/kornia/kornia/issues/3938>`_.+ - ``eps`` only picks the branch, it never enters the division, so the+ result carries no bias from it. Note that ``eps`` is compared against+ ``w`` in the working dtype: in ``float16`` the default ``eps`` and+ ``w = 2e-8`` both underflow to ``0``, so ``[[2., 4., 2e-8]]`` takes+ the pass-through branch there and returns ``[[2., 4.]]``Args:points: the points to be transformed of shape :math:`(*, N, D)`.- eps: to avoid division by zero.+ eps: threshold on ``abs(w)`` below which the point is returned+ unchanged instead of divided, to avoid division by zero.Returns:the points in Euclidean space :math:`(*, N, D-1)`.@@ -303,7 +291,8 @@ def convert_points_from_homogeneous(points: torch.Tensor, eps: float = 1e-8) -># follow the convention of opencv:# https://github.com/opencv/opencv/pull/14411/filesmask: torch.Tensor = torch.abs(z_vec) > eps- scale = torch.where(mask, 1.0 / (z_vec + eps), torch.ones_like(z_vec))+ safe_z_vec = torch.where(mask, z_vec, torch.ones_like(z_vec))+ scale = torch.where(mask, 1.0 / safe_z_vec, torch.ones_like(z_vec))return scale * points[..., :-1]diff --git a/tests/geometry/test_conversions.py b/tests/geometry/test_conversions.pyindex 6e4836213..7c669167c 100644--- a/tests/geometry/test_conversions.py+++ b/tests/geometry/test_conversions.py@@ -3002,22 +3002,15 @@ class TestConvertPointsFromHomogeneous(BaseTester):self.assert_close(actual, expected)- @pytest.mark.xfail(- raises=AssertionError,- reason="convert_points_from_homogeneous divides by w + eps β kornia#3938",- strict=True,- )def test_convention_divides_by_exactly_w(self, device, dtype):- # Intended behavior: for |w| > eps the point is divided by exactly w. It currently is- # divided by w + eps with no regard for sign, so the signed relative error is exactly- # -eps / (w + eps): -1/3 at w = +2e-8 (33 % low) and +1 at w = -2e-8 (100 % high) (#3938).- # Marked xfail(strict=True) so fixing #3938 makes this XPASS and forces the mark out.+ # For |w| > eps the point is divided by exactly w, matching OpenCV's+ # convertPointsFromHomogeneous (scale = fabs(w) > FLT_EPSILON ? 1./w : 1.), see+ # https://github.com/opencv/opencv/pull/14411/files. This used to divide by w + eps,+ # which made the result 33 % low here (#3938).# Snippet used to generate expected (by hand):- # 2 / 2e-8, 4 / 2e-8 -> [1e8, 2e8] (kornia returns [6.6666667e7, 1.3333333e8])- # measured signed relative error in float64: -0.3333333333333334, and- # -eps / (w + eps) = -1e-8 / 3e-8 = -0.3333333333333333+ # 2 / 2e-8, 4 / 2e-8 -> [1e8, 2e8]if dtype == torch.float16:- pytest.skip("float16 underflows w=2e-8 to 0, which is the |w| <= eps passthrough branch, not the eps bias")+ pytest.skip("float16 underflows w=2e-8 to 0, which takes the |w| <= eps passthrough branch instead")points = torch.tensor([[2.0, 4.0, 2e-8]], device=device, dtype=dtype)@@ -3026,36 +3019,62 @@ class TestConvertPointsFromHomogeneous(BaseTester):expected = torch.tensor([[1e8, 2e8]], device=device, dtype=dtype)self.assert_close(out, expected)- def test_wart_division_is_by_w_plus_eps_for_both_signs_3938(self, device, dtype):- # Wart pin for kornia#3938, companion to the strict xfail above: assert the CURRENT- # biased outputs for both signs of w. The xfail pins the intended behavior but cannot- # flip under every fix polarity -- a sign-aware eps (w + sign(w) * eps) leaves the- # positive-w case failing the intended [1e8, 2e8] and the mark silently XFAIL; here the- # NEGATIVE-w cell (divided by -2e-8 + 1e-8 = -1e-8, doubling the point) flips under that- # fix too, and both cells flip under an exact division or a grad-only eps. If either- # assert fails, #3938 was (partly) fixed -- update or remove the warning in- # convert_points_from_homogeneous and flip/remove the strict xfail above. eps=1e-8 is- # passed explicitly so the pinned literals do not silently track a later change to the default.- # Snippet used to generate expected (torch only, executed at each pinned dtype):- # cpfh = kornia.geometry.conversions.convert_points_from_homogeneous- # cpfh(torch.tensor([[2., 4., 2e-8]], dtype=torch.float64), eps=1e-8)- # -> [[66666666.66666666, 133333333.33333331]] (f32: [[66666668.0, 133333336.0]])- # cpfh(torch.tensor([[2., 4., -2e-8]], dtype=torch.float64), eps=1e-8)- # -> [[-200000000.0, -400000000.0]] (f32: identical)- # At bfloat16 the outputs land within 0.15 % of the literals (66584576, -200278016),- # inside rtol 1e-2, so the pin holds there too.+ def test_division_is_exact_for_both_signs_of_w(self, device, dtype):+ # Companion to the test above: the same small |w| in both signs. The division used to+ # be by w + eps, which is not sign-aware, so it grew a small positive denominator and+ # shrank a small negative one -- 33 % low at w = +2e-8 and 100 % high at w = -2e-8,+ # errors in opposite directions for inputs that differ only in the sign of w (#3938).+ # w is a power of two just above the default eps (2 ** -26 = 1.4901161e-8 > 1e-8), so+ # the quotients are powers of two too and are exact in every dtype that can hold them.+ # That is what lets this assert with zero tolerance. eps is passed explicitly so the+ # literals do not silently track a later change to the default.+ # Snippet used to generate expected (by hand, all values exact in binary):+ # 2 / 2 ** -26 -> 2 ** 27 = 134217728.0, 4 / 2 ** -26 -> 2 ** 28 = 268435456.0if dtype == torch.float16:- pytest.skip("float16 underflows w=2e-8 to 0, which is the |w| <= eps passthrough branch, not the eps bias")+ pytest.skip("float16 underflows w=2**-26 to 0 (the |w| <= eps passthrough branch) and overflows 2**27")++ cpfh = kornia.geometry.conversions.convert_points_from_homogeneous+ w = 2.0**-26++ out_pos = cpfh(torch.tensor([[2.0, 4.0, w]], device=device, dtype=dtype), eps=1e-8)+ out_neg = cpfh(torch.tensor([[2.0, 4.0, -w]], device=device, dtype=dtype), eps=1e-8)++ expected_pos = torch.tensor([[2.0**27, 2.0**28]], device=device, dtype=dtype)+ self.assert_close(out_pos, expected_pos, atol=0.0, rtol=0.0)+ self.assert_close(out_neg, -expected_pos, atol=0.0, rtol=0.0)++ def test_roundtrip_from_to_homogeneous_is_identity(self, device, dtype):+ # Oracle-free invariant: convert_points_to_homogeneous appends w = 1, so dividing by+ # exactly w must return the input untouched in any dtype. Dividing by w + eps instead+ # made this an identity only to ~1e-8 relative, worse than float32 even in float64 (#3938).+ points = torch.tensor([[1.5, -2.5], [0.0, 3.25], [-4.75, 0.125]], device=device, dtype=dtype)+ cpth = kornia.geometry.conversions.convert_points_to_homogeneouscpfh = kornia.geometry.conversions.convert_points_from_homogeneous- out_pos = cpfh(torch.tensor([[2.0, 4.0, 2e-8]], device=device, dtype=dtype), eps=1e-8)- out_neg = cpfh(torch.tensor([[2.0, 4.0, -2e-8]], device=device, dtype=dtype), eps=1e-8)+ self.assert_close(cpfh(cpth(points)), points, atol=0.0, rtol=0.0)- expected_pos = torch.tensor([[6.6666668e7, 1.33333336e8]], device=device, dtype=dtype)- expected_neg = torch.tensor([[-2e8, -4e8]], device=device, dtype=dtype)- self.assert_close(out_pos, expected_pos, atol=0.0, rtol=1e-2)- self.assert_close(out_neg, expected_neg, atol=0.0, rtol=1e-2)+ def test_convention_the_masked_division_keeps_the_gradient_finite(self, device):+ # Companion to the forward pins above, for the half of the fix they cannot see. The old+ # `1.0 / (z_vec + eps)` is evaluated for EVERY point, including the ones the mask discards.+ # At w == -eps that denominator is exactly zero, so the reciprocal is inf; torch.where drops+ # that lane from the forward value but the backward pass still multiplies it by the zero+ # cotangent and 0 * inf is NaN. Measured on the unfixed code the gradient at this input is+ # [1., 1., nan]; the double-`where` makes it [1., 1., 0.].+ # w is the exact pole rather than merely a small value: a nearby w only makes the reciprocal+ # large, which no assertion on finiteness would catch. eps is passed explicitly so the input+ # tracks the guard rather than the default. float64 only -- at float16 the default eps+ # underflows to 0 (pinned in test_wart_float16_underflowed_default_eps_flips_branches), so+ # w == -eps is not the pole there.+ eps = 1e-8+ points = torch.tensor([[2.0, 4.0, -eps]], device=device, dtype=torch.float64, requires_grad=True)++ kornia.geometry.conversions.convert_points_from_homogeneous(points, eps=eps).sum().backward()++ assert torch.isfinite(points.grad).all(), (+ "kornia#3938: convert_points_from_homogeneous divides by exactly zero at w == -eps, so "+ f"the discarded branch poisons the gradient: {points.grad.tolist()}"+ )def _skip_if_mps_clamp_caching(device):@@ -3253,10 +3272,11 @@ def test_wart_default_eps_1e_8_backs_the_remaining_quoted_warning_numbers():def test_wart_float16_underflowed_default_eps_flips_branches(device):- # Wart pins for the float16 sentences of the #3939 and #3938 warnings. float16 is- # hardcoded (no dtype fixture) so the pins run in every test configuration: the float16- # legs of the wart pins above are skipped because the default eps=1e-8 underflows to 0- # there, which is exactly the behavior pinned here. eps is left at its default on purpose+ # Wart pin for the float16 sentence of the #3939 warning and for the float16 sentence of+ # the convert_points_from_homogeneous Convention block. float16 is hardcoded (no dtype+ # fixture) so the pins run in every test configuration: the float16 legs of the tests+ # above are skipped because the default eps=1e-8 underflows to 0 there, which is exactly+ # the behavior pinned here. eps is left at its default on purpose# -- the underflow of the *default* is the claim. atol=rtol=0.0 because both claims are# exactness claims: with the float16 default tolerance (1e-3) the eps-biased# rho = 1e-4 of the other branch would still compare equal to 0.