如何用visual studio 2019配置OnnxRuntime

发布时间:2026/7/24 21:13:17
如何用visual studio 2019配置OnnxRuntime 如何用visual studio 2019配置OnnxRuntime_onnxruntime.dll-CSDN博客Onnxruntime在Visual Studio的配置_onnxruntime下载vs-CSDN博客https://zhuanlan.zhihu.com/p/662592782一、下载onnx库文件这里有两种方案1、直接下载include和lib文件https://github.com/Microsoft/onnxruntime/releases/tag/v1.19.22、下载nupkg包https://www.onnxruntime.ai/选择适应自己的版本博主选用的是cpu版本如下链接https://www.nuget.org/packages/Microsoft.ML.OnnxRuntime得到一个microsoft.ml.onnxruntime.1.19.2.nupkg文件这个包nupkg是visual studio 的NuGet Package的一个包文件 。这个包里存放着onnx的头文件和库文件。把这个包拷贝到E:\git\cache路径二、用visual studio 2019 解析nupkg包1、首先用vs2019新建立一个项目选择 工具-NuGet管理包-程序包管理控制台然后输入Install-Package Microsoft.ML.OnnxRuntime -Source E:\git\cache这里E:\git\cache里面放着microsoft.ml.onnxruntime.1.19.2.nupkg文件文件注意这里一定要建立个工程才能执行以上的文件否则会报Install-Package : 找不到项目“Default”。的错误。完事后在一下红框中的路径下有个\packages\Microsoft.ML.OnnxRuntime.1.19.2有几个文件build下的include文件夹就是onnxruntime的接口头文件runtime文件夹下有着各个系统下的算法库文件。这里本文选择win-x64\native文件夹这里放着OnnxRunTime的动态库和静态库。这里ONNX的nupkg文件已经解析完毕。三、用vs2019中的cmake来配置ONNXRunTime习惯了linux系统的我还是更喜欢用cmake来配置工程用vs2019建立一个cmake项目CMakeLists.txt 里添加 ort 的 头文件和算法库的路径等cmake_minimum_required (VERSION 3.8) project (CMakeProject1) include_directories(E:/code/Microsoft.ML.OnnxRuntime.1.8.0/build/native/include) include_directories(E:/opencv4.5.2/opencv/build/include) link_directories(E:/code/Microsoft.ML.OnnxRuntime.1.8.0/runtimes/win-x64/native) link_directories(E:/opencv4.5.2/opencv/build/x64/vc15/lib) #link_libraries(onnxruntime) ADD_EXECUTABLE(hello CMakeProject1.cpp) target_link_libraries(hello onnxruntime opencv_world452)main.cpp文件夹#include iostream #include assert.h #include vector #include onnxruntime_cxx_api.h #include string #include opencv2/opencv.hpp int main(int argc, char* argv[]) { Ort::Env env(ORT_LOGGING_LEVEL_WARNING, test); Ort::SessionOptions session_options; session_options.SetIntraOpNumThreads(1); session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_BASIC); #ifdef _WIN32 const wchar_t* model_path LE:/code/CMakeProject1/CMakeProject1/models/SENet_224.onnx; #else const char* model_path E:/code/CMakeProject1/CMakeProject1/models/SENet_224.onnx; #endif Ort::Session session(env, model_path, session_options); // print model input layer (node names, types, shape etc.) Ort::AllocatorWithDefaultOptions allocator; size_t num_input_nodes session.GetInputCount(); std::cout session.GetInputName(0, allocator) std::endl; std::cout session.GetOutputName(0, allocator) std::endl; return 0; }运行调试时可能会报找不到 onnxruntime.dll 的报错这个主要是由于windows找不到库所在的路径导致的。解决方案1将 onnxruntime.dll文件拷贝到可执行当前目录解决方案2将 onnxruntime.dll 所在的文件夹添加到系统环境变量。或者 配置CUDA和OnnxruntimeVisual Studio配置CUDA的步骤: 参考上一节Opencv的配置方式将C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\include添加至头文件目录将C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\lib\x64添加至库文件目录。将D:\software\onnxruntime-win-x64-gpu-1.16.0\include添加至头文件目录将D:\software\onnxruntime-win-x64-gpu-1.16.0\lib添加至库文件目录将onnxruntime.lib添加至依赖项。在运行VS项目以后会生成Release或Debug文件夹需要将D:\software\onnxruntime-win-x64-gpu-1.16.0\lib目录下的dll文件拷贝至Release或Debug文件夹中否则程序会报错。