问题解决之 RuntimeError: Couldn‘t load custom C++ ops. This can happen if your PyTorch XXX

问题解决之 RuntimeError: Couldn‘t load custom C++ ops. This can happen if your PyTorch XXX,第1张

一、问题描述

深度学习环境 GPU 版 pytorch 下,运行代码出现报错,关键报错信息如下:

RuntimeError: Couldn't load custom C++ ops. This can happen if your PyTorch and torchvision versions are incompatible, 

大致的意思是说当前环境的 PyTorch 和 torchvision 版本不匹配,建议重新安装 PyTorch 和 torchvision。

具体报错信息如下:

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 tor
chvision 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.

二、解决办法

重新安装 pytorch 环境,保证 PyTorch 和 torchvision 版本匹配。
torchvision 的版本要大于 0.3 才能使用 ops 。
建议到 pytorch 官网 找到版本匹配的 Pytorch 和 torchvision 进行安装。


如果不想安装界面上显示的 pytorch 版本,可以点击 Previous versions of PyTorch 安装之前的 pytorch 版本。
当然,也可以直接进入 https://pytorch.org/get-started/previous-versions/查看先前的版本。

pytorch 安装包括 conda 安装和 pip 安装两种安装方式。其中,conda 安装是基于 Anaconda 软件或者 Miniconda 软件安装。pip 安装是基于 python 解释器的安装。pytorch 官网给出的命令支持上述两种安装方式。

本文主要介绍GPU 版本 pytorch 的安装方法。

1. 确保完全卸载 pytorch

无论是之前 pytorch 安装失败还是想要安装其他版本的 pytorch 环境,都需要确保完全卸载掉之前的 pytorch 环境。
否则会报类似 Couldn‘t load custom C++ ops 的错误,具体可以参见博客解决 “Couldn‘t load custom C++ ops. This can happen if your PyTorch and “ 。

如果多次安装失败或者同时使用过 conda 和 pip 要格外注意完全卸载 pytorch 环境。
卸载的具体步骤如下:

  • 清除 conda 下的 pytorch
conda uninstall torch
conda uninstall torchvision
conda uninstall cudatoolkit
  • 清除 pip 下的 pytorch
pip uninstall torch
pip uninstall torchvision
pip uninstall torchaudio
2. 安装 pytorch 环境

这里推荐大家使用 pip 进行安装。尤其是安装一些较低版本的 pytorch 环境。

  1. 首先,打开 pytorch 官网,确定要安装的 pytorch 版本。
    注意 :pytorch 版本要和显卡 cuda 相匹配
    在 cmd 中输入 nvcc --version 可以查看 cuda 版本。当前显卡 cuda 是 10.0

  2. 复制官网上的安装命令

# CUDA 10.0
pip install torch==1.2.0 torchvision==0.4.0

然后就安装成功了。

注意

如果按照pytorch 官网的命令安装后,依然报相同的错误,那么可能是代码使用的 Pytorch 和 torchvision 版本和现有的 Pytorch 和 torchvision 不兼容。可以考虑依照代码要求的版本安装 Pytorch 和 torchvision 。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/873883.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存