当前位置:   article > 正文

onnxruntime配置_onnxruntime安装

onnxruntime安装

目录

1. onnxruntime 安装

2. onnxruntime-gpu 安装

2.1 举例:创建onnxruntime-gpu==1.14.1的conda环境


runtime指的是程序运行环境,是计算机系统中负责程序运行的组件。在编写程序时,需要考虑程序与运行环境之间的交互,以及程序在运行时所需的资源和环境。

在不同的编程语言中,runtime的实现方式也会有所不同。例如在Java中,runtime由Java虚拟机(JVM)实现,而在C++中,runtime可能由操作系统提供的动态链接库(DLL)实现。

通常情况下,程序员不需要直接操作runtime。但是,在调试程序或优化程序性能时,需要对runtime有一定的了解和掌握。此外,一些高级编程技术,如反射和动态加载,也需要使用runtime的相关功能。

1. onnxruntime 安装

onnx 模型在 CPU 上进行推理,在conda环境中直接使用pip安装即可

pip install onnxruntime

2. onnxruntime-gpu 安装

安装 onnxruntime-gpu 注意事项:

onnxruntime-gpu包含onnxruntime的大部分功能。如果已安装onnruntime要把onnruntime卸载掉。
安装时一定要注意与CUDA、cuDNN版本适配问题,具体适配列表参考:CUDA Execution Provider
 

  1. >>> import onnxruntime
  2. >>> onnxruntime.get_device()
  3. 'GPU' #表示GPU可用
  4. >>> onnxruntime.get_available_providers()
  5. ['TensorrtExecutionProvider', 'CUDAExecutionProvider', 'CPUExecutionProvider']

在 conda 环境中安装,不依赖于 本地主机 上已安装的 cuda 和 cudnn 版本,灵活方便。

  • python3.6, cudatoolkit10.2.89, cudnn7.6.5, onnxruntime-gpu1.4.0
  • python3.8, cudatoolkit11.3.1, cudnn8.2.1, onnxruntime-gpu1.14.1

如果需要其他的版本, 可以根据 onnxruntime-gpu, cuda, cudnn 三者对应关系自行组合测试。

2.1 举例:创建onnxruntime-gpu==1.14.1的conda环境

  1. ## 创建conda环境
  2. conda create -n torch python=3.8
  3. ## 激活conda环境
  4. source activate torch
  5. conda install pytorch==1.10.0 torchvision==0.11.0 torchaudio==0.10.0 cudatoolkit=11.3 -c pytorch -c conda-forge
  6. conda install cudnn==8.2.1
  7. pip install onnxruntime-gpu==1.14.1
  8. ## pip install ... (根据需求,安装其他的包)

3.推理过程示例如下:

  1. import onnxruntime
  2. import numpy as np
  3. device_name = 'cuda:0' # or 'cpu'
  4. print(onnxruntime.get_available)
  5. if device_name == 'cpu':
  6. providers = ['CPUExecutionProvider']
  7. elif device_name == 'cuda:0':
  8. providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
  9. # Create inference session
  10. onnx_model = onnxruntime.InferenceSession('slowfast.onnx', providers=providers)
  11. # Create the input(这里的输入对应slowfast的输入)
  12. data = np.random.rand(1, 1, 3, 32, 256, 256).astype(np.float32)
  13. # Inference
  14. onnx_input = {onnx_model.get_inputs()[0].name: data}
  15. outputs = onnx_model.run(None, onnx_input)

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号