facebookresearch / pytorch3d

PyTorch3D is FAIR's library of reusable components for deep learning with 3D data
https://pytorch3d.org/
Other
8.81k stars 1.32k forks source link

Problem loading certain models with texture information #1594

Open idilsulo opened 1 year ago

idilsulo commented 1 year ago

Hi! I have a problem regarding loading certain model files in PyTorch3D.

🐛 Bugs / Unexpected behaviors

This occurs when I try to load certain objects with their corresponding material information with load_obj.

verts, faces, aux = pytorch3d.io.load_obj("{}model.obj".format(path),
    create_texture_atlas=True,
    device=device)

I get an error for most of the files like the following. Note that the index and size throwing this error changes across different models, but the line throwing this error is the same.

 verts, faces, aux = pytorch3d.io.load_obj("{}model.obj".format(path),
  File "/opt/homebrew/lib/python3.10/site-packages/pytorch3d/io/obj_io.py", line 222, in load_obj
    return _load_obj(
  File "/opt/homebrew/lib/python3.10/site-packages/pytorch3d/io/obj_io.py", line 652, in _load_obj
    texture_atlas = make_mesh_texture_atlas(
  File "/opt/homebrew/lib/python3.10/site-packages/pytorch3d/io/mtl_io.py", line 86, in make_mesh_texture_atlas
    faces_verts_uvs = verts_uvs[faces_uvs] if len(verts_uvs) > 0 else None
IndexError: index 976 is out of bounds for dimension 0 with size 98

When the texture is not considered, the objects are correctly loaded with the same method, so the problem is regarding the material information. This happens due to the following line in mtl_io.py:

https://github.com/facebookresearch/pytorch3d/blob/6462aa60eacbc339714064befea203d5b255521c/pytorch3d/io/mtl_io.py#L86

Interestingly, it is possible to load these models in other libraries such as Trimesh. The models can also be correctly opened via MeshLab.

Attached you can also find an example model causing this error.

Thank you very much for your help!

model.zip

idilsulo commented 1 year ago

@bottler - Also another error related to the load_obj method, this one is thrown when I try to load a ShapeNet model:

 <module>
    meshes = get_shapenet_pointcloud(path)
  File "/Users/idil/Desktop/pointcloudvlm/pointcloudvlm/render/shapenet_render.py", line 84, in get_shapenet_pointcloud
    verts, faces, aux = pytorch3d.io.load_obj("{}models/model_normalized.obj".format(path),
  File "/opt/homebrew/lib/python3.10/site-packages/pytorch3d/io/obj_io.py", line 224, in load_obj
    return _load_obj(
  File "/opt/homebrew/lib/python3.10/site-packages/pytorch3d/io/obj_io.py", line 627, in _load_obj
    material_colors, texture_images = _load_materials(
  File "/opt/homebrew/lib/python3.10/site-packages/pytorch3d/io/obj_io.py", line 552, in _load_materials
    return load_mtl(
  File "/opt/homebrew/lib/python3.10/site-packages/pytorch3d/io/mtl_io.py", line 529, in load_mtl
    return _load_texture_images(
  File "/opt/homebrew/lib/python3.10/site-packages/pytorch3d/io/mtl_io.py", line 474, in _load_texture_images
    _read_image(path, path_manager=path_manager, format="RGB") / 255.0
  File "/opt/homebrew/lib/python3.10/site-packages/pytorch3d/io/utils.py", line 73, in _read_image
    with path_manager.open(file_name, "rb") as f:
  File "/opt/homebrew/lib/python3.10/site-packages/iopath/common/file_io.py", line 1062, in open
    bret = handler._open(path, mode, buffering=buffering, **kwargs)  # type: ignore
  File "/opt/homebrew/lib/python3.10/site-packages/iopath/common/file_io.py", line 645, in _open
    return open(  # type: ignore
IsADirectoryError: [Errno 21] Is a directory: 'ShapeNetCore.v2/03001627/f3c0ab68f3dab6071b17743c18fb63dc'

Interestingly, this path is supposed to be a directory and does not throw an error for most of the other ShapeNet models (but only for 3 of them). I share the ShapeNet model below, as well.

f3c0ab68f3dab6071b17743c18fb63dc.zip