π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
add more shape tests for depth_to_3d
edgarriba1831766cf8a2
diff --git a/kornia/geometry/camera/perspective.py b/kornia/geometry/camera/perspective.pyindex cbeebfa30..a4e19282c 100644--- a/kornia/geometry/camera/perspective.py+++ b/kornia/geometry/camera/perspective.py@@ -114,21 +114,20 @@ def unproject_points(# y = (v - cy) * Z / fy# unpack coordinates-- u_coord: torch.Tensor = point_2d[..., 0:1]- v_coord: torch.Tensor = point_2d[..., 1:2]+ u_coord: torch.Tensor = point_2d[..., 0]+ v_coord: torch.Tensor = point_2d[..., 1]# unpack intrinsics- fx: torch.Tensor = camera_matrix[..., 0:1, 0]- fy: torch.Tensor = camera_matrix[..., 1:2, 1]- cx: torch.Tensor = camera_matrix[..., 0:1, 2]- cy: torch.Tensor = camera_matrix[..., 1:2, 2]+ fx: torch.Tensor = camera_matrix[..., 0, 0]+ fy: torch.Tensor = camera_matrix[..., 1, 1]+ cx: torch.Tensor = camera_matrix[..., 0, 2]+ cy: torch.Tensor = camera_matrix[..., 1, 2]# projectivex_coord: torch.Tensor = (u_coord - cx) / fxy_coord: torch.Tensor = (v_coord - cy) / fy- xyz: torch.Tensor = torch.cat([x_coord, y_coord], dim=-1)+ xyz: torch.Tensor = torch.stack([x_coord, y_coord], dim=-1)xyz = convert_points_to_homogeneous(xyz)if normalize:diff --git a/kornia/geometry/depth.py b/kornia/geometry/depth.pyindex 9756b3628..b7347e316 100644--- a/kornia/geometry/depth.py+++ b/kornia/geometry/depth.py@@ -33,11 +33,11 @@ def depth_to_3d(depth: torch.Tensor, camera_matrix: torch.Tensor) -> torch.Tensof"Got {type(camera_matrix)}.")if not len(camera_matrix.shape) == 3 and camera_matrix.shape[-2:] == (3, 3):- raise ValueError(f"Input camera_matrix musth have a shape (B, 3, 3). "+ raise ValueError(f"Input camera_matrix must have a shape (B, 3, 3). "f"Got: {camera_matrix.shape}.")# create base coordinates grid- height, width = depth.shape[-2:]+ batch_size, _, height, width = depth.shapepoints_2d: torch.Tensor = create_meshgrid(height, width, normalized_coordinates=False) # 1xHxWx2points_2d = points_2d.to(depth.device).to(depth.dtype)@@ -46,7 +46,8 @@ def depth_to_3d(depth: torch.Tensor, camera_matrix: torch.Tensor) -> torch.Tensopoints_depth: torch.Tensor = depth.permute(0, 2, 3, 1) # 1xHxWx1# project pixels to camera frame+ camera_matrix_tmp: torch.Tensor = camera_matrix[:, None, None] # Bx1x1x3x3points_3d: torch.Tensor = unproject_points(- points_2d, points_depth, camera_matrix, normalize=True) # BxHxWx3+ points_2d, points_depth, camera_matrix_tmp, normalize=True) # BxHxWx3return points_3d.permute(0, 3, 1, 2) # Bx3xHxWdiff --git a/test/geometry/test_depth.py b/test/geometry/test_depth.pyindex 9bca8e03d..28771a96d 100644--- a/test/geometry/test_depth.py+++ b/test/geometry/test_depth.py@@ -10,7 +10,6 @@ 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)@@ -18,6 +17,22 @@ class TestDepthTo3D: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)++ 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)++ points3d = kornia.depth_to_3d(depth, camera_matrix)+ assert points3d.shape == (batch_size, 3, 3, 4)+def test_gradcheck(self):# generate input datadepth = torch.rand(1, 1, 3, 4)