🐍 Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Batched quaternion to matrix (#2395)
Michał Tyszkiewicz59f2f5214d6e
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.pyindex 64f9d6a09..b2143101a 100644--- a/kornia/geometry/conversions.py+++ b/kornia/geometry/conversions.py@@ -566,7 +566,7 @@ def quaternion_to_rotation_matrix(tzz: Tensor = tz * zone: Tensor = tensor(1.0)- matrix: Tensor = stack(+ matrix_flat: Tensor = stack((one - (tyy + tzz),txy - twz,@@ -579,10 +579,12 @@ def quaternion_to_rotation_matrix(one - (txx + tyy),),dim=-1,- ).view(-1, 3, 3)+ )++ # this slightly awkward construction of the output shape is to satisfy torchscript+ output_shape = list(quaternion.shape[:-1]) + [3, 3]+ matrix = matrix_flat.reshape(output_shape)- if len(quaternion.shape) == 1:- matrix = torch.squeeze(matrix, dim=0)return matrixdiff --git a/kornia/geometry/quaternion.py b/kornia/geometry/quaternion.pyindex 939f6b675..54b1fb14c 100644--- a/kornia/geometry/quaternion.py+++ b/kornia/geometry/quaternion.py@@ -239,7 +239,7 @@ class Quaternion(Module):>>> mtensor([[1., 0., 0.],[0., 1., 0.],- [0., 0., 1.]], grad_fn=<SqueezeBackward1>)+ [0., 0., 1.]], grad_fn=<ReshapeAliasBackward0>)"""return quaternion_to_rotation_matrix(self.data, order=QuaternionCoeffOrder.WXYZ)diff --git a/test/geometry/test_conversions.py b/test/geometry/test_conversions.pyindex a01cdebac..7997c14b7 100644--- a/test/geometry/test_conversions.py+++ b/test/geometry/test_conversions.py@@ -544,20 +544,20 @@ class TestRotationMatrixToQuaternion:class TestQuaternionToRotationMatrix:- @pytest.mark.parametrize("batch_size", (1, 3, 8))- def test_smoke_batch_xyzw(self, batch_size, device, dtype):- quaternion = torch.zeros(batch_size, 4, device=device, dtype=dtype)+ @pytest.mark.parametrize("batch_dims", ((), (1,), (3,), (8,), (1, 1), (5, 6)))+ def test_smoke_batch_xyzw(self, batch_dims, device, dtype):+ quaternion = torch.zeros(*batch_dims, 4, device=device, dtype=dtype)with pytest.warns(UserWarning):matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.XYZW)- assert matrix.shape == (batch_size, 3, 3)+ assert matrix.shape == (*batch_dims, 3, 3)- @pytest.mark.parametrize("batch_size", (1, 3, 8))- def test_smoke_batch(self, batch_size, device, dtype):- quaternion = torch.zeros(batch_size, 4, device=device, dtype=dtype)+ @pytest.mark.parametrize("batch_dims", ((), (1,), (3,), (8,), (1, 1), (5, 6)))+ def test_smoke_batch(self, batch_dims, device, dtype):+ quaternion = torch.zeros(*batch_dims, 4, device=device, dtype=dtype)matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.WXYZ)- assert matrix.shape == (batch_size, 3, 3)+ assert matrix.shape == (*batch_dims, 3, 3)def test_unit_quaternion_xyzw(self, device, dtype, atol, rtol):quaternion = torch.tensor((0.0, 0.0, 0.0, 1.0), device=device, dtype=dtype)