1.安裝onnxruntime-gpu
新版的onnxruntime-gpu 即支持gpu的推理,也支持cpu的推理。
卸載舊的1.7.1 cpu版本,安裝新的gpu版本:
pip uninstall onnxruntime
pip install onnxruntime-gpu
檢查是否安裝成功:
>>> import onnxruntime
>>> onnxruntime.__version__
'1.10.0'
>>> onnxruntime.get_device()
'GPU'
>>> onnxruntime.get_available_providers()
['TensorrtExecutionProvider', 'CUDAExecutionProvider', 'CPUExecutionProvider']
2.修改推理代碼
在推理代碼上增加 providers參數(shù),選擇推理的框架??醋约褐С帜膫€就選擇自己支持的就可以了。
session = onnxruntime.InferenceSession('yolov5s.onnx', None)
# 改為:
session = onnxruntime.InferenceSession('yolov5s.onnx',
providers=['TensorrtExecutionProvider', 'CUDAExecutionProvider', 'CPUExecutionProvider'])
如果運行推理代碼出現(xiàn) Tensorrt, CUDA都無法推理,如下所示,則是自己的?ONNX Runtime,?TensorRT,?CUDA 版本沒對應(yīng)正確 。
2022-08-09 15:38:31.386436528 [W:onnxruntime:Default, onnxruntime_pybind_state.cc:509 CreateExecutionProviderInstance] Failed to create TensorrtExecutionProvider. Please reference https://onnxruntime.ai/docs/execution-providers/TensorRT-ExecutionProvider.html#requirements to ensure all dependencies are met.
對應(yīng)版本如下 :文章來源:http://www.zghlxwxcb.cn/news/detail-557234.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-557234.html
到了這里,關(guān)于使用onnxruntime-gpu 模型推理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!