Skip to content

Error when trying to use sample code on CPU #36

@M3nxudo

Description

@M3nxudo

When trying to use the example code snippet in the Readme, i get an error, but it only happens when i force the device to cpu, with cuda acceleration i don't get the same error.

import cv2
import torch

from depth_anything_v2.dpt import DepthAnythingV2

model_configs = {
    'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]},
    'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]},
    'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
    'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}
}

encoder = 'vitl'  # or 'vits', 'vitb', 'vitg'

model = DepthAnythingV2(**model_configs[encoder])
model.load_state_dict(torch.load(f'models/depth_anything_v2_{encoder}.pth', map_location='cpu'))
model.eval()

raw_img = cv2.imread('../folder/image.png')
depth = model.infer_image(raw_img)  # HxW raw depth map in numpy
cv2.imshow("Depth map",depth)
cv2.waitKey(0)

Only changed the directory for the models and for the image from the sample code, and i get the following error, seems to be a mismatch from trying to use cuda datatypes on cpu?

xFormers not available
xFormers not available
Traceback (most recent call last):
  File "D:\source\repos\Depth_Calibration\Depth-Anything-V2\minimal.py", line 20, in <module>
    depth = model.infer_image(raw_img)  # HxW raw depth map in numpy
  File "C:\Users\Daniel\anaconda3\envs\GPU\lib\site-packages\torch\utils\_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "D:\source\repos\Depth_Calibration\Depth-Anything-V2\depth_anything_v2\dpt.py", line 190, in infer_image
    depth = self.forward(image)
  File "D:\source\repos\Depth_Calibration\Depth-Anything-V2\depth_anything_v2\dpt.py", line 179, in forward
    features = self.pretrained.get_intermediate_layers(x, self.intermediate_layer_idx[self.encoder], return_class_token=True)
  File "D:\source\repos\Depth_Calibration\Depth-Anything-V2\depth_anything_v2\dinov2.py", line 308, in get_intermediate_layers
    outputs = self._get_intermediate_layers_not_chunked(x, n)
  File "D:\source\repos\Depth_Calibration\Depth-Anything-V2\depth_anything_v2\dinov2.py", line 272, in _get_intermediate_layers_not_chunked
    x = self.prepare_tokens_with_masks(x)
  File "D:\source\repos\Depth_Calibration\Depth-Anything-V2\depth_anything_v2\dinov2.py", line 214, in prepare_tokens_with_masks
    x = self.patch_embed(x)
  File "C:\Users\Daniel\anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "C:\Users\Daniel\anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "D:\source\repos\Depth_Calibration\Depth-Anything-V2\depth_anything_v2\dinov2_layers\patch_embed.py", line 76, in forward
    x = self.proj(x)  # B C H W
  File "C:\Users\Daniel\anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "C:\Users\Daniel\anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "C:\Users\Daniel\anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\conv.py", line 460, in forward
    return self._conv_forward(input, self.weight, self.bias)
  File "C:\Users\Daniel\anaconda3\envs\GPU\lib\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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions