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

问题背景

在运行yolov5的detect.py文件时,报了下面的错误,之前也遇到过且解决过,所以这次记录下解决方案。

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.


问题原因

显然,根据报错信息得知:torch版本和torchvision版本不匹配。我们需要重装torch或者torchvision使得它们版本匹配。

点此查看torch和torchvision正确对应版本,看完之后发现自己版本也是对应的啊,为什么会报错呢?这里就是最细节的地方,因为比如我的torch是torch1.11.0+cu111,那么torchvision也得是torchvision0.10.0+cu111,而不是0.10.0。默认安装时很可能没有选择相同版本的安装包导致出错。

解决方案

1、进入https://download.pytorch.org/whl/cu113,分别进入torch和torchvision分支选择版本对应且后缀相同的安装包,然后下载到本地。因为我的ubuntu是aarch64架构的,所以后缀就是下面这样的,你们下载时候先uname -a查看下ubuntu架构。


2、然后卸载已之前的torch和torchvision,pip uninstall torchpip uninstall torchvision


3、whl格式本质上是一个压缩包,里面包含了py文件,以及经过编译的pyd文件。我们切换到安装包的下载目录,然后先pip install torch-1.9.0-cp38-cp38-manylinux2014_aarch64.whl,后 pip install torchvision-0.10.0-cp38-cp38-manylinux2014_aarch64.whl

注意,一定要先安装torch,再安装torchvision!如果顺序反过来,它会自动下载torch进行安装,然后版本又不对了,还得卸载了重新安装,我帮你们又踩了下坑。


4、我们再次运行yolov5,没有任何问题,检测结果已成功保存。

来源:振华OPPO

物联沃分享整理
物联沃-IOTWORD物联网 » Couldn‘t load custom C++ ops. This can happen if your PyTorch and torchvision versions are incompati

发表评论