c++读取mnn模型

之前写了一篇文章训练了一个模型,并转换成mnn模型 ,并且使用了python进行调用

不过并没有使用c++进行调用

模型还是使用那篇文章的模型,调用代码如下:

#include <iostream>
#include<opencv2/core.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui.hpp>
#include<MNN/Interpreter.hpp>
#include<MNN/ImageProcess.hpp>

using namespace std;
using namespace cv;
using namespace MNN;
int main()
{   auto net = std::shared_ptr<MNN::Interpreter>(MNN::Interpreter::createFromFile("E:\\vs2019\\first\\first\\model-flow.mnn"));//创建解释器
	cout << "Interpreter created" << endl;
	ScheduleConfig config;
	config.numThread = 8;
	config.type = MNN_FORWARD_CPU;
	auto session = net->createSession(config);//创建session
	cout << "session created" << endl;
	//读取图片
	cv::Mat image = cv::imread("C:\\Users\\Administrator\\Pictures\\397.jpg");
	cv::Mat img_resized;
	cv::resize(image, img_resized, cv::Size(180,180));

	auto inTensor = net->getSessionInput(session, NULL);
	auto outTensor = net->getSessionInput(session, NULL);
	auto _Tensor = MNN::Tensor::create<float>(inTensor->shape(), NULL, MNN::Tensor::TENSORFLOW);
	//cout << _Tensor->elementSize() << endl;
	for (int i = 0; i < _Tensor->elementSize(); i++) {
		_Tensor->host<float>()[i] = image.data[i];
	}
	inTensor->copyFromHostTensor(_Tensor);
	inTensor->printShape();
	//cout << *(inTensor->host<float>()+1) << endl;
	//_Tensor->print();
	_Tensor->printShape();
	//推理
	net->runSession(session);
    auto output= net->getSessionOutput(session, NULL);
	MNN::Tensor feat_tensor(output, output->getDimensionType());
	output->copyToHostTensor(&feat_tensor);
	feat_tensor.print();
	waitKey(0);
	return 0;
}

运行结果如下:

注意的是,数据结果和之前使用python调用的结果并不一致,应该是赋值数据的时候rgb的顺序颠倒了

之所以不是使用很多文章使用memcpy的方式读取input张量,因为这个在我的机器一直memcpy拷贝不成功 ,数据为系统自动初始化的数值,会导致每次的结果不一致,是随机数

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值