Couldn‘t load custom C++ ops. This can happen if your PyTorch an

举报
振华OPPO 发表于 2022/06/29 16:27:03 2022/06/29
【摘要】 问题背景在运行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...

问题背景

在运行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,没有任何问题,检测结果已成功保存。

在这里插入图片描述

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。