Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use the exported .obj file with torch3d for rendering #105

Open
siddhant230 opened this issue Jun 30, 2024 · 0 comments
Open

Unable to use the exported .obj file with torch3d for rendering #105

siddhant230 opened this issue Jun 30, 2024 · 0 comments

Comments

@siddhant230
Copy link

siddhant230 commented Jun 30, 2024

First of all thanks for such amazing work, this really is helpful.
I am trying to render the .obj file usng torch3d as per the code below, but it is asking for texture which I am not sure how to get.
While loading itself I get this warning
/usr/local/lib/python3.10/dist-packages/pytorch3d/io/obj_io.py:550: UserWarning: Mtl file does not exist: /content/data/cow_mesh/cow.mtl warnings.warn(f"Mtl file does not exist: {f}"),
hence I donwnloaded a dummy obj, mtl and texture.png file using

!wget -P data/cow_mesh https://dl.fbaipublicfiles.com/pytorch3d/data/cow_mesh/cow.obj

!wget -P data/cow_mesh https://dl.fbaipublicfiles.com/pytorch3d/data/cow_mesh/cow.mtl

!wget -P data/cow_mesh https://dl.fbaipublicfiles.com/pytorch3d/data/cow_mesh/cow_texture.png

torch3d runs flawlessly with this, but if I remove mtl it stops working completely. I am really stuck with this. My objective is to render obj from triposr from multiple angles and save the images. Is there a direct or alternate way to solve this. Also please point if I am doing something wrong. I would be really greatful. Thanks.

CODE
`
import torch
from pytorch3d.io import load_objs_as_meshes, load_obj
from pytorch3d.structures import Meshes

from pytorch3d.renderer import (
look_at_view_transform,
FoVPerspectiveCameras,
RasterizationSettings,
MeshRenderer,
MeshRasterizer,
PointLights,
SoftPhongShader
)

device = torch.device("cuda") if torch.cuda.is_available() else "cpu"

load mesh

obj_filename = "mesh.obj"
mesh = load_objs_as_meshes([obj_filename], device=device)

R, T = look_at_view_transform(2.7, 0, 180)
cameras = FoVPerspectiveCameras(device=device, R=R, T=T)

raster_settings = RasterizationSettings(
image_size=512, blur_radius=0.0, faces_per_pixel=1
)

lights = PointLights(device=device, location=[[0.0, 0.0, -3.0]])

renderer = MeshRenderer(
rasterizer=MeshRasterizer(cameras=cameras,
raster_settings=raster_settings),
shader=SoftPhongShader(device=device,
cameras=cameras,
lights=lights)
)

multi-view

bs = 5
meshes = mesh.extend(bs)

elev = torch.linspace(0, 180, bs)
azim = torch.linspace(-180, 180, bs)

R, T = look_at_view_transform(dist=2.7, elev=elev, azim=azim)
cameras = FoVPerspectiveCameras(device=device, R=R, T=T)

lights.locaion = torch.tensor([[0.0, 0.0, -3.0]], device=device)

render

images = renderer(meshes, cameras=cameras, lights=lights)

ERROR : ValueError: Meshes does not have textures

`

`
"""
ERROR:


ValueError Traceback (most recent call last)
in <cell line: 1>()
----> 1 images = renderer(meshes, cameras=cameras, lights=lights)

6 frames
/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _wrapped_call_impl(self, *args, **kwargs)
1530 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1531 else:
-> 1532 return self._call_impl(*args, **kwargs)
1533
1534 def _call_impl(self, *args, **kwargs):

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
1539 or _global_backward_pre_hooks or _global_backward_hooks
1540 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1541 return forward_call(*args, **kwargs)
1542
1543 try:

/usr/local/lib/python3.10/dist-packages/pytorch3d/renderer/mesh/renderer.py in forward(self, meshes_world, **kwargs)
62
63 fragments = self.rasterizer(meshes_world, **kwargs)
---> 64 images = self.shader(fragments, meshes_world, **kwargs)
65
66 return images

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _wrapped_call_impl(self, *args, **kwargs)
1530 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1531 else:
-> 1532 return self._call_impl(*args, **kwargs)
1533
1534 def _call_impl(self, *args, **kwargs):

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
1539 or _global_backward_pre_hooks or _global_backward_hooks
1540 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1541 return forward_call(*args, **kwargs)
1542
1543 try:

/usr/local/lib/python3.10/dist-packages/pytorch3d/renderer/mesh/shader.py in forward(self, fragments, meshes, **kwargs)
126 def forward(self, fragments: Fragments, meshes: Meshes, **kwargs) -> torch.Tensor:
127 cameras = super()._get_cameras(**kwargs)
--> 128 texels = meshes.sample_textures(fragments)
129 lights = kwargs.get("lights", self.lights)
130 materials = kwargs.get("materials", self.materials)

/usr/local/lib/python3.10/dist-packages/pytorch3d/structures/meshes.py in sample_textures(self, fragments)
1546 )
1547 else:
-> 1548 raise ValueError("Meshes does not have textures")
1549
1550 def submeshes(

ValueError: Meshes does not have textures
"""
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant