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

RuntimeError: Tensors must have same number of dimensions: got 4 and 5 #19

Closed
pbonazzi opened this issue Jul 20, 2022 · 3 comments
Closed

Comments

@pbonazzi
Copy link

pbonazzi commented Jul 20, 2022

I have run into the following error when running scripts/create_mvr_data_from_mesh.py for the first time

`  File "scripts/create_mvr_data_from_mesh.py", line 207, in <module>
    images = renderer.shader(fragments, meshes_batch, lights=lights, cameras=cams)
  File "/home/mengqi/.conda/envs/pytorch3d/lib/python3.8/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/mengqi/.conda/envs/pytorch3d/lib/python3.8/site-packages/pytorch3d/renderer/mesh/shader.py", line 376, in forward
    images = hard_rgb_blend(colors, fragments, blend_params)
  File "/home/mengqi/.conda/envs/pytorch3d/lib/python3.8/site-packages/pytorch3d/renderer/blending.py", line 84, in hard_rgb_blend
    return torch.cat([pixel_colors, alpha], dim=-1)  # (N, H, W, 4)
RuntimeError: Tensors must have same number of dimensions: got 4 and 5`

As you can see the error is raised at line 206 of the script and is caused by this call:
images = renderer.shader(fragments, meshes_batch, lights=lights, cameras=cams)

Can you help me with this ?

Version
Python: 3.8
Pytorch: 1.6
Pytorch3D : 0.6.1

System
System='Linux'
release='4.15.0-188-generic'
version='#199-Ubuntu SMP Wed Jun 15 20:42:56 UTC 2022'
machine='x86_64'

Note
In the README.md file you say to run external/prefix , but I could only find external/prefix_sum.
I have successfully run all the setup.py scripts.

@lxxue
Copy link
Collaborator

lxxue commented Jul 20, 2022

Hi, this is probably a pytorch3d version problem. We were working with an old version, and the new version is incompatible. I update the installation part in the fix/install branch. Could you check it out and create the environment again to see if you still have an issue? Things work fine on my side with this new installation.

@pbonazzi
Copy link
Author

pbonazzi commented Jul 20, 2022

Well done , I have already managed to solve it on my own , by tracing back the problem in pytorch3d library .

It had to do with the renderer.shader function , in your script I have done the following

            # add these imports
            from pytorch3d.renderer.blending import BlendParams, hard_rgb_blend
            from pytorch3d.renderer.mesh.shading import flat_shading

            # replace these lines (206)
            # images = renderer.shader(fragments, meshes_batch, lights=lights, cameras=cams)
            texels = meshes_batch.sample_textures(fragments)
            colors = flat_shading(meshes=meshes_batch, fragments=fragments, lights=lights, cameras=cams, materials=renderer.shader.materials, texels=texels)
            images = hard_rgb_blend(colors[0], fragments, BlendParams())

@lxxue lxxue closed this as completed in b26ad17 Jul 21, 2022
@pbonazzi
Copy link
Author

It works like a charm with #21

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

2 participants