VAST-AI-Research / TripoSR

MIT License
4.6k stars 532 forks source link

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

Open siddhant230 opened 4 months ago

siddhant230 commented 4 months ago

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 () ----> 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 """ `