c++动态库之四 onnxruntime

终于进入正题了,首先我们先默认已经装好了onnx并且能正常运行,具体步骤跳到第一篇文章去看。

注意,我输入的是特征而不是图片,即输入是二维的不是三维的,三维的情况下篇文章再说。

下方是总体代码,我数据处理部分太长就不贴出来了,最后输入的特征是vector< float >的格式,具体一点就是8046 ,输出是20183,我的label类别有83,特征804卷出来后是201。python的onnx是可以任意长度的输入,不过c++的好像必须是固定长度,这些数字在之前python输出onnx模型的时候就设定好。

详细看代码内部的解析。

#include <assert.h>
#include<onnxruntime_cxx_api.h>
#include <iostream>
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

string feature2result(vector<float> feature_list)
{
   
	Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "test");
	Ort::SessionOptions session_options;
	session_options.SetIntraOpNumThreads(1);
	session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);
	//const char* model_path = "F://c++//test.onnx";
	const wchar_t* model_path = L"test.onnx";  //改为模型路径,我一般是最后把路径复制到这个文件旁边,免得绝对路径换环境就完蛋


	printf("Using Onnxruntime C++ API\n");


	Ort::Session session(env, model_path, session_options);
	// print model input layer (node names, types, shape etc.)
	Ort::AllocatorWithDefaultOptions allocator;

	// print number of model input nodes
	size_t num_input_nodes = session.GetInputCount();
	std::vector<const char*> input_node_names = {
    "input" };
	std::vector<const char*> output_node_names = {
    "output" };

	//后面的数据类型全是float,换成double的报错,我也不知道怎么改,先将就吧
	//输入的维度,我是804*6维的特征,这两行依次写1,804,6
	std::vector<int64_t> input_node_dims = {
    1, 804, 6 }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值