File size: 6,463 Bytes
36c95ba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import torch
from torch.autograd import gradcheck
import kornia
from kornia.testing import assert_close, tensor_to_gradcheck_var
class TestProjectPoints:
def test_smoke(self, device, dtype):
point_3d = torch.zeros(1, 3, device=device, dtype=dtype)
camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
point_2d = kornia.geometry.camera.project_points(point_3d, camera_matrix)
assert point_2d.shape == (1, 2)
def test_smoke_batch(self, device, dtype):
point_3d = torch.zeros(2, 3, device=device, dtype=dtype)
camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(2, -1, -1)
point_2d = kornia.geometry.camera.project_points(point_3d, camera_matrix)
assert point_2d.shape == (2, 2)
def test_smoke_batch_multi(self, device, dtype):
point_3d = torch.zeros(2, 4, 3, device=device, dtype=dtype)
camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(2, 4, -1, -1)
point_2d = kornia.geometry.camera.project_points(point_3d, camera_matrix)
assert point_2d.shape == (2, 4, 2)
def test_project_and_unproject(self, device, dtype):
point_3d = torch.tensor([[10.0, 2.0, 30.0]], device=device, dtype=dtype)
depth = point_3d[..., -1:]
camera_matrix = torch.tensor(
[[[2746.0, 0.0, 991.0], [0.0, 2748.0, 619.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype
)
point_2d = kornia.geometry.camera.project_points(point_3d, camera_matrix)
point_3d_hat = kornia.geometry.camera.unproject_points(point_2d, depth, camera_matrix)
assert_close(point_3d, point_3d_hat, atol=1e-4, rtol=1e-4)
def test_gradcheck(self, device, dtype):
# TODO: point [0, 0, 0] crashes
points_3d = torch.ones(1, 3, device=device, dtype=dtype)
camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
# evaluate function gradient
points_3d = tensor_to_gradcheck_var(points_3d)
camera_matrix = tensor_to_gradcheck_var(camera_matrix)
assert gradcheck(kornia.geometry.camera.project_points, (points_3d, camera_matrix), raise_exception=True)
def test_jit(self, device, dtype):
points_3d = torch.zeros(1, 3, device=device, dtype=dtype)
camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
op = kornia.geometry.camera.project_points
op_jit = torch.jit.script(op)
assert_close(op(points_3d, camera_matrix), op_jit(points_3d, camera_matrix))
class TestUnprojectPoints:
def test_smoke(self, device, dtype):
points_2d = torch.zeros(1, 2, device=device, dtype=dtype)
depth = torch.ones(1, 1, device=device, dtype=dtype)
camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
point_3d = kornia.geometry.camera.unproject_points(points_2d, depth, camera_matrix)
assert point_3d.shape == (1, 3)
def test_smoke_batch(self, device, dtype):
points_2d = torch.zeros(2, 2, device=device, dtype=dtype)
depth = torch.ones(2, 1, device=device, dtype=dtype)
camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(2, -1, -1)
point_3d = kornia.geometry.camera.unproject_points(points_2d, depth, camera_matrix)
assert point_3d.shape == (2, 3)
def test_smoke_multi_batch(self, device, dtype):
points_2d = torch.zeros(2, 3, 2, device=device, dtype=dtype)
depth = torch.ones(2, 3, 1, device=device, dtype=dtype)
camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(2, 3, -1, -1)
point_3d = kornia.geometry.camera.unproject_points(points_2d, depth, camera_matrix)
assert point_3d.shape == (2, 3, 3)
def test_unproject_center(self, device, dtype):
point_2d = torch.tensor([[0.0, 0.0]], device=device, dtype=dtype)
depth = torch.tensor([[2.0]], device=device, dtype=dtype)
camera_matrix = torch.tensor([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]], device=device, dtype=dtype)
expected = torch.tensor([[0.0, 0.0, 2.0]], device=device, dtype=dtype)
actual = kornia.geometry.camera.unproject_points(point_2d, depth, camera_matrix)
assert_close(actual, expected, atol=1e-4, rtol=1e-4)
def test_unproject_center_normalize(self, device, dtype):
point_2d = torch.tensor([[0.0, 0.0]], device=device, dtype=dtype)
depth = torch.tensor([[2.0]], device=device, dtype=dtype)
camera_matrix = torch.tensor([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]], device=device, dtype=dtype)
expected = torch.tensor([[0.0, 0.0, 2.0]], device=device, dtype=dtype)
actual = kornia.geometry.camera.unproject_points(point_2d, depth, camera_matrix, True)
assert_close(actual, expected, atol=1e-4, rtol=1e-4)
def test_unproject_and_project(self, device, dtype):
point_2d = torch.tensor([[0.0, 0.0]], device=device, dtype=dtype)
depth = torch.tensor([[2.0]], device=device, dtype=dtype)
camera_matrix = torch.tensor([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]], device=device, dtype=dtype)
point_3d = kornia.geometry.camera.unproject_points(point_2d, depth, camera_matrix)
point_2d_hat = kornia.geometry.camera.project_points(point_3d, camera_matrix)
assert_close(point_2d, point_2d_hat, atol=1e-4, rtol=1e-4)
def test_gradcheck(self, device, dtype):
points_2d = torch.zeros(1, 2, device=device, dtype=dtype)
depth = torch.ones(1, 1, device=device, dtype=dtype)
camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
# evaluate function gradient
points_2d = tensor_to_gradcheck_var(points_2d)
depth = tensor_to_gradcheck_var(depth)
camera_matrix = tensor_to_gradcheck_var(camera_matrix)
assert gradcheck(
kornia.geometry.camera.unproject_points, (points_2d, depth, camera_matrix), raise_exception=True
)
def test_jit(self, device, dtype):
points_2d = torch.zeros(1, 2, device=device, dtype=dtype)
depth = torch.ones(1, 1, device=device, dtype=dtype)
camera_matrix = torch.eye(3, device=device, dtype=dtype).expand(1, -1, -1)
args = (points_2d, depth, camera_matrix)
op = kornia.geometry.camera.unproject_points
op_jit = torch.jit.script(op)
assert_close(op(*args), op_jit(*args))
|