Depth-Anything icon indicating copy to clipboard operation
Depth-Anything copied to clipboard

RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

Open Arbiter123123123 opened this issue 10 months ago • 2 comments

HI! I followed the <README--No network connection, cannot load these models?> When I try to run the run.py,I still got this error:

Traceback (most recent call last):
  File "/root/autodl-tmp/Depth-Anything-main/run.py", line 84, in <module>
    depth = depth_anything(image)
  File "/root/miniconda3/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/root/miniconda3/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/root/autodl-tmp/Depth-Anything-main/depth_anything/dpt.py", line 158, in forward
    features = self.pretrained.get_intermediate_layers(x, 4, return_class_token=True)
  File "/root/autodl-tmp/Depth-Anything-main/torchhub/facebookresearch_dinov2_main/vision_transformer.py", line 308, in get_intermediate_layers
    outputs = self._get_intermediate_layers_not_chunked(x, n)
  File "/root/autodl-tmp/Depth-Anything-main/torchhub/facebookresearch_dinov2_main/vision_transformer.py", line 272, in _get_intermediate_layers_not_chunked
    x = self.prepare_tokens_with_masks(x)
  File "/root/autodl-tmp/Depth-Anything-main/torchhub/facebookresearch_dinov2_main/vision_transformer.py", line 214, in prepare_tokens_with_masks
    x = self.patch_embed(x)
  File "/root/miniconda3/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/root/miniconda3/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/root/autodl-tmp/Depth-Anything-main/torchhub/facebookresearch_dinov2_main/dinov2/layers/patch_embed.py", line 76, in forward
    x = self.proj(x)  # B C H W
  File "/root/miniconda3/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/root/miniconda3/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/root/miniconda3/lib/python3.10/site-packages/torch/nn/modules/conv.py", line 460, in forward
    return self._conv_forward(input, self.weight, self.bias)
  File "/root/miniconda3/lib/python3.10/site-packages/torch/nn/modules/conv.py", line 456, in _conv_forward
    return F.conv2d(input, weight, bias, self.stride,
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

Arbiter123123123 avatar Apr 21 '24 15:04 Arbiter123123123

This sort of error suggests that the model & image are being processed by different devices (e.g. cpu vs gpu). In this case, it looks like the input image is on the gpu while the model is on the cpu.

It should be a simple fix. However you're loading the model, you'll just need to add .to(DEVICE) onto the end of it before processing the input image. You can see an example of this from the original run.py script (near the end of line 34).

heyoeyo avatar Apr 21 '24 18:04 heyoeyo

I found a solution on some forums:

depth_anything.load_state_dict(torch.load(f'./checkpoints/depth_anything_{encoder}14.pth'))

Add the following line of code to the end of the previous line:

depth_anything.cuda()

is

    depth_anything.load_state_dict(torch.load(f'./checkpoints/depth_anything_{encoder}14.pth'))
    depth_anything.cuda()

Arbiter123123123 avatar Apr 22 '24 04:04 Arbiter123123123

Thanks for the valuable discussion. Since the issue has been resolved, I will close it for now,

LiheYoung avatar May 27 '24 09:05 LiheYoung