参考:https://blog.csdn.net/qq_19865329/article/details/115945454
参考:https://blog.csdn.net/hjxu2016/article/details/118391341
一、下载对应版本的onnx
在github上查看 pytorch1.8.1对应的版本是onnx runtime v1.8.1
我的pytorch是1.7.1 所以我下载的是onnx runtime v1.7.0 cpu 版本
选择cpu或者gpu : https://onnxruntime.ai/
下载nupkg包 链接:https://www.nuget.org/packages/Microsoft.ML.OnnxRuntime
下载得到microsoft.ml.onnxruntime.1.7.0.nupkg文件
通过使用vs2019 运行这个文件,可以的得到onnx的头文件和库文件
具体步骤如下:
先建一个文件夹,文件名可以随意,然后将nupkg文件拷贝进去
通过vs2019解析nupkg包
新建一个控制台应用程序,项目名称可以随意
选择 工具->NuGet管理包->程序包管理控制台
输入以下命令
Install-Package Microsoft.ML.OnnxRuntime -Source E:\onnx1.7.0
我们看到在项目文件夹下生成Microsoft.ML.OnnxRuntime.1.7.0文件
接下来配置环境
为了防止路径过于繁琐,我将Microsoft.ML.OnnxRuntime.1.7.0直接拷贝到E盘下面
原来的文件就可以删除了。
VC++目录 ——包含目录
将包含头文件的文件夹路径加入
VC++目录——库目录
静态库目录路径
链接器——输入——附加依赖项
加入动态链接库
路径:E:\Microsoft.ML.OnnxRuntime.1.7.0\runtimes\win-x64\native\下面
还需要将onnxruntime.dll放入debug或者Release(我配置的是release模式,debug也是一样)
在网上找的测试程序
#include <iostream>
#include <assert.h>
#include <vector>
#include <onnxruntime_cxx_api.h>
#include <string>
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);
const wchar_t* model_path = L"regnetx_800mf.onnx";
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;
system("pause");
return 0;
}
配置完成。