Skip to content

Commit 6c48ff6

Browse files
gkioxarifacebook-github-bot
authored andcommitted
replace view with reshape, check for nans
Summary: Replace view with reshape, add check for nans before mesh sampling Reviewed By: nikhilaravi Differential Revision: D20548456 fbshipit-source-id: c4e1b88e033ecb8f0f3a8f3a33a04ce13a5b5043
1 parent 5359977 commit 6c48ff6

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

pytorch3d/ops/sample_points_from_meshes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def sample_points_from_meshes(
4141
raise ValueError("Meshes are empty.")
4242

4343
verts = meshes.verts_packed()
44+
if not torch.isfinite(verts).all():
45+
raise ValueError("Meshes contain nan or inf.")
4446
faces = meshes.faces_packed()
4547
mesh_to_face = meshes.mesh_to_faces_packed_first_idx()
4648
num_meshes = len(meshes)

pytorch3d/renderer/mesh/texturing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def interpolate_texture_map(fragments, meshes) -> torch.Tensor:
5353
N, H_in, W_in, C = texture_maps.shape # 3 for RGB
5454

5555
# pixel_uvs: (N, H, W, K, 2) -> (N, K, H, W, 2) -> (NK, H, W, 2)
56-
pixel_uvs = pixel_uvs.permute(0, 3, 1, 2, 4).view(N * K, H_out, W_out, 2)
56+
pixel_uvs = pixel_uvs.permute(0, 3, 1, 2, 4).reshape(N * K, H_out, W_out, 2)
5757

5858
# textures.map:
5959
# (N, H, W, C) -> (N, C, H, W) -> (1, N, C, H, W)
@@ -81,7 +81,7 @@ def interpolate_texture_map(fragments, meshes) -> torch.Tensor:
8181
if texture_maps.device != pixel_uvs.device:
8282
texture_maps = texture_maps.to(pixel_uvs.device)
8383
texels = F.grid_sample(texture_maps, pixel_uvs, align_corners=False)
84-
texels = texels.view(N, K, C, H_out, W_out).permute(0, 3, 4, 1, 2)
84+
texels = texels.reshape(N, K, C, H_out, W_out).permute(0, 3, 4, 1, 2)
8585
return texels
8686

8787

pytorch3d/structures/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def padded_to_packed(
191191
"Only one of split_size or pad_value should be provided."
192192
)
193193

194-
x_packed = x.view(-1, D) # flatten padded
194+
x_packed = x.reshape(-1, D) # flatten padded
195195

196196
if pad_value is None and split_size is None:
197197
return x_packed

tests/test_sample_points_from_meshes.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,26 @@ def test_multinomial_weights(self):
291291
if sampled_weights.min() <= 0:
292292
return False
293293
return True
294+
295+
def test_verts_nan(self):
296+
num_verts = 30
297+
num_faces = 50
298+
for device in ["cpu", "cuda:0"]:
299+
for invalid in ["nan", "inf"]:
300+
verts = torch.rand(
301+
(num_verts, 3), dtype=torch.float32, device=device
302+
)
303+
# randomly assign an invalid type
304+
verts[torch.randperm(num_verts)[:10]] = float(invalid)
305+
faces = torch.randint(
306+
num_verts, size=(num_faces, 3), dtype=torch.int64, device=device
307+
)
308+
meshes = Meshes(verts=[verts], faces=[faces])
309+
310+
with self.assertRaisesRegex(ValueError, "Meshes contain nan or inf."):
311+
sample_points_from_meshes(
312+
meshes, num_samples=100, return_normals=True
313+
)
294314

295315
@staticmethod
296316
def sample_points_with_init(

0 commit comments

Comments
 (0)