facebookresearch / pytorch3d

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

RuntimeError: The size of tensor a (3) must match the size of tensor b (4) at non-singleton dimension 4 #1824

Closed wang1528186571 closed 3 months ago

wang1528186571 commented 3 months ago
Traceback (most recent call last):
  File "/home/wang/Desktop/wjl-project/Adversarial_camou/Visualize-Shirts.py", line 150, in <module>
    target_img =renderer(meshes)
  File "/home/wang/Anaconda3/envs/camou/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/wang/Anaconda3/envs/camou/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/wang/Anaconda3/envs/camou/lib/python3.8/site-packages/pytorch3d/renderer/mesh/renderer.py", line 62, in forward
    images = self.shader(fragments, meshes_world, **kwargs)
  File "/home/wang/Anaconda3/envs/camou/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/wang/Anaconda3/envs/camou/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/wang/Anaconda3/envs/camou/lib/python3.8/site-packages/pytorch3d/renderer/mesh/shader.py", line 130, in forward
    colors = phong_shading(
  File "/home/wang/Anaconda3/envs/camou/lib/python3.8/site-packages/pytorch3d/renderer/mesh/shading.py", line 119, in phong_shading
    colors, _ = _phong_shading_with_pixels(
  File "/home/wang/Anaconda3/envs/camou/lib/python3.8/site-packages/pytorch3d/renderer/mesh/shading.py", line 94, in _phong_shading_with_pixels
    colors = (ambient + diffuse) * texels + specular
RuntimeError: The size of tensor a (3) must match the size of tensor b (4) at non-singleton dimension 4
def create_tiled_texture(texture_path, target_height, target_width):
    print("w:",target_width,"h:",target_height)
    target_height = 4 * target_height
    target_width = 4 * target_width
    # 加载纹理图片
    image = Image.open(texture_path).convert('RGBA')
    print(image.mode)

    # 计算新的尺寸,为原始尺寸的十分之一
    new_width = image.width // 20
    new_height = image.height // 20
    # 调整图像尺寸
    resized_image = image.resize((new_width, new_height), Image.LANCZOS)
    # 将PIL图像转换为Numpy数组
    texture = np.array(resized_image)  # 转换后的数组形状为 [H, W, 3]
    print(texture.shape)

    # 获取纹理的尺寸
    texture_height, texture_width, _ = texture.shape

    # 计算纵向和横向需要重复的次数
    repeat_y = (target_height + texture_height - 1) // texture_height
    repeat_x = (target_width + texture_width - 1) // texture_width

    # 铺满纹理
    tiled_texture = np.tile(texture, (repeat_y, repeat_x, 1))

    # 裁剪多余的部分以匹配目标尺寸
    tiled_texture = tiled_texture[:target_height, :target_width, :]

    # 将Numpy数组转换为Tensor
    tiled_texture = tiled_texture / 255.0
    tiled_texture = torch.from_numpy(tiled_texture).permute(2, 0, 1)  # 转换为 [C, H, W] 以匹配PyTorch的要求

    return tiled_texture.float()  # 转换为float类型的Tensor

I don't know why this error occurs. pytorch3d==0.3.0

bottler commented 3 months ago

More detail about your code would be useful.

But potentially the problem here is the mixture of different numbers of channels. If you are rendering with C channels then everything must have C channels. Could it be that e.g. your lights have RGB components but you have RGBA textures - so mixing 3 and 4? I'm guessing, but you could try something like return tiled_texture.float()[:3] in place of return tiled_texture.float().