torch导出ONNX模型报错:OnnxExporterError: Module onnx is not installed
问题:
使用torch 导出模型为onnx文件时报错:torch.onnx.OnnxExporterError: Module onnx is not installed!
环境:
操作系统 Win10
python运行环境 Anacoda3
torch 2.6.0
torchvision 0.21.0
分析:
开始以为缺少onnx模块,使用pip install onnx 命令安装onnx模块后依然报错。再看错误日志,发现有错误:
ImportError: DLL load failed while importing onnx_cpp2py_export: 动态链接库(DLL)初始化例程失败。
通过查看网上各个网友的解决办法,发现可能是onnx版本的问题。
默认安装的onnx模块为最新的1.17.0版本,使用网友推荐的1.16.1版本,测试通过。
解决:
卸载onnx模块,重新安装1.16.1版本的。
pip uninstall onnx
pip install onnx==1.16.1