//Description: 记录在Windows上使用onnxruntime/openvino进行模型推理部署过程中的笔记,写于2025年3月
//Create Date: 2025-02-20 14:20:28
//Author: channy
[toc]
Linux上训练了一个视频编解码的.pt模型,需要在其它平台(Windows)使用解码推理。
先使用’torch.onnx.export’导出成.onnx模型,注意查看其中的opset_version默认值,后面可能要用到
mo --input_model model.onnx --output_dir ir_output
需要把onnxruntime的dll放置到exe可执行文件同一目录下。它不像OpenCV没有dll会直接报“找不到OpenCVxxx.dll”,而是直接崩溃。。。
The given version [20] is not supported, only version 1 to 10 is supported in this build
一开始用的是onnxruntime 1.20.1版本在Linux上正常在Windows上vs2022运行直接崩溃。后面在别的电脑上vs2019运行同样崩溃,报的该错误。推测是版本相关问题。
查看官网每一版的Release说明中有写哪哪版开始不再支持VC++ Runtime xxx版本以下。
vs | c++ runtime |
---|---|
vs2022 | 14.30 |
vs2019 | 14.20 |
vs2017 | 14.10 |
vs2015 | 14.00 |
vs2013 | 12.00 |
vs2012 | 11.00 |
vs2010 | 10.00 |
换成onnxruntime 1.10.0后正常,确认是版本问题
try {
// ...
// 加载模型
m_pSession = new Ort::Session(m_env, pModelPath, m_sessionOptions);
// ...
} catch (std::exception&e) {
printf("init Get Exception: [%s]\n", e.what());
}
其中模型加载中’Ort::Session’参数二的类型是const ORTCHAR_T*
,在Linux下是#define ORTCHAR_T char
没问题,在Windows下是#define ORTCHAR_T wchar_t
直接强行转换
m_pSession = new Ort::Session(m_env, (const ORTCHAR_T*), m_sessionOptions);
在Windows下大概率是乱码,通过exception的打印可以看到报找不到模型的错误。
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
std::wstring wsModelPath = converter.from_bytes(pModelPath);
m_pSession = new Ort::Session(m_env, wsModelPath.c_str(), m_sessionOptions);
可以用stl库函数直接转换。
其中onnxruntime 1.10最高只支持opset=15的.onnx模型,使用python的onnxruntime 1.16.0转换默认是opset=17太高了不支持
$ ls /usr/local
bin cuda-12 cuda-12.5 games lib sbin src
cuda cuda-12.1 etc include man share
$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Wed_Apr_17_19:19:55_PDT_2024
Cuda compilation tools, release 12.5, V12.5.40
Build cuda_12.5.r12.5/compiler.34177558_0
$ nvidia-smi
Thu Feb 20 14:10:24 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 555.42.02 Driver Version: 555.42.02 CUDA Version: 12.5 |
|-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA GeForce RTX 3090 Off | 00000000:55:00.0 On | N/A |
| 53% 68C P0 201W / 350W | 16523MiB / 24576MiB | 11% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+
+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| 0 N/A N/A 1957 G /usr/lib/xorg/Xorg 35MiB |
| 0 N/A N/A 2799 G /usr/lib/xorg/Xorg 163MiB |
| 0 N/A N/A 2937 G /usr/bin/gnome-shell 54MiB |
| 0 N/A N/A 3498 G ...sion,SpareRendererForSitePerProcess 81MiB |
| 0 N/A N/A 63928 C python3 16138MiB |
+-----------------------------------------------------------------------------------------+
CUDA-Toolkit
多个版本安装过程中去掉Driver
显卡驱动安装
添加到update-alternatives
sudo update-alternatives --install /usr/local/cuda cuda /usr/local/cuda-12.1 121
sudo update-alternatives --install /usr/local/cuda cuda /usr/local/cuda-12.5 125
切换
$ sudo update-alternatives --config cuda
update-alternatives --config cuda
There are 2 choices for the alternative cuda (providing /usr/local/cuda).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/local/cuda-12.5 125 auto mode
1 /usr/local/cuda-12 10 manual mode
2 /usr/local/cuda-12.5 125 manual mode
Press <enter> to keep the current choice[*], or type selection number:
切换后验证nvcc -V
sudo apt install ./sdkmanager_2.1.0-11698_amd64.deb
sudo apt get update
Put the device into reset/recovery mode.
Power on the carrier board and hold down the Recovery button.
Press the Reset button.
台式机通过命令lsusb
确认是否进入Recovery模式
$ lsusb
Bus <bbb> Device <ddd>: ID 0955: <nnnn> Nvidia Corp.
$ sdkmanager
根据提示下载安装
PyTorch需要安装特定版本 PyTorch [torch][https://developer.download.nvidia.cn/compute/redist/jp/v61/pytorch/] forum