Skip to content

Commit 45d096e

Browse files
bottlerfacebook-github-bot
authored andcommitted
cameras_from_opencv_projection device #1021
Summary: Fix #1021 that cameras_from_opencv_projection always creates on CPU. Reviewed By: nikhilaravi Differential Revision: D33508211 fbshipit-source-id: fadebd45cacafd633af6a58094cf6f654529992c
1 parent 39bb2ce commit 45d096e

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

pytorch3d/renderer/camera_conversions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def _cameras_from_opencv_projection(
5555
focal_length=focal_pytorch3d,
5656
principal_point=p0_pytorch3d,
5757
image_size=image_size,
58+
device=R.device,
5859
)
5960

6061

tests/test_camera_conversions.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_opencv_conversion(self):
6868
return correct projections of random 3D points. The check is done
6969
against a set of results precomuted using `cv2.projectPoints` function.
7070
"""
71-
71+
device = torch.device("cuda:0")
7272
image_size = [[480, 640]] * 4
7373
R = [
7474
[
@@ -116,17 +116,19 @@ def test_opencv_conversion(self):
116116
]
117117

118118
principal_point, focal_length, R, tvec, image_size = [
119-
torch.FloatTensor(x)
119+
torch.tensor(x, device=device)
120120
for x in (principal_point, focal_length, R, tvec, image_size)
121121
]
122-
camera_matrix = eyes(dim=3, N=4)
122+
camera_matrix = eyes(dim=3, N=4, device=device)
123123
camera_matrix[:, 0, 0], camera_matrix[:, 1, 1] = (
124124
focal_length[:, 0],
125125
focal_length[:, 1],
126126
)
127127
camera_matrix[:, :2, 2] = principal_point
128128

129-
pts = torch.nn.functional.normalize(torch.randn(4, 1000, 3), dim=-1)
129+
pts = torch.nn.functional.normalize(
130+
torch.randn(4, 1000, 3, device=device), dim=-1
131+
)
130132

131133
# project the 3D points with the opencv projection function
132134
rvec = so3_log_map(R)
@@ -136,6 +138,7 @@ def test_opencv_conversion(self):
136138
cameras_opencv_to_pytorch3d = cameras_from_opencv_projection(
137139
R, tvec, camera_matrix, image_size
138140
)
141+
self.assertEqual(cameras_opencv_to_pytorch3d.device, device)
139142

140143
# project the 3D points with converted cameras to screen space.
141144
pts_proj_pytorch3d_screen = cameras_opencv_to_pytorch3d.transform_points_screen(

0 commit comments

Comments
 (0)