From 45d096e219379bd0b3d14bbe4633b09f818f988d Mon Sep 17 00:00:00 2001 From: Jeremy Reizenstein Date: Fri, 21 Jan 2022 05:29:04 -0800 Subject: [PATCH] cameras_from_opencv_projection device #1021 Summary: Fix https://github.com/facebookresearch/pytorch3d/issues/1021 that cameras_from_opencv_projection always creates on CPU. Reviewed By: nikhilaravi Differential Revision: D33508211 fbshipit-source-id: fadebd45cacafd633af6a58094cf6f654529992c --- pytorch3d/renderer/camera_conversions.py | 1 + tests/test_camera_conversions.py | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pytorch3d/renderer/camera_conversions.py b/pytorch3d/renderer/camera_conversions.py index b1cc3a68..9fb73e5f 100644 --- a/pytorch3d/renderer/camera_conversions.py +++ b/pytorch3d/renderer/camera_conversions.py @@ -55,6 +55,7 @@ def _cameras_from_opencv_projection( focal_length=focal_pytorch3d, principal_point=p0_pytorch3d, image_size=image_size, + device=R.device, ) diff --git a/tests/test_camera_conversions.py b/tests/test_camera_conversions.py index 4e8106c7..47c26665 100644 --- a/tests/test_camera_conversions.py +++ b/tests/test_camera_conversions.py @@ -68,7 +68,7 @@ class TestCameraConversions(TestCaseMixin, unittest.TestCase): return correct projections of random 3D points. The check is done against a set of results precomuted using `cv2.projectPoints` function. """ - + device = torch.device("cuda:0") image_size = [[480, 640]] * 4 R = [ [ @@ -116,17 +116,19 @@ class TestCameraConversions(TestCaseMixin, unittest.TestCase): ] principal_point, focal_length, R, tvec, image_size = [ - torch.FloatTensor(x) + torch.tensor(x, device=device) for x in (principal_point, focal_length, R, tvec, image_size) ] - camera_matrix = eyes(dim=3, N=4) + camera_matrix = eyes(dim=3, N=4, device=device) camera_matrix[:, 0, 0], camera_matrix[:, 1, 1] = ( focal_length[:, 0], focal_length[:, 1], ) camera_matrix[:, :2, 2] = principal_point - pts = torch.nn.functional.normalize(torch.randn(4, 1000, 3), dim=-1) + pts = torch.nn.functional.normalize( + torch.randn(4, 1000, 3, device=device), dim=-1 + ) # project the 3D points with the opencv projection function rvec = so3_log_map(R) @@ -136,6 +138,7 @@ class TestCameraConversions(TestCaseMixin, unittest.TestCase): cameras_opencv_to_pytorch3d = cameras_from_opencv_projection( R, tvec, camera_matrix, image_size ) + self.assertEqual(cameras_opencv_to_pytorch3d.device, device) # project the 3D points with converted cameras to screen space. pts_proj_pytorch3d_screen = cameras_opencv_to_pytorch3d.transform_points_screen(