pyinstaller打包onnxruntime-gpu报错找不到CUDA的解决方案

问题说明:

使用onnxruntime-gpu完成了深度学习模型部署,但在打包时发生了报错:找不到CUDA

具体问题描述:

RuntimeError: D:\a\_work\1\s\onnxruntime\python\onnxruntime_pybind_state.cc:857 onnxruntime::python::CreateExecutionProviderInstance CUDA_PATH is set but CUDA wasnt able to be loaded. Please install the correct version of CUDA andcuDNN as mentioned in the GPU requirements page  (https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html#requirements),  make sure they're in the PATH, and that your GPU is supported

前提:

1、确保你的 GPU 受 CUDA 支持,并且驱动程序已正确安装

2、电脑上已经安装与onnxruntime-gpu对应版本的cuda和cudnn,并加入了环境变量

3、打包之前可以正常运行

解决方案

onnxruntime-gpu 依赖于一些动态链接库,这些库可能不会被 pyinstaller 自动检测到。你可以创建一个自定义的 hook-onnxruntime.py 文件来帮助 pyinstaller 正确识别 onnxruntime-gpu 的依赖项。在项目目录下,创建一个hook-onnxruntime.py文件,并加入以下代码:

from PyInstaller.utils.hooks import collect_dynamic_libs

hiddenimports = ['onnxruntime', 'onnxruntime.capi', 'onnxruntime.capi.onnxruntime_pybind11_state']

binaries = collect_dynamic_libs('onnxruntime')

然后在pyinstaller命令中,使用:

pyinstaller --onefile --additional-hooks-dir=. -w main.py

--onefile:将所有的文件和依赖打包成一个单独的可执行文件

--additional-hooks-dir=.:指定一个额外的目录,pyinstaller 会在这个目录中查找自定义的钩子(hooks)文件。钩子文件用于在打包过程中处理一些特殊的依赖或行为。. 表示当前目录,即在当前目录中查找钩子文件

-w main.py:指定需要打包的py文件,不带控制台窗口打包

pyinstaller常用命令参数

基本参数
  • -F or --onefile:将所有内容打包成一个单独的可执行文件
  • -D or --onedir:将所有内容打包成一个目录
  • -w or --windowed or --noconsole:对于 Windows 和 Mac OS X,不带控制台窗口打包
添加数据文件
  • --add-data:添加额外的数据文件到打包的应用程序中;在 Windows 上使用 ;分隔,在 Unix 上使用 : 分隔,例如:pyinstaller --add-data "data/file.txt;data"
  • --add-binary:添加二进制文件到打包的应用程序中,例:pyinstaller --add-binary "/path/to/binary;bin"
  • --icon:设置可执行文件的图标
  • --version:添加版本信息文件
  • --hidden-import:指定在脚本中没有直接导入的模块
  • --exclude-module:排除指定的模块
其他参数
  • -n or --name:指定生成的可执行文件的名称
  • --clean:在构建之前清理 PyInstaller 缓存和临时文件
  • --key:使用指定的密钥对 Python 字节码进行加密
  • 25
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值