From 06cdc313a7996a3363e78b19edaf893f4454ba1c Mon Sep 17 00:00:00 2001 From: Eric Young Date: Mon, 11 Dec 2023 12:49:17 -0800 Subject: [PATCH] PyTorch3D - Avoid flip in TexturesAtlas Summary: Performance improvement: Use torch.lerp to map uv coordinates to the range needed for grid_sample (i.e. map [0, 1] to [-1, 1] and invert the y-axis) Reviewed By: bottler Differential Revision: D51961728 fbshipit-source-id: db19a5e3f482e9af7b96b20f88a1e5d0076dac43 --- pytorch3d/renderer/mesh/textures.py | 16 ++++++++++++---- tests/test_render_meshes.py | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pytorch3d/renderer/mesh/textures.py b/pytorch3d/renderer/mesh/textures.py index 67abf2ef..5f134635 100644 --- a/pytorch3d/renderer/mesh/textures.py +++ b/pytorch3d/renderer/mesh/textures.py @@ -995,9 +995,13 @@ class TexturesUV(TexturesBase): # is the left-top pixel of input, and values x = 1, y = 1 is the # right-bottom pixel of input. - pixel_uvs = pixel_uvs * 2.0 - 1.0 + # map to a range of [-1, 1] and flip the y axis + pixel_uvs = torch.lerp( + pixel_uvs.new_tensor([-1.0, 1.0]), + pixel_uvs.new_tensor([1.0, -1.0]), + pixel_uvs, + ) - texture_maps = torch.flip(texture_maps, [2]) # flip y axis of the texture map if texture_maps.device != pixel_uvs.device: texture_maps = texture_maps.to(pixel_uvs.device) texels = F.grid_sample( @@ -1035,8 +1039,12 @@ class TexturesUV(TexturesBase): texture_maps = self.maps_padded() # NxHxWxC texture_maps = texture_maps.permute(0, 3, 1, 2) # NxCxHxW - faces_verts_uvs = faces_verts_uvs * 2.0 - 1.0 - texture_maps = torch.flip(texture_maps, [2]) # flip y axis of the texture map + # map to a range of [-1, 1] and flip the y axis + faces_verts_uvs = torch.lerp( + faces_verts_uvs.new_tensor([-1.0, 1.0]), + faces_verts_uvs.new_tensor([1.0, -1.0]), + faces_verts_uvs, + ) textures = F.grid_sample( texture_maps, diff --git a/tests/test_render_meshes.py b/tests/test_render_meshes.py index f6b91a93..61fe4641 100644 --- a/tests/test_render_meshes.py +++ b/tests/test_render_meshes.py @@ -932,7 +932,7 @@ class TestRenderMeshes(TestCaseMixin, unittest.TestCase): ) ).save(DATA_DIR / f"DEBUG_test_joinuvs{i}_map3.png") - self.assertClose(output, merged) + self.assertClose(output, merged, atol=0.005) self.assertClose(output, image_ref, atol=0.005) self.assertClose(mesh.textures.maps_padded()[0].cpu(), map_ref, atol=0.05)