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

本文讲述了在运行yolov5检测脚本时遇到的torch与torchvision版本不兼容错误,作者通过详细步骤教导如何从官网下载对应版本的whl包,重装并确保版本一致性,最终成功解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题背景

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

在这里插入图片描述

一时劝人以口,百世劝人以书。——《了凡四训·积善之方》

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

振华OPPO

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值