π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Added tests, fixed docstrings and made some other changes (#1211)
Bharath Raj424ca1da65af
diff --git a/kornia/geometry/camera/pinhole.py b/kornia/geometry/camera/pinhole.pyindex ae9488708..613edb62e 100644--- a/kornia/geometry/camera/pinhole.py+++ b/kornia/geometry/camera/pinhole.py@@ -561,10 +561,10 @@ def pixel2cam(depth: torch.Tensor, intrinsics_inv: torch.Tensor, pixel_coords: tArgs:depth: the source depth maps. Shape must be Bx1xHxW.intrinsics_inv: the inverse intrinsics camera matrix. Shape must be Bx4x4.- pixel_coords: the grid with the homogeneous camera coordinates. Shape must be BxHxWx3.+ pixel_coords: the grid with (u, v, 1) pixel coordinates. Shape must be BxHxWx3.Returns:- tensor of (u, v, 1) cam coordinates with shape BxHxWx3.+ tensor of shape BxHxWx3 with (x, y, z) cam coordinates."""if not len(depth.shape) == 4 and depth.shape[1] == 1:raise ValueError("Input depth has to be in the shape of " "Bx1xHxW. Got {}".format(depth.shape))@@ -580,16 +580,17 @@ def pixel2cam(depth: torch.Tensor, intrinsics_inv: torch.Tensor, pixel_coords: t# https://github.com/ClementPinard/SfmLearner-Pytorch/blob/master/inverse_warp.py#L43-def cam2pixel(cam_coords_src: torch.Tensor, dst_proj_src: torch.Tensor, eps: float = 1e-6) -> torch.Tensor:+def cam2pixel(cam_coords_src: torch.Tensor, dst_proj_src: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:r"""Transform coordinates in the camera frame to the pixel frame.Args:- cam_coords: pixel coordinates defined in the first camera coordinates system. Shape must be BxHxWx3.+ cam_coords: (x, y, z) coordinates defined in the first camera coordinates system. Shape must be BxHxWx3.dst_proj_src: the projection matrix between thereference and the non reference camera frame. Shape must be Bx4x4.+ eps: small value to avoid division by zero error.Returns:- tensor of [-1, 1] coordinates of shape BxHxWx2.+ tensor of shape BxHxWx2 with (u, v) pixel coordinates."""if not len(cam_coords_src.shape) == 4 and cam_coords_src.shape[3] == 3:raise ValueError(@@ -610,7 +611,7 @@ def cam2pixel(cam_coords_src: torch.Tensor, dst_proj_src: torch.Tensor, eps: flo# stack and return the coordinates, that's the actual flowpixel_coords_dst: torch.Tensor = torch.stack([u_coord, v_coord], dim=-1)- return pixel_coords_dst # (B*N)xHxWx2+ return pixel_coords_dst # BxHxWx2# layer apidiff --git a/test/geometry/test_pinhole.py b/test/geometry/test_pinhole.pyindex 234f72d94..399b8d3a2 100644--- a/test/geometry/test_pinhole.py+++ b/test/geometry/test_pinhole.py@@ -1,7 +1,211 @@+import pytestimport torch+from torch.autograd import gradcheckimport kornia-from kornia.testing import assert_close+from kornia.testing import assert_close, tensor_to_gradcheck_var+++class TestCam2Pixel:+ def _create_intrinsics(self, batch_size, fx, fy, cx, cy, device, dtype):+ temp = torch.eye(4, device=device, dtype=dtype)+ temp[0, 0], temp[0, 2] = fx, cx+ temp[1, 1], temp[1, 2] = fy, cy+ intrinsics = temp.expand(batch_size, -1, -1)+ return intrinsics++ def _create_intrinsics_inv(self, batch_size, fx, fy, cx, cy, device, dtype):+ temp = torch.eye(4, device=device, dtype=dtype)+ temp[0, 0], temp[0, 2] = 1 / fx, -cx / fx+ temp[1, 1], temp[1, 2] = 1 / fy, -cy / fy+ intrinsics_inv = temp.expand(batch_size, -1, -1)+ return intrinsics_inv++ def _get_samples(self, shape, low, high, device, dtype):+ """Returns a tensor having the given shape and whose values are in the range [low, high)"""+ return ((high - low) * torch.rand(shape, device=device, dtype=dtype)) + low++ @pytest.mark.parametrize("batch_size", (1, 2, 5))+ def test_smoke(self, batch_size, device, dtype):+ H, W = 250, 500+ fx, fy = W, H+ cx, cy = W / 2, H / 2+ eps = 1e-12+ seed = 77+ low, high = -500, 500++ intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy, device=device, dtype=dtype)++ # Setting the projection matrix to the intrinsic matrix for+ # simplicity (i.e. assuming that the RT matrix is an identity matrix)+ proj_mat = intrinsics++ torch.manual_seed(seed)+ cam_coords_src = self._get_samples((batch_size, H, W, 3), low, high, device, dtype)++ pixel_coords_dst = kornia.cam2pixel(cam_coords_src=cam_coords_src, dst_proj_src=proj_mat, eps=eps)+ assert pixel_coords_dst.shape == (batch_size, H, W, 2)++ @pytest.mark.parametrize("batch_size", (1, 2, 5))+ def test_consistency(self, batch_size, device, dtype):+ H, W = 250, 500+ fx, fy = W, H+ cx, cy = W / 2, H / 2+ eps = 1e-12+ seed = 77+ low, high = -500, 500++ intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy, device=device, dtype=dtype)+ intrinsics_inv = self._create_intrinsics_inv(batch_size, fx, fy, cx, cy, device=device, dtype=dtype)++ # Setting the projection matrix to the intrinsic matrix for+ # simplicity (i.e. assuming that the RT matrix is an identity matrix)+ proj_mat = intrinsics++ torch.manual_seed(seed)+ cam_coords_input = self._get_samples((batch_size, H, W, 3), low, high, device, dtype)++ pixel_coords_output = kornia.cam2pixel(cam_coords_src=cam_coords_input, dst_proj_src=proj_mat, eps=eps)++ last_ch = torch.ones((batch_size, H, W, 1), device=device, dtype=dtype)+ pixel_coords_concat = torch.cat([pixel_coords_output, last_ch], axis=-1)++ depth = cam_coords_input[..., 2:3].permute(0, 3, 1, 2).contiguous()+ cam_coords_output = kornia.pixel2cam(+ depth=depth, intrinsics_inv=intrinsics_inv, pixel_coords=pixel_coords_concat+ )++ assert_close(cam_coords_output, cam_coords_input, atol=1e-4, rtol=1e-4)++ @pytest.mark.parametrize("batch_size", (1, 2, 5))+ def test_gradcheck(self, batch_size, device, dtype):+ H, W = 10, 20+ fx, fy = W, H+ cx, cy = W / 2, H / 2+ eps = 1e-12+ seed = 77+ low, high = -500, 500+ atol, rtol = 1e-5, 1e-3++ # Different tolerances for the below case.+ if (device.type == "cuda") and ((dtype == "float64") or (dtype == torch.float64)):+ atol, rtol = 1e-4, 1e-2++ # If contiguous() is not called, gradcheck fails+ intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy, device=device, dtype=dtype).contiguous()++ # Setting the projection matrix to the intrinsic matrix for+ # simplicity (i.e. assuming that the RT matrix is an identity matrix)+ proj_mat = intrinsics++ torch.manual_seed(seed)+ cam_coords_src = self._get_samples((batch_size, H, W, 3), low, high, device, dtype)++ cam_coords_src = tensor_to_gradcheck_var(cam_coords_src)+ proj_mat = tensor_to_gradcheck_var(proj_mat)++ assert gradcheck(+ kornia.geometry.cam2pixel, (cam_coords_src, proj_mat, eps), raise_exception=True, atol=atol, rtol=rtol+ )+++class TestPixel2Cam:+ def _create_intrinsics(self, batch_size, fx, fy, cx, cy, device, dtype):+ temp = torch.eye(4, device=device, dtype=dtype)+ temp[0, 0], temp[0, 2] = fx, cx+ temp[1, 1], temp[1, 2] = fy, cy+ intrinsics = temp.expand(batch_size, -1, -1)+ return intrinsics++ def _create_intrinsics_inv(self, batch_size, fx, fy, cx, cy, device, dtype):+ temp = torch.eye(4, device=device, dtype=dtype)+ temp[0, 0], temp[0, 2] = 1 / fx, -cx / fx+ temp[1, 1], temp[1, 2] = 1 / fy, -cy / fy+ intrinsics_inv = temp.expand(batch_size, -1, -1)+ return intrinsics_inv++ def _get_samples(self, shape, low, high, device, dtype):+ """Returns a tensor having the given shape and whose values are in the range [low, high)"""+ return ((high - low) * torch.rand(shape, device=device, dtype=dtype)) + low++ @pytest.mark.parametrize("batch_size", (1, 2, 5))+ def test_smoke(self, batch_size, device, dtype):+ H, W = 250, 500+ fx, fy = W, H+ cx, cy = W / 2, H / 2+ seed = 77+ low_1, high_1 = -500, 500+ low_2, high_2 = -(max(W, H) * 3), (max(W, H) * 3)++ torch.manual_seed(seed)+ depth = self._get_samples((batch_size, 1, H, W), low_1, high_1, device, dtype)+ pixel_coords = self._get_samples((batch_size, H, W, 2), low_2, high_2, device, dtype)++ last_ch = torch.ones((batch_size, H, W, 1), device=device, dtype=dtype)+ pixel_coords_input = torch.cat([pixel_coords, last_ch], axis=-1)++ intrinsics_inv = self._create_intrinsics_inv(batch_size, fx, fy, cx, cy, device=device, dtype=dtype)++ output = kornia.pixel2cam(depth=depth, intrinsics_inv=intrinsics_inv, pixel_coords=pixel_coords_input)++ assert output.shape == (batch_size, H, W, 3)++ @pytest.mark.parametrize("batch_size", (1, 2, 5))+ def test_consistency(self, batch_size, device, dtype):+ H, W = 250, 500+ fx, fy = W, H+ cx, cy = W / 2, H / 2+ eps = 1e-12+ seed = 77+ low_1, high_1 = -500, 500+ low_2, high_2 = -(max(W, H) * 3), (max(W, H) * 3)++ torch.manual_seed(seed)+ depth = self._get_samples((batch_size, 1, H, W), low_1, high_1, device, dtype)+ pixel_coords = self._get_samples((batch_size, H, W, 2), low_2, high_2, device, dtype)++ last_ch = torch.ones((batch_size, H, W, 1), device=device, dtype=dtype)+ pixel_coords_input = torch.cat([pixel_coords, last_ch], axis=-1)++ intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy, device=device, dtype=dtype)+ intrinsics_inv = self._create_intrinsics_inv(batch_size, fx, fy, cx, cy, device=device, dtype=dtype)++ cam_coords = kornia.pixel2cam(depth=depth, intrinsics_inv=intrinsics_inv, pixel_coords=pixel_coords_input)++ # Setting the projection matrix to the intrinsic matrix for+ # simplicity (i.e. assuming that the RT matrix is an identity matrix)+ proj_mat = intrinsics+ pixel_coords_output = kornia.cam2pixel(cam_coords_src=cam_coords, dst_proj_src=proj_mat, eps=eps)+ pixel_coords_concat = torch.cat([pixel_coords_output, last_ch], axis=-1)++ assert_close(pixel_coords_concat, pixel_coords_input, atol=1e-4, rtol=1e-4)++ @pytest.mark.parametrize("batch_size", (1, 2, 5))+ def test_gradcheck(self, batch_size, device, dtype):+ H, W = 10, 20+ fx, fy = W, H+ cx, cy = W / 2, H / 2+ seed = 77+ low_1, high_1 = -500, 500+ low_2, high_2 = -(max(W, H) * 3), (max(W, H) * 3)++ torch.manual_seed(seed)+ depth = self._get_samples((batch_size, 1, H, W), low_1, high_1, device, dtype)+ pixel_coords = self._get_samples((batch_size, H, W, 2), low_2, high_2, device, dtype)++ last_ch = torch.ones((batch_size, H, W, 1), device=device, dtype=dtype)+ pixel_coords_input = torch.cat([pixel_coords, last_ch], axis=-1)++ # If contiguous() is not called, gradcheck fails+ intrinsics_inv = self._create_intrinsics_inv(+ batch_size, fx, fy, cx, cy, device=device, dtype=dtype+ ).contiguous()++ depth = tensor_to_gradcheck_var(depth)+ intrinsics_inv = tensor_to_gradcheck_var(intrinsics_inv)+ pixel_coords_input = tensor_to_gradcheck_var(pixel_coords_input)++ assert gradcheck(kornia.geometry.pixel2cam, (depth, intrinsics_inv, pixel_coords_input), raise_exception=True)class TestPinholeCamera: