如何用visual studio 2019配置OnnxRuntime


一直以来都是用的linux系统,突然面对windows有点不知所措,这里记录一下,如何用vs2019配置OnnxRunTime

一、下载onnx库文件

这里有两种方案
1、直接下载include和lib文件
https://github.com/Microsoft/onnxruntime/releases/tag/v1.8.1
2、下载nupkg包
https://www.onnxruntime.ai/
选择适应自己的版本,博主选用的是cpu版本,如下链接
https://www.nuget.org/packages/Microsoft.ML.OnnxRuntime
在这里插入图片描述
得到一个microsoft.ml.onnxruntime.1.8.0.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.8.0.nupkg文件文件

注意,这里一定要建立个工程,才能执行以上的文件,否则会报

Install-Package : 找不到项目“Default”。

的错误。

完事后,在一下红框中的路径下,
在这里插入图片描述
有个\packages\Microsoft.ML.OnnxRuntime.1.8.0有几个文件
在这里插入图片描述

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 = L"E:/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 所在的文件夹添加到系统环境变量。

在这里插入图片描述

  • 11
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值