Skip to content

Commit eb04a48

Browse files
JudyYefacebook-github-bot
authored andcommitted
TexturesVertex._num_verts_per_mesh deep copy (#623)
Summary: When a list of Meshes is `join_batched()`, the `num_verts_per_mesh` in the list would be unexpectedly modified. Also some cleanup around `_num_verts_per_mesh`. Pull Request resolved: #623 Test Plan: A modification to an existing test checks this. Reviewed By: nikhilaravi Differential Revision: D27682104 Pulled By: bottler fbshipit-source-id: 9d00913dfb4869bd6c7d3f5cc9156b7b6f1aecc9
1 parent 8660db9 commit eb04a48

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

pytorch3d/renderer/mesh/textures.py

+6-17
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,7 @@ def verts_uvs_list(self) -> List[torch.Tensor]:
842842
else:
843843
# The number of vertices in the mesh and in verts_uvs can differ
844844
# e.g. if a vertex is shared between 3 faces, it can
845-
# have up to 3 different uv coordinates. Therefore we cannot
846-
# convert directly from padded to list using _num_verts_per_mesh
845+
# have up to 3 different uv coordinates.
847846
self._verts_uvs_list = list(self._verts_uvs_padded.unbind(0))
848847
return self._verts_uvs_list
849848

@@ -1283,25 +1282,15 @@ def clone(self):
12831282
tex = self.__class__(self.verts_features_padded().clone())
12841283
if self._verts_features_list is not None:
12851284
tex._verts_features_list = [f.clone() for f in self._verts_features_list]
1286-
num_verts = (
1287-
self._num_verts_per_mesh.clone()
1288-
if torch.is_tensor(self._num_verts_per_mesh)
1289-
else self._num_verts_per_mesh
1290-
)
1291-
tex._num_verts_per_mesh = num_verts
1285+
tex._num_verts_per_mesh = self._num_verts_per_mesh.copy()
12921286
tex.valid = self.valid.clone()
12931287
return tex
12941288

12951289
def detach(self):
12961290
tex = self.__class__(self.verts_features_padded().detach())
12971291
if self._verts_features_list is not None:
12981292
tex._verts_features_list = [f.detach() for f in self._verts_features_list]
1299-
num_verts = (
1300-
self._num_verts_per_mesh.detach()
1301-
if torch.is_tensor(self._num_verts_per_mesh)
1302-
else self._num_verts_per_mesh
1303-
)
1304-
tex._num_verts_per_mesh = num_verts
1293+
tex._num_verts_per_mesh = self._num_verts_per_mesh.copy()
13051294
tex.valid = self.valid.detach()
13061295
return tex
13071296

@@ -1414,13 +1403,13 @@ def join_batch(self, textures: List["TexturesVertex"]) -> "TexturesVertex":
14141403

14151404
verts_features_list = []
14161405
verts_features_list += self.verts_features_list()
1417-
num_faces_per_mesh = self._num_verts_per_mesh
1406+
num_verts_per_mesh = self._num_verts_per_mesh.copy()
14181407
for tex in textures:
14191408
verts_features_list += tex.verts_features_list()
1420-
num_faces_per_mesh += tex._num_verts_per_mesh
1409+
num_verts_per_mesh += tex._num_verts_per_mesh
14211410

14221411
new_tex = self.__class__(verts_features=verts_features_list)
1423-
new_tex._num_verts_per_mesh = num_faces_per_mesh
1412+
new_tex._num_verts_per_mesh = num_verts_per_mesh
14241413
return new_tex
14251414

14261415
def join_scene(self) -> "TexturesVertex":

tests/test_io_obj.py

+5
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,11 @@ def check_item(x, y):
786786
mesh_rgb = Meshes(verts=[verts], faces=[faces], textures=rgb_tex)
787787
mesh_rgb3 = join_meshes_as_batch([mesh_rgb, mesh_rgb, mesh_rgb])
788788
check_triple(mesh_rgb, mesh_rgb3)
789+
nums_rgb = mesh_rgb.textures._num_verts_per_mesh
790+
nums_rgb3 = mesh_rgb3.textures._num_verts_per_mesh
791+
self.assertEqual(type(nums_rgb), list)
792+
self.assertEqual(type(nums_rgb3), list)
793+
self.assertListEqual(nums_rgb * 3, nums_rgb3)
789794

790795
# meshes with texture atlas, join into a batch.
791796
device = "cuda:0"

0 commit comments

Comments
 (0)