示例#1
0
    def test_rotation(self, axis, device, dtype, atol, rtol):
        array = [0.0, 0.0, 0.0, 0.0]
        array[1 + axis] = 1.0
        quaternion = torch.tensor(array, device=device, dtype=dtype)
        angle_axis = kornia.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
        quaternion_hat = kornia.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
        assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)

        # just to be sure, check that mixing orders fails
        with pytest.warns(UserWarning):
            quaternion_hat_wrong = kornia.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.XYZW)
        assert not torch.allclose(quaternion_hat_wrong, quaternion, atol=atol, rtol=rtol)
示例#2
0
 def test_zero_angle(self, device, dtype, atol, rtol):
     angle_axis = torch.tensor((0.0, 0.0, 0.0), device=device, dtype=dtype)
     quaternion = kornia.angle_axis_to_quaternion(
         angle_axis, order=QuaternionCoeffOrder.WXYZ)
     angle_axis_hat = kornia.quaternion_to_angle_axis(
         quaternion, order=QuaternionCoeffOrder.WXYZ)
     assert_allclose(angle_axis_hat, angle_axis, atol=atol, rtol=rtol)
示例#3
0
 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)
     with pytest.warns(UserWarning):
         angle_axis = kornia.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.XYZW)
     with pytest.warns(UserWarning):
         quaternion_hat = kornia.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.XYZW)
     assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)
示例#4
0
 def test_small_angle(self, device, dtype):
     theta = 1e-2
     angle_axis = torch.tensor([theta, 0., 0.]).to(device, dtype)
     expected = torch.tensor([np.cos(theta / 2),
                              np.sin(theta / 2), 0., 0.]).to(device, dtype)
     quaternion = kornia.angle_axis_to_quaternion(angle_axis)
     assert_allclose(quaternion, expected)
示例#5
0
 def test_x_rotation(self, device, dtype):
     half_sqrt2 = 0.5 * np.sqrt(2)
     angle_axis = torch.tensor([kornia.pi / 2, 0., 0.]).to(device, dtype)
     expected = torch.tensor([half_sqrt2, half_sqrt2, 0.,
                              0.]).to(device, dtype)
     quaternion = kornia.angle_axis_to_quaternion(angle_axis)
     assert_allclose(quaternion, expected)
示例#6
0
 def test_unit_quaternion(self, device, dtype, atol, rtol):
     quaternion = torch.tensor((1., 0., 0., 0.), device=device, dtype=dtype)
     angle_axis = kornia.quaternion_to_angle_axis(
         quaternion, order=QuaternionCoeffOrder.WXYZ)
     quaternion_hat = kornia.angle_axis_to_quaternion(
         angle_axis, order=QuaternionCoeffOrder.WXYZ)
     assert_allclose(quaternion_hat, quaternion, atol=atol, rtol=rtol)
示例#7
0
    def test_triplet_qma_xyzw(self, axis, device, dtype, atol, rtol):
        array = [[0.0, 0.0, 0.0, 0.0]]
        array[0][axis] = 1.0  # `0 + axis` should fail when WXYZ
        quaternion = torch.tensor(array, device=device, dtype=dtype)
        assert quaternion.shape[-1] == 4

        with pytest.warns(UserWarning):
            mm = kornia.quaternion_to_rotation_matrix(
                quaternion, order=QuaternionCoeffOrder.XYZW)
        assert mm.shape[-1] == 3
        assert mm.shape[-2] == 3

        angle_axis = kornia.rotation_matrix_to_angle_axis(mm)
        assert angle_axis.shape[-1] == 3
        angle_axis_expected = [[0.0, 0.0, 0.0]]
        angle_axis_expected[0][axis] = kornia.pi
        angle_axis_expected = torch.tensor(angle_axis_expected,
                                           device=device,
                                           dtype=dtype)
        assert_allclose(angle_axis, angle_axis_expected, atol=atol, rtol=rtol)

        with pytest.warns(UserWarning):
            quaternion_hat = kornia.angle_axis_to_quaternion(
                angle_axis, order=QuaternionCoeffOrder.XYZW)
        assert_allclose(quaternion_hat, quaternion, atol=atol, rtol=rtol)
示例#8
0
 def test_rotation(self, axis, device, dtype, atol, rtol):
     # half_sqrt2 = 0.5 * np.sqrt(2)
     array = [0.0, 0.0, 0.0]
     array[axis] = kornia.pi / 2.0
     angle_axis = torch.tensor(array, device=device, dtype=dtype)
     quaternion = kornia.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
     angle_axis_hat = kornia.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
     assert_close(angle_axis_hat, angle_axis, atol=atol, rtol=rtol)
示例#9
0
 def test_small_angle(self, axis, device, dtype, atol, rtol):
     theta = 1.0e-2
     array = [0.0, 0.0, 0.0]
     array[axis] = theta
     angle_axis = torch.tensor(array, device=device, dtype=dtype)
     quaternion = kornia.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
     angle_axis_hat = kornia.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
     assert_close(angle_axis_hat, angle_axis, atol=atol, rtol=rtol)
示例#10
0
    def test_small_angle(self, axis, device, dtype, atol, rtol):
        theta = 1.e-2
        array = [np.cos(theta / 2), 0., 0., 0.]
        array[1 + axis] = np.sin(theta / 2.)
        quaternion = torch.tensor(array, device=device, dtype=dtype)
        angle_axis = kornia.quaternion_to_angle_axis(
            quaternion, order=QuaternionCoeffOrder.WXYZ)
        quaternion_hat = kornia.angle_axis_to_quaternion(
            angle_axis, order=QuaternionCoeffOrder.WXYZ)
        assert_allclose(quaternion_hat, quaternion, atol=atol, rtol=rtol)

        # just to be sure, check that mixing orders fails
        with pytest.warns(UserWarning):
            quaternion_hat_wrong = kornia.angle_axis_to_quaternion(
                angle_axis, order=QuaternionCoeffOrder.XYZW)
        assert not torch.allclose(
            quaternion_hat_wrong, quaternion, atol=atol, rtol=rtol)
示例#11
0
 def test_rotation_xyzw(self, axis, device, dtype, atol, rtol):
     array = [0.0, 0.0, 0.0, 0.0]
     array[axis] = 1.0
     quaternion = torch.tensor(array, device=device, dtype=dtype)
     with pytest.warns(UserWarning):
         angle_axis = kornia.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.XYZW)
     with pytest.warns(UserWarning):
         quaternion_hat = kornia.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.XYZW)
     assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)
示例#12
0
 def test_small_angle_xyzw(self, axis, device, dtype, atol, rtol):
     theta = 1.0e-2
     array = [0.0, 0.0, 0.0, np.cos(theta / 2)]
     array[axis] = np.sin(theta / 2.0)
     quaternion = torch.tensor(array, device=device, dtype=dtype)
     with pytest.warns(UserWarning):
         angle_axis = kornia.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.XYZW)
     with pytest.warns(UserWarning):
         quaternion_hat = kornia.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.XYZW)
     assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)
示例#13
0
    def test_triplet_aqm(self, axis, device, dtype, atol, rtol):
        array = [[0.0, 0.0, 0.0]]
        array[0][axis] = kornia.pi / 2.0
        angle_axis = torch.tensor(array, device=device, dtype=dtype)
        assert angle_axis.shape[-1] == 3

        quaternion = kornia.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
        assert quaternion.shape[-1] == 4

        rot_m = kornia.quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.WXYZ)
        assert rot_m.shape[-1] == 3
        assert rot_m.shape[-2] == 3

        angle_axis_hat = kornia.rotation_matrix_to_angle_axis(rot_m)
        assert_close(angle_axis_hat, angle_axis, atol=atol, rtol=rtol)
示例#14
0
    def test_triplet_qma(self, axis, device, dtype, atol, rtol):
        array = [[0.0, 0.0, 0.0, 0.0]]
        array[0][1 + axis] = 1.0  # `1 + axis` this should fail when XYZW
        quaternion = torch.tensor(array, device=device, dtype=dtype)
        assert quaternion.shape[-1] == 4

        mm = kornia.quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.WXYZ)
        assert mm.shape[-1] == 3
        assert mm.shape[-2] == 3

        angle_axis = kornia.rotation_matrix_to_angle_axis(mm)
        assert angle_axis.shape[-1] == 3
        angle_axis_expected = [[0.0, 0.0, 0.0]]
        angle_axis_expected[0][axis] = kornia.pi
        angle_axis_expected = torch.tensor(angle_axis_expected, device=device, dtype=dtype)
        assert_close(angle_axis, angle_axis_expected, atol=atol, rtol=rtol)

        quaternion_hat = kornia.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
        assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)
