π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): drop the identity fallback of Se3.exp and Se3.log so d t / d omega is -0.5 [upsilon]_x there (#4963)
εΊζ’η117ce23eb812
diff --git a/changelog.d/4963.fixed.md b/changelog.d/4963.fixed.mdnew file mode 100644index 000000000..e11e8c06b--- /dev/null+++ b/changelog.d/4963.fixed.md@@ -0,0 +1,5 @@+`Se3.exp` and `Se3.log` now have the right gradient at the identity (#4953). Both fell back to `t = upsilon`+(`upsilon = t`) at `omega = 0`, which is the right value since `V(0) = I` but does not depend on `omega`, so autograd+returned `d t / d omega = 0` there instead of `-0.5 [upsilon]_x`, and `d upsilon / d q_vec = 0` for `log` instead of+`[t]_x`. The coefficients of `V` and `V^-1` have been finite at `theta = 0` since #4941, so `V @ upsilon` is now taken+for every element. The forward values are unchanged.diff --git a/kornia/geometry/liegroup/se3.py b/kornia/geometry/liegroup/se3.pyindex 0b4890682..2dff847ed 100644--- a/kornia/geometry/liegroup/se3.py+++ b/kornia/geometry/liegroup/se3.py@@ -178,22 +178,21 @@ class Se3(nn.Module):omega_hat_sq = omega_hat @ omega_hattheta_sq = batched_dot_product(omega, omega)nonzero = theta_sq > 0- # V is a 0/0 at omega = 0 and its sqrt has an unbounded derivative there, so although the- # where below already returns upsilon at the identity, autograd walked V anyway and- # 0 * nan = nan reached v.grad. Evaluate both on a substituted theta of 1 there; the- # where discards the value, only the gradient changes (kornia#4229's shape).+ # sqrt has an unbounded derivative at 0, so it is taken on a substituted 1 at omega = 0 and+ # the where selects the exact 0 (kornia#4229's shape). The coefficients of V are finite there+ # (1/2 and 1/6) and even in theta, so V @ upsilon is taken for every element: the former+ # fallback to upsilon at the identity did not depend on omega and made d t / d omega = 0+ # there instead of -0.5 [upsilon]_x (kornia#4953). The value is unchanged, V(0) = I exactly.safe_theta_sq = torch.where(nonzero, theta_sq, torch.ones_like(theta_sq))theta = torch.where(nonzero, safe_theta_sq.sqrt(), torch.zeros_like(theta_sq))- safe_theta = torch.where(nonzero, theta, torch.ones_like(theta))R = So3.exp(omega)- a, b, _ = _so3_small_angle_coefficients(safe_theta)+ a, b, _ = _so3_small_angle_coefficients(theta)V = (torch.eye(3, device=v.device, dtype=v.dtype)+ a[..., None, None] * omega_hat+ b[..., None, None] * omega_hat_sq)- U = torch.where(nonzero[..., None], (upsilon[..., None, :] * V).sum(-1), upsilon)- return Se3(R, U)+ return Se3(R, (upsilon[..., None, :] * V).sum(-1))def log(self) -> torch.Tensor:"""Convert elements of lie group to elements of lie algebra.@@ -212,20 +211,19 @@ class Se3(nn.Module):# incoming gradient straight through at its bound, and 1e-12 underflows to 0 in float16# anyway, so sqrt's unbounded derivative at 0 reached v.grad as nan (kornia#4229). Keep# the floor on the branch that is selected -- byte-identical for every theta_sq > 0 --- # and take the exact zero elsewhere, where V_inv is the identity and both branches of- # the where below agree.+ # and take the exact zero elsewhere, where V_inv is the identity.safe_theta_sq = torch.where(nonzero, theta_sq.clamp_min(1e-12), torch.ones_like(theta_sq))theta = torch.where(nonzero, safe_theta_sq.sqrt(), torch.zeros_like(theta_sq))- safe_theta = torch.where(nonzero, theta, torch.ones_like(theta))t = _unwrap(self.t)omega_hat = So3.hat(omega)omega_hat_sq = omega_hat @ omega_hat- _, _, c = _so3_small_angle_coefficients(safe_theta)+ # c is finite at theta = 0 (1/12), so V^-1 @ t is taken for every element; the former fallback+ # to t at the identity made d upsilon / d q_vec = 0 there instead of [t]_x (kornia#4953).+ _, _, c = _so3_small_angle_coefficients(theta)V_inv = (torch.eye(3, device=omega.device, dtype=omega.dtype) - 0.5 * omega_hat + c[..., None, None] * omega_hat_sq)- t = torch.where(nonzero[..., None], (t[..., None, :] * V_inv).sum(-1), t)- return torch.cat((t, omega), -1)+ return torch.cat(((t[..., None, :] * V_inv).sum(-1), omega), -1)@staticmethoddef hat(v: torch.Tensor) -> torch.Tensor:diff --git a/tests/geometry/liegroup/test_se3.py b/tests/geometry/liegroup/test_se3.pyindex 112bd3d7e..756214d28 100644--- a/tests/geometry/liegroup/test_se3.py+++ b/tests/geometry/liegroup/test_se3.py@@ -69,8 +69,8 @@ class TestSe3(BaseTester):self.gradcheck(lambda x: Se3.exp(x).matrix(), (v,))def test_gradient_is_finite_at_the_identity_4404(self, device, dtype):- # #4404: at omega = 0 the where below returns upsilon, but autograd still walks V, whose- # sqrt has an unbounded derivative there and whose terms divide by theta**2 and theta**3.+ # #4404: at omega = 0 autograd walks V, whose sqrt has an unbounded derivative there and+ # whose closed-form terms divide by theta**2 and theta**3.# log has the same defect through its own theta, where clamp_min(1e-12) guards the value# and not the gradient (#4229) and underflows to 0 in float16 besides.v = torch.zeros(1, 6, device=device, dtype=dtype, requires_grad=True)@@ -81,6 +81,23 @@ class TestSe3(BaseTester):Se3(So3(Quaternion(data[:, :4])), data[:, 4:]).log().sum().backward()assert bool(torch.isfinite(data.grad).all()), data.grad+ def test_gradient_at_the_identity_couples_rotation_and_translation_4953(self, device, dtype):+ # #4953: exp fell back to t = upsilon and log to upsilon = t at omega = 0. The values were+ # right, V(0) = I, but the fallback did not depend on omega, so autograd returned+ # d t / d omega = 0 at the identity, the standard initialisation for pose optimisation.+ # The derivative of V(omega) upsilon = upsilon + 0.5 omega x upsilon + O(|omega|^2) is+ # -0.5 [upsilon]_x, and d upsilon / d q_vec of log at the identity is [t]_x.+ v = torch.tensor([[1.0, 2.0, 3.0, 0.0, 0.0, 0.0]], device=device, dtype=dtype)+ jac = torch.autograd.functional.jacobian(lambda x: Se3.exp(x).t, v)[0, :, 0, :]+ upsilon = v[0, :3]+ self.assert_close(jac[:, :3], torch.eye(3, device=device, dtype=dtype))+ self.assert_close(jac[:, 3:], -0.5 * So3.hat(upsilon))+ qt = torch.tensor([[1.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0]], device=device, dtype=dtype)+ jac = torch.autograd.functional.jacobian(lambda x: Se3(So3(Quaternion(x[:, :4])), x[:, 4:]).log(), qt)+ jac = jac[0, :, 0, :]+ self.assert_close(jac[:3, 4:], torch.eye(3, device=device, dtype=dtype))+ self.assert_close(jac[:3, 1:4], So3.hat(qt[0, 4:]))+# TODO: implement medef test_jit(self, device, dtype):pass