c++调用pb模型的过程

开启计算图

头文件
#include <tensorflow/core/framework/op_kernel.h>
#include <tensorflow/core/public/session.h>
#include <tensorflow/core/platform/env.h>
tensorflow::Session *p_session;
tensorflow::Session *p2_session;

// 开启计算图
bool CreateGraph(string model_path, int flag = 1)
{
	tensorflow::GraphDef graph;
	//tensorflow::Session  tmpsession;
	tensorflow::Status ret = tensorflow::ReadBinaryProto(tensorflow::Env::Default(), model_path, &graph);
	if (!ret.ok())
	{
		myprint("模型加载失败");
		return false;
	}
	if (flag == 1)
	{
		NewSession(tensorflow::SessionOptions(), &p_session);
		ret = p_session->Create(graph);
		//NewSession(tensorflow::SessionOptions(), newp_session(p_session));
	}
	else
	{
		NewSession(tensorflow::SessionOptions(), &p2_session);
		ret = p2_session->Create(graph);
	}
	//NewSession(tensorflow::SessionOptions(), &p_session);
	if (!ret.ok())
	{
		return false;
	}
	return true;
}

得出结果

// 分类结果
int CalcClassify(Mat transrc, int whsize, int flag = 1)
{

	int image_width = whsize;  //32
	int image_height = whsize; //32
	int image_channel = 1;
	int image_batch = 1;
	//ReadPicToVec(img_path, image_height,image_width);
	//cv::Mat src = imread(img_path, 0);
	Mat src = transrc.clone();
	//Mat dst(Size(image_width,image_height),CV_8UC1,Scalar::all(0));
	Mat dst;
	resize(src, dst, Size(image_width, image_height), 0, 0, INTER_LINEAR);
	//imshow("123", dst);
	//waitKey(0);
	int row = dst.rows;
	int col = dst.cols*dst.channels();
	//std::cout << row << "," << col << std::endl;
	vector<float> src_data;
	for (int i = 0; i < row; ++i)
	{
		uchar *data = dst.ptr<uchar>(i);
		for (int j = 0; j < col; ++j)
		{
			src_data.push_back((float(data[j]) / 255 - 0.5));
		}
	}

	/*
	std::vector<float> src_data;
	for (size_t i = 0; i < image_width*image_height*image_batch*image_channel; i++)
	{
	src_data.push_back(0.5);
	}
	*/

	tensorflow::Tensor image_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({ image_batch,image_height,image_width,image_channel }));
	std::copy_n(src_data.begin(), src_data.size(), image_tensor.flat<float>().data());
	const std::string input_layer_name = "input:0";
	const std::string output_layer_name = "MobilenetV1/Predictions/Reshape_1:0";

	std::vector < std::pair<std::string, tensorflow::Tensor>> inputs;
	inputs.push_back(std::pair <std::string, tensorflow::Tensor>(input_layer_name, image_tensor));
	std::vector<tensorflow::Tensor> outputs;
	tensorflow::Status run_status;
	if (flag == 1)
	{
		run_status = p_session->Run(inputs, { output_layer_name }, {}, &outputs);

	}
	else
	{
		run_status = p2_session->Run(inputs, { output_layer_name }, {}, &outputs);

	}

	if (!run_status.ok())
	{
		//tensorflow::LogAllRegisteredKernels();
		std::cout << "tensorflow running model failed \n" << std::endl;
		//return 0;
	}


	tensorflow::Tensor *ts_out = &outputs[0];
	const Eigen::TensorMap<Eigen::Tensor<float, 1, Eigen::RowMajor>, Eigen::Aligned> prediction = ts_out->flat<float>();

	//std::cout << "predict:" << std::endl;

	float MaxScore = 0.0;
	int MaxLoc = -1;
	for (size_t i = 0; i < prediction.size(); i++)
	{
		//cout << prediction(i) << " ";
		//printf("%e ", prediction(i));

		if (prediction(i) > MaxScore)
		{
			MaxScore = prediction(i);
			MaxLoc = i;
		}
	}

	return MaxLoc;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值