示例#15
0
    def test_triplet_aqm_xyzw(self, axis, device, dtype, atol, rtol):
        array = [[0., 0., 0.]]
        array[0][axis] = kornia.pi / 2.
        angle_axis = torch.tensor(array, device=device, dtype=dtype)
        assert angle_axis.shape[-1] == 3

        with pytest.warns(UserWarning):
            quaternion = kornia.angle_axis_to_quaternion(
                angle_axis, order=QuaternionCoeffOrder.XYZW)
        assert quaternion.shape[-1] == 4

        with pytest.warns(UserWarning):
            rot_m = kornia.quaternion_to_rotation_matrix(
                quaternion, order=QuaternionCoeffOrder.XYZW)
        assert rot_m.shape[-1] == 3
        assert rot_m.shape[-2] == 3

        angle_axis_hat = kornia.rotation_matrix_to_angle_axis(rot_m)
        assert_allclose(angle_axis_hat, angle_axis, atol=atol, rtol=rtol)
示例#16
0
import torch
import numpy as np
import BPnP
import matplotlib.pyplot as plt
import torchvision
from scipy.io import loadmat, savemat
import kornia as kn

device = 'cuda'

cube = loadmat('demo_data/cube.mat')
pts3d_gt = torch.tensor(cube['pts3d'], device=device, dtype=torch.float)
n = pts3d_gt.size(0)
poses = loadmat('demo_data/poses.mat')
P = torch.tensor(poses['poses'][0],device=device).view(1,6) # camera poses in angle-axis
q_gt = kn.angle_axis_to_quaternion(P[0,0:3])

f = 300
u = 128
v = 128
K = torch.tensor(
    [[f, 0, u],
     [0, f, v],
     [0, 0, 1]],
    device=device, dtype=torch.float
)

pts2d_gt = BPnP.batch_project(P, pts3d_gt, K)
bpnp = BPnP.BPnP.apply
ite = 2000
示例#17
0
 def test_zero_angle(self, device, dtype):
     angle_axis = torch.tensor([0., 0., 0.], device=device, dtype=dtype)
     expected = torch.tensor([1., 0., 0., 0.], device=device, dtype=dtype)
     quaternion = kornia.angle_axis_to_quaternion(angle_axis)
     assert_allclose(quaternion, expected, atol=1e-4, rtol=1e-4)
示例#18
0
 def test_smoke_batch(self, batch_size, device, dtype):
     angle_axis = torch.zeros(batch_size, 3, device=device, dtype=dtype)
     quaternion = kornia.angle_axis_to_quaternion(angle_axis)
     assert quaternion.shape == (batch_size, 4)
示例#19
0
 def test_smoke(self, device, dtype):
     angle_axis = torch.zeros(3)
     quaternion = kornia.angle_axis_to_quaternion(angle_axis)
     assert quaternion.shape == (4, )
示例#20
0
 def test_x_rotation(self):
     half_sqrt2 = 0.5 * np.sqrt(2)
     angle_axis = torch.Tensor([kornia.pi / 2, 0, 0])
     expected = torch.Tensor([half_sqrt2, half_sqrt2, 0, 0])
     quaternion = kornia.angle_axis_to_quaternion(angle_axis)
     assert utils.check_equal_torch(quaternion, expected)
示例#21
0
 def test_small_angle(self):
     theta = 1e-2
     angle_axis = torch.Tensor([theta, 0, 0])
     expected = torch.Tensor([np.cos(theta / 2), np.sin(theta / 2), 0, 0])
     quaternion = kornia.angle_axis_to_quaternion(angle_axis)
     assert utils.check_equal_torch(quaternion, expected)
示例#22
0
 def test_zero_angle(self):
     angle_axis = torch.Tensor([0, 0, 0])
     expected = torch.Tensor([1, 0, 0, 0])
     quaternion = kornia.angle_axis_to_quaternion(angle_axis)
     assert utils.check_equal_torch(quaternion, expected)
示例#23
0
 def test_zero_angle(self, device):
     angle_axis = torch.tensor([0., 0., 0.]).to(device)
     expected = torch.tensor([1., 0., 0., 0.]).to(device)
     quaternion = kornia.angle_axis_to_quaternion(angle_axis)
     assert_allclose(quaternion, expected)
示例#24
0
 def test_zero_angle(self):
     angle_axis = torch.Tensor([0, 0, 0])
     expected = torch.Tensor([1, 0, 0, 0])
     quaternion = kornia.angle_axis_to_quaternion(angle_axis)
     assert_allclose(quaternion, expected)