facebookresearch / facebookresearch/meshflow
IndexError in build_topology() when input mesh has fewer vertices than n_verts
- Dominant language
- Python
- Stars
- 512
- Forks
- 27
- PR merge metrics
- No merged PRs in 30d
Description
Hi, thanks for releasing MeshFlow.
I encountered an `IndexError` when running `inference_vae.py` on a simple `.ply` mesh whose number of vertices is smaller than the model's configured `n_verts=4096`.
## Reproduction
I created a simple test sphere:
```python
import trimesh
mesh = trimesh.creation.icosphere(subdivisions=4, radius=1.0)
mesh.export("data/test/sphere.ply")
print(mesh.vertices.shape, mesh.faces.shape)
```
Then I ran:
```bash
CUDA_VISIBLE_DEVICES=0 python inference_vae.py \
--model_path ckpt/meshflow \
--input data/test/sphere.ply \
--output outputs/meshflow_vae/sphere_test \
--dtype bf16
```
## Error
```text
Loaded MeshFlowVAE from ckpt/meshflow
n_verts=4096 max_degree=50 point_feats_type=['embed@verts_normal', 'neighbor_points', 'embed@degree', 'embed@verts_mask']
sample_posterior=True dynamic_latent=True device=cuda dtype=bf16
Found 1 meshes in data/test/sphere.ply
Traceback (most recent call last):
File "inference_vae.py", line 168, in
main()
File "inference_vae.py", line 128, in main
topo = mesh.build_topology(
File "meshflow/utils/mesh.py", line 566, in build_topology
nearest_verts_idx = face_vertices[
IndexError: index 484 is out of bounds for dimension 1 with size 3
```
## Suspected cause
The error seems to happen in the padding branch when the input mesh has fewer vertices than `n_verts`.
In `meshflow/utils/mesh.py`, the following code appears to compute distances using face vertex indices instead of vertex coordinates:
```python
face_vertices = m.faces[negative_face_idx]
dist2 = (face_vertices - negative_verts[:, None, :]).pow(2).sum(dim=-1)
nearest_choice = dist2.argmin(dim=1)
nearest_verts_idx = face_vertices[
torch.arange(n_negative_verts, device=device),
nearest_choice,
]
```
Since `face_vertices` has shape `[N, 3]`, `nearest_choice` is expected to be in `{0, 1, 2}`. However, because the distance is computed against index values rather than 3D coordinates, `argmin` can produce invalid indices, which causes the out-of-bounds error.
## Temporary fix
The following patch fixed the issue on my side:
```python
face_vertices = m.faces[negative_face_idx]
face_vertex_pos = m.verts[face_vertices]
dist2 = (face_vertex_pos - negative_verts[:, None, :]).pow(2).sum(dim=-1)
nearest_choice = dist2.argmin(dim=1)
nearest_verts_idx = face_vertices[
torch.arange(n_negative_verts, device=device),
nearest_choice,
]
```
After this modification, `inference_vae.py` runs successfully and generates the reconstructed mesh.
Could you please confirm whether this is the intended fix, or whether the padding topology construction should be handled differently?
Thanks.
Contributor guide
Assessment
This issue has not been assessed yet.