RuntimeError: Couldn‘t load custom C++ ops.This can happen if your PyTorch and torchvision versions

文章目录

      • 出现问题
      • 问题分析
      • 问题解决

出现问题

 File "/home/anaconda3/envs/tris/lib/python3.8/site-packages/torchvision/ops/boxes.py", line 40, in nms
    _assert_has_ops()
  File "/home/anaconda3/envs/tris/lib/python3.8/site-packages/torchvision/extension.py", line 48, in _assert_has_ops
    raise RuntimeError(
RuntimeError: Couldn't load custom C++ ops. This can happen if your PyTorch and torchvision versions are incompatible, or if you had errors while compiling torchvision from source. For further information on the compatible versions, check https://github.com/pytorch/vision#installation for the compatibility matrix. Please check your PyTorch version with torch.__version__ and your torchvision version with torchvision.__version__ and verify if they are compatible, and if not please reinstall torchvision so that it matches your PyTorch install.

问题分析


UserWarning: Failed to load image Python extension: libtorch_cuda_cu.so: cannot open shared object file: No such file or directory:
这表明 torchvision 的图像处理扩展(依赖 PyTorch 的 CUDA 支持)未能加载。

libtorch_cuda_cu.so 是 PyTorch 的 CUDA 相关动态库,缺失可能是版本不匹配或安装问题。
RuntimeError: Couldn't load custom C++ ops:
torchvision 的某些操作(如 nms,非极大值抑制)依赖自定义 C++ 实现。

错误明确指出 PyTorch 和 torchvision 版本不兼容,或者编译时出错。
你的环境:
text
Wrap
Copy
torch                    1.8.0+cu111
torchvision              0.14.1
PyTorch 版本是 1.8.0(带 CUDA 11.1 支持)。
torchvision 版本是 0.14.1。

兼容性问题
根据 torchvision 的官方文档(https://github.com/pytorch/vision#installation),PyTorch 和 torchvision 的版本需要严格匹配:
PyTorch 1.8.0 兼容的 torchvision 版本是 0.9.0。
torchvision 0.14.1 对应 PyTorch 1.13.1。

你的组合(torch 1.8.0 + torchvision 0.14.1)不兼容,导致 torchvision 无法找到匹配的 C++ 扩展库(libtorch_cuda_cu.so 是 PyTorch 1.8.0 的库,而 torchvision 0.14.1 期望更高版本的 PyTorch)。

我的cuda版本为

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Tue_Feb_27_16:19:38_PST_2024
Cuda compilation tools, release 12.4, V12.4.99
Build cuda_12.4.r12.4/compiler.33961263_0

我的torch版本为

ema-pytorch              0.2.3
torch                    1.8.0+cu111
torchaudio               0.8.0
torchdata                0.7.1
torchvision              0.14.1

问题解决

1.卸载现有 PyTorch 和 torchvision

pip uninstall torch torchvision torchaudio

2.安装支持 CUDA 12.x 的版本
PyTorch 2.2.0 支持 CUDA 12.1,适用于 CUDA 12.4(向下兼容)。安装命令:

pip install torch==2.2.0 torchvision==0.17.0 -f https://download.pytorch.org/whl/cu121/torch_stable.html

3:验证安装

import torch
import torchvision
print(torch.__version__)       # 应为 2.2.0+cu121
print(torchvision.__version__) # 应为 0.17.0+cu121
print(torch.cuda.is_available()) # 应为 True
print(torch.version.cuda)      # 应为 12.1

你可能感兴趣的:(常见运行错误,服务器使用,pytorch,开发语言,torch版本错误)