π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
implement quaternion_log_to_exp, quaternion_exp_to_log
edgarriba1e75934208a0
diff --git a/docs/source/geometry.conversions.rst b/docs/source/geometry.conversions.rstindex e3603fb35..76ad5f5ea 100644--- a/docs/source/geometry.conversions.rst+++ b/docs/source/geometry.conversions.rst@@ -11,6 +11,8 @@ kornia.geometry.conversions.. autofunction:: rotation_matrix_to_quaternion.. autofunction:: quaternion_to_angle_axis.. autofunction:: quaternion_to_rotation_matrix+.. autofunction:: quaternion_log_to_exp+.. autofunction:: quaternion_exp_to_log.. autofunction:: angle_axis_to_quaternion.. autofunction:: angle_axis_to_rotation_matrix.. autofunction:: rtvec_to_posediff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.pyindex 0b22ad6c7..d41189258 100644--- a/kornia/geometry/conversions.py+++ b/kornia/geometry/conversions.py@@ -18,6 +18,8 @@ __all__ = ["rotation_matrix_to_quaternion","quaternion_to_angle_axis","quaternion_to_rotation_matrix",+ "quaternion_log_to_exp",+ "quaternion_exp_to_log","rtvec_to_pose","denormalize_pixel_coordinates","normalize_pixel_coordinates",@@ -461,6 +463,82 @@ def quaternion_to_angle_axis(quaternion: torch.Tensor) -> torch.Tensor:return angle_axis+def quaternion_log_to_exp(quaternion: torch.Tensor,+ eps: float = 1e-8) -> torch.Tensor:+ r"""Applies exponential map to log quaternion.++ Args:+ quaternion (torch.Tensor): a tensor containing a quaternion to be+ converted. The tensor can be of shape :math:`(*, 3)`.++ Return:+ torch.Tensor: the quaternion exponential map of shape :math:`(*, 4)`.++ Example:+ >>> quaternion = torch.tensor([0., 0., 0.])+ >>> kornia.quaternion_log_to_exp(quaternion)+ tensor([0., 0., 0., 1.])+ """+ if not torch.is_tensor(quaternion):+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(+ type(quaternion)))++ if not quaternion.shape[-1] == 3:+ raise ValueError(+ "Input must be a tensor of shape (*, 3). Got {}".format(+ quaternion.shape))+ # compute quaternion norm+ norm_q: torch.Tensor = torch.norm(+ quaternion, p=2, dim=-1, keepdim=True).clamp(min=eps)++ # compute scalar and vector+ quaternion_vector: torch.Tensor = quaternion * torch.sin(norm_q) / norm_q+ quaternion_scalar: torch.Tensor = torch.cos(norm_q)++ # compose quaternion and return+ quaternion_exp: torch.Tensor = torch.cat(+ [quaternion_vector, quaternion_scalar], dim=-1)+ return quaternion_exp+++def quaternion_exp_to_log(quaternion: torch.Tensor,+ eps: float = 1e-8) -> torch.Tensor:+ r"""Applies the log map to a quaternion.++ Args:+ quaternion (torch.Tensor): a tensor containing a quaternion to be+ converted. The tensor can be of shape :math:`(*, 4)`.++ Return:+ torch.Tensor: the quaternion log map of shape :math:`(*, 3)`.++ Example:+ >>> quaternion = torch.tensor([0., 0., 0., 1.])+ >>> kornia.quaternion_exp_to_log(quaternion)+ tensor([0., 0., 0.])+ """+ if not torch.is_tensor(quaternion):+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(+ type(quaternion)))++ if not quaternion.shape[-1] == 4:+ raise ValueError(+ "Input must be a tensor of shape (*, 4). Got {}".format(+ quaternion.shape))+ # unpack quaternion vector and scalar+ quaternion_vector: torch.Tensor = quaternion[..., 0:3]+ quaternion_scalar: torch.Tensor = quaternion[..., 3:4]++ # compute quaternion norm+ norm_q: torch.Tensor = torch.norm(+ quaternion_vector, p=2, dim=-1, keepdim=True).clamp(min=eps)++ # apply log map+ quaternion_log: torch.Tensor = quaternion_vector * torch.acos(+ torch.clamp(quaternion_scalar, min=-1.0, max=1.0)) / norm_q+ return quaternion_log++# based on:# https://github.com/facebookresearch/QuaterNet/blob/master/common/quaternion.py#L138diff --git a/test/geometry/test_conversions.py b/test/geometry/test_conversions.pyindex b886a968b..82ae6ab36 100644--- a/test/geometry/test_conversions.py+++ b/test/geometry/test_conversions.py@@ -1,3 +1,5 @@+from typing import Optional+import pytestimport numpy as np@@ -121,6 +123,82 @@ class TestQuaternionToRotationMatrix:assert_allclose(actual, expected)+class TestQuaternionLogToExp:++ @pytest.mark.parametrize("batch_size", (1, 3, 8))+ def test_smoke_batch(self, batch_size):+ quaternion_log = torch.zeros(batch_size, 3)+ quaternion_exp = kornia.quaternion_log_to_exp(quaternion_log)+ assert quaternion_exp.shape == (batch_size, 4)++ def test_unit_quaternion(self):+ quaternion_log = torch.tensor([0., 0., 0.])+ expected = torch.tensor([0., 0., 0., 1.])+ assert_allclose(kornia.quaternion_log_to_exp(quaternion_log), expected)++ def test_back_and_forth(self):+ quaternion_log = torch.tensor([0., 0., 0.])+ quaternion_exp = kornia.quaternion_log_to_exp(quaternion_log)+ quaternion_log_hat = kornia.quaternion_exp_to_log(quaternion_exp)+ assert_allclose(quaternion_log, quaternion_log_hat)++ def test_gradcheck(self):+ quaternion = torch.tensor([0., 0., 1.])+ quaternion = tensor_to_gradcheck_var(quaternion)+ # evaluate function gradient+ assert gradcheck(kornia.quaternion_log_to_exp, (quaternion,),+ raise_exception=True)++ def test_jit(self):+ @torch.jit.script+ def op_script(input: torch.Tensor, eps: float) -> torch.Tensor:+ return kornia.quaternion_log_to_exp(input, eps)++ eps = 1e-8+ quaternion = torch.tensor([0., 0., 1.])+ actual = op_script(quaternion, eps)+ expected = kornia.quaternion_log_to_exp(quaternion, eps)+ assert_allclose(actual, expected)+++class TestQuaternionExpToLog:++ @pytest.mark.parametrize("batch_size", (1, 3, 8))+ def test_smoke_batch(self, batch_size):+ quaternion_exp = torch.zeros(batch_size, 4)+ quaternion_log = kornia.quaternion_exp_to_log(quaternion_exp)+ assert quaternion_log.shape == (batch_size, 3)++ def test_unit_quaternion(self):+ quaternion_exp = torch.tensor([0., 0., 0., 1.])+ expected = torch.tensor([0., 0., 0.])+ assert_allclose(kornia.quaternion_exp_to_log(quaternion_exp), expected)++ def test_back_and_forth(self):+ quaternion_exp = torch.tensor([0., 0., 0., 1.])+ quaternion_log = kornia.quaternion_exp_to_log(quaternion_exp)+ quaternion_exp_hat = kornia.quaternion_log_to_exp(quaternion_log)+ assert_allclose(quaternion_exp, quaternion_exp_hat)++ def test_gradcheck(self):+ quaternion = torch.tensor([0., 0., 0., 1.])+ quaternion = tensor_to_gradcheck_var(quaternion)+ # evaluate function gradient+ assert gradcheck(kornia.quaternion_exp_to_log, (quaternion,),+ raise_exception=True)++ def test_jit(self):+ @torch.jit.script+ def op_script(input: torch.Tensor, eps: float) -> torch.Tensor:+ return kornia.quaternion_exp_to_log(input, eps)++ eps = 1e-8+ quaternion = torch.tensor([0., 0., 1., 0.])+ actual = op_script(quaternion, eps)+ expected = kornia.quaternion_exp_to_log(quaternion, eps)+ assert_allclose(actual, expected)++class TestQuaternionToAngleAxis:def test_smoke(self):