π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
implement cuda test for depth warper
edgarriba4b1654984ac3
diff --git a/test/geometry/test_depth.py b/test/geometry/test_depth.pyindex 16c4cb200..d1bc5526b 100644--- a/test/geometry/test_depth.py+++ b/test/geometry/test_depth.py@@ -2,7 +2,7 @@ import pytestimport korniaimport kornia.testing as utils # test utils-from test.common import TEST_DEVICES+from test.common import deviceimport torchfrom torch.autograd import gradcheck@@ -10,42 +10,42 @@ from torch.testing import assert_allcloseclass TestDepthTo3d:- def test_smoke(self):- depth = torch.rand(1, 1, 3, 4)- camera_matrix = torch.rand(1, 3, 3)+ def test_smoke(self, device):+ depth = torch.rand(1, 1, 3, 4).to(device)+ camera_matrix = torch.rand(1, 3, 3).to(device)points3d = kornia.depth_to_3d(depth, camera_matrix)assert points3d.shape == (1, 3, 3, 4)@pytest.mark.parametrize("batch_size", [2, 4, 5])- def test_shapes(self, batch_size):- depth = torch.rand(batch_size, 1, 3, 4)- camera_matrix = torch.rand(batch_size, 3, 3)+ def test_shapes(self, device, batch_size):+ depth = torch.rand(batch_size, 1, 3, 4).to(device)+ camera_matrix = torch.rand(batch_size, 3, 3).to(device)points3d = kornia.depth_to_3d(depth, camera_matrix)assert points3d.shape == (batch_size, 3, 3, 4)@pytest.mark.parametrize("batch_size", [1, 2, 4, 5])- def test_shapes_broadcast(self, batch_size):- depth = torch.rand(batch_size, 1, 3, 4)- camera_matrix = torch.rand(1, 3, 3)+ def test_shapes_broadcast(self, device, batch_size):+ depth = torch.rand(batch_size, 1, 3, 4).to(device)+ camera_matrix = torch.rand(1, 3, 3).to(device)points3d = kornia.depth_to_3d(depth, camera_matrix)assert points3d.shape == (batch_size, 3, 3, 4)- def test_unproject(self):+ def test_unproject(self, device):depth = 2 * torch.tensor([[[[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],- ]]])+ ]]]).to(device)camera_matrix = torch.tensor([[[1., 0., 0.],[0., 1., 0.],[0., 0., 1.],- ]])+ ]]).to(device)points3d_expected = torch.tensor([[[[0.0000, 1.4142, 1.7889],@@ -62,39 +62,39 @@ class TestDepthTo3d:[1.4142, 1.1547, 0.8165],[0.8944, 0.8165, 0.6667],[0.6325, 0.6030, 0.5345],- ]]])+ ]]]).to(device)points3d = kornia.depth_to_3d(depth, camera_matrix)assert_allclose(points3d, points3d_expected)- def test_unproject_and_project(self):+ def test_unproject_and_project(self, device):depth = 2 * torch.tensor([[[[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],- ]]])+ ]]]).to(device)camera_matrix = torch.tensor([[[1., 0., 0.],[0., 1., 0.],[0., 0., 1.],- ]])+ ]]).to(device)points3d = kornia.depth_to_3d(depth, camera_matrix)points2d = kornia.project_points(points3d.permute(0, 2, 3, 1),camera_matrix[:, None, None])- points2d_expected = kornia.create_meshgrid(4, 3, False)+ points2d_expected = kornia.create_meshgrid(4, 3, False).to(device)assert_allclose(points2d, points2d_expected)- def test_gradcheck(self):+ def test_gradcheck(self, device):# generate input data- depth = torch.rand(1, 1, 3, 4)+ depth = torch.rand(1, 1, 3, 4).to(device)depth = utils.tensor_to_gradcheck_var(depth) # to var- camera_matrix = torch.rand(1, 3, 3)+ camera_matrix = torch.rand(1, 3, 3).to(device)camera_matrix = utils.tensor_to_gradcheck_var(camera_matrix) # to var# evaluate function gradient@@ -103,42 +103,42 @@ class TestDepthTo3d:class TestDepthToNormals:- def test_smoke(self):- depth = torch.rand(1, 1, 3, 4)- camera_matrix = torch.rand(1, 3, 3)+ def test_smoke(self, device):+ depth = torch.rand(1, 1, 3, 4).to(device)+ camera_matrix = torch.rand(1, 3, 3).to(device)points3d = kornia.depth_to_normals(depth, camera_matrix)assert points3d.shape == (1, 3, 3, 4)@pytest.mark.parametrize("batch_size", [2, 4, 5])- def test_shapes(self, batch_size):- depth = torch.rand(batch_size, 1, 3, 4)- camera_matrix = torch.rand(batch_size, 3, 3)+ def test_shapes(self, device, batch_size):+ depth = torch.rand(batch_size, 1, 3, 4).to(device)+ camera_matrix = torch.rand(batch_size, 3, 3).to(device)points3d = kornia.depth_to_normals(depth, camera_matrix)assert points3d.shape == (batch_size, 3, 3, 4)@pytest.mark.parametrize("batch_size", [2, 4, 5])- def test_shapes_broadcast(self, batch_size):- depth = torch.rand(batch_size, 1, 3, 4)- camera_matrix = torch.rand(1, 3, 3)+ def test_shapes_broadcast(self, device, batch_size):+ depth = torch.rand(batch_size, 1, 3, 4).to(device)+ camera_matrix = torch.rand(1, 3, 3).to(device)points3d = kornia.depth_to_normals(depth, camera_matrix)assert points3d.shape == (batch_size, 3, 3, 4)- def test_simple(self):+ def test_simple(self, device):depth = 2 * torch.tensor([[[[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],- ]]])+ ]]]).to(device)camera_matrix = torch.tensor([[[1., 0., 0.],[0., 1., 0.],[0., 0., 1.],- ]])+ ]]).to(device)normals_expected = torch.tensor([[[[0.3432, 0.4861, 0.7628],@@ -155,17 +155,17 @@ class TestDepthToNormals:[0.8253, 0.7981, 0.6415],[0.5432, 0.5807, 0.5105],[0.4129, 0.4824, 0.4784],- ]]])+ ]]]).to(device)normals = kornia.depth_to_normals(depth, camera_matrix)assert_allclose(normals, normals_expected, 1e-3, 1e-3)- def test_gradcheck(self):+ def test_gradcheck(self, device):# generate input data- depth = torch.rand(1, 1, 3, 4)+ depth = torch.rand(1, 1, 3, 4).to(device)depth = utils.tensor_to_gradcheck_var(depth) # to var- camera_matrix = torch.rand(1, 3, 3)+ camera_matrix = torch.rand(1, 3, 3).to(device)camera_matrix = utils.tensor_to_gradcheck_var(camera_matrix) # to var# evaluate function gradient@@ -174,77 +174,77 @@ class TestDepthToNormals:class TestWarpFrameDepth:- def test_smoke(self):- image_src = torch.rand(1, 3, 3, 4)- depth_dst = torch.rand(1, 1, 3, 4)- src_trans_dst = torch.rand(1, 4, 4)- camera_matrix = torch.rand(1, 3, 3)+ def test_smoke(self, device):+ image_src = torch.rand(1, 3, 3, 4).to(device)+ depth_dst = torch.rand(1, 1, 3, 4).to(device)+ src_trans_dst = torch.rand(1, 4, 4).to(device)+ camera_matrix = torch.rand(1, 3, 3).to(device)image_dst = kornia.warp_frame_depth(image_src, depth_dst, src_trans_dst, camera_matrix)assert image_dst.shape == (1, 3, 3, 4)@pytest.mark.parametrize("batch_size", [2, 4, 5])@pytest.mark.parametrize("num_features", [1, 3, 5])- def test_shape(self, batch_size, num_features):- image_src = torch.rand(batch_size, num_features, 3, 4)- depth_dst = torch.rand(batch_size, 1, 3, 4)- src_trans_dst = torch.rand(batch_size, 4, 4)- camera_matrix = torch.rand(batch_size, 3, 3)+ def test_shape(self, device, batch_size, num_features):+ image_src = torch.rand(batch_size, num_features, 3, 4).to(device)+ depth_dst = torch.rand(batch_size, 1, 3, 4).to(device)+ src_trans_dst = torch.rand(batch_size, 4, 4).to(device)+ camera_matrix = torch.rand(batch_size, 3, 3).to(device)image_dst = kornia.warp_frame_depth(image_src, depth_dst, src_trans_dst, camera_matrix)assert image_dst.shape == (batch_size, num_features, 3, 4)- def test_translation(self):+ def test_translation(self, device):image_src = torch.tensor([[[[1., 2., 3.],[1., 2., 3.],[1., 2., 3.],[1., 2., 3.],- ]]])+ ]]]).to(device)depth_dst = torch.tensor([[[[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],- ]]])+ ]]]).to(device)src_trans_dst = torch.tensor([[[1., 0., 0., 1.],[0., 1., 0., 0.],[0., 0., 1., 0.],[0., 0., 0., 1.],- ]])+ ]]).to(device)h, w = image_src.shape[-2:]camera_matrix = torch.tensor([[[1., 0., w / 2],[0., 1., h / 2],[0., 0., 1.],- ]])+ ]]).to(device)image_dst_expected = torch.tensor([[[[0.9223, 0.0000, 0.0000],[2.8153, 1.5000, 0.0000],[2.8028, 2.6459, 0.0000],[2.8153, 1.5000, 0.0000],- ]]])+ ]]]).to(device)image_dst = kornia.warp_frame_depth(image_src, depth_dst, src_trans_dst, camera_matrix)assert_allclose(image_dst, image_dst_expected, 1e-3, 1e-3)- def test_gradcheck(self):- image_src = torch.rand(1, 3, 3, 4)+ def test_gradcheck(self, device):+ image_src = torch.rand(1, 3, 3, 4).to(device)image_src = utils.tensor_to_gradcheck_var(image_src) # to var- depth_dst = torch.rand(1, 1, 3, 4)+ depth_dst = torch.rand(1, 1, 3, 4).to(device)depth_dst = utils.tensor_to_gradcheck_var(depth_dst) # to var- src_trans_dst = torch.rand(1, 4, 4)+ src_trans_dst = torch.rand(1, 4, 4).to(device)src_trans_dst = utils.tensor_to_gradcheck_var(src_trans_dst) # to var- camera_matrix = torch.rand(1, 3, 3)+ camera_matrix = torch.rand(1, 3, 3).to(device)camera_matrix = utils.tensor_to_gradcheck_var(camera_matrix) # to var# evaluate function gradient