Couldn’t load custom C++ ops.
1.问题描述
Couldn‘t load custom C++ ops. This can happen if your PyTorch and torchvision versions are incompatible.
第一次遇到这个问题,说是torch和torchvision版本不对应。但是这个环境我已经使用好久了,一直没有出现问题(用conda安装的)。但是既然问题出现了,就得想办法解决!
2.分析问题
2.1 查看torch和torchvision版本
可以使用以下代码在Pycharm中查看
import torch
print(torch.__version__)
import torchvision
print(torchvision.__version__)
print(torch.version.cuda)
输出:
1.12.1
0.13.1
11.3
看了一些torch和torchvision的版本独对应关系,发现没啥问题torch1.12.1应该是对应torchvision0.13.1,貌似没啥问题。
2.2 重新安装torch,torchvision,torchaudio
结合网上一些说法(说法一:把torch,torchvision,torchaudion卸载干净重新安装对应的版本;说法二:推荐使用pip安装),因此,我先将torch,torchvision,torchaudion全部卸载,然后重新用pip安装。
2.2.1卸载torch,torchvision,torchaudion
pip uninstall torch
pip uninstall torchvision
pip uninstall torchaudio
conda uninstall torch
conda uninstall torchvision
conda uninstall torchaudio
这下总下载干净了
2.2.2pip安装
在pytorch官网找到与自己cuda对应的torch,torchvision,torchaudio版本,并且选择用pip安装
我使用的是cuda11.3,pip安装命令如下:
pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu113
安装成功后可以查看下版本
import torch
print(torch.__version__)
import torchvision
print(torchvision.__version__)
print(torch.version.cuda)
输出:
1.12.1+cu113
0.13.1+cu113
11.3
貌似跟刚开始的有点区别,但感觉区别不是很大。
反正最终问题解决了,没有报错Couldn’t load custom C++ ops.
但是又报错显示找不到PIL模块,不过这是个小问题,卸载重装一遍即可。
pip uninstall pillow
pip install pillow
代码正常运行,没有问题了!
3.总结
所以,pip安装就没有问题,conda安装有问题,推荐pip安装。