diff --git a/pytorch3d/ops/sample_points_from_meshes.py b/pytorch3d/ops/sample_points_from_meshes.py index 7d97a2906..d96e407de 100644 --- a/pytorch3d/ops/sample_points_from_meshes.py +++ b/pytorch3d/ops/sample_points_from_meshes.py @@ -41,6 +41,8 @@ def sample_points_from_meshes( raise ValueError("Meshes are empty.") verts = meshes.verts_packed() + if not torch.isfinite(verts).all(): + raise ValueError("Meshes contain nan or inf.") faces = meshes.faces_packed() mesh_to_face = meshes.mesh_to_faces_packed_first_idx() num_meshes = len(meshes) diff --git a/pytorch3d/renderer/mesh/texturing.py b/pytorch3d/renderer/mesh/texturing.py index 1891e3fa3..c57ad4555 100644 --- a/pytorch3d/renderer/mesh/texturing.py +++ b/pytorch3d/renderer/mesh/texturing.py @@ -53,7 +53,7 @@ def interpolate_texture_map(fragments, meshes) -> torch.Tensor: N, H_in, W_in, C = texture_maps.shape # 3 for RGB # pixel_uvs: (N, H, W, K, 2) -> (N, K, H, W, 2) -> (NK, H, W, 2) - pixel_uvs = pixel_uvs.permute(0, 3, 1, 2, 4).view(N * K, H_out, W_out, 2) + pixel_uvs = pixel_uvs.permute(0, 3, 1, 2, 4).reshape(N * K, H_out, W_out, 2) # textures.map: # (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: if texture_maps.device != pixel_uvs.device: texture_maps = texture_maps.to(pixel_uvs.device) texels = F.grid_sample(texture_maps, pixel_uvs, align_corners=False) - texels = texels.view(N, K, C, H_out, W_out).permute(0, 3, 4, 1, 2) + texels = texels.reshape(N, K, C, H_out, W_out).permute(0, 3, 4, 1, 2) return texels diff --git a/pytorch3d/structures/utils.py b/pytorch3d/structures/utils.py index 9fc1c81dc..dcaadafea 100644 --- a/pytorch3d/structures/utils.py +++ b/pytorch3d/structures/utils.py @@ -191,7 +191,7 @@ def padded_to_packed( "Only one of split_size or pad_value should be provided." ) - x_packed = x.view(-1, D) # flatten padded + x_packed = x.reshape(-1, D) # flatten padded if pad_value is None and split_size is None: return x_packed diff --git a/tests/test_sample_points_from_meshes.py b/tests/test_sample_points_from_meshes.py index e26267290..7225de5b0 100644 --- a/tests/test_sample_points_from_meshes.py +++ b/tests/test_sample_points_from_meshes.py @@ -291,6 +291,26 @@ def test_multinomial_weights(self): if sampled_weights.min() <= 0: return False return True + + def test_verts_nan(self): + num_verts = 30 + num_faces = 50 + for device in ["cpu", "cuda:0"]: + for invalid in ["nan", "inf"]: + verts = torch.rand( + (num_verts, 3), dtype=torch.float32, device=device + ) + # randomly assign an invalid type + verts[torch.randperm(num_verts)[:10]] = float(invalid) + faces = torch.randint( + num_verts, size=(num_faces, 3), dtype=torch.int64, device=device + ) + meshes = Meshes(verts=[verts], faces=[faces]) + + with self.assertRaisesRegex(ValueError, "Meshes contain nan or inf."): + sample_points_from_meshes( + meshes, num_samples=100, return_normals=True + ) @staticmethod def sample_points_with_init(