c++调用tensorflow的pb模型

本文介绍了一个使用OpenCV和DNN进行图像识别的C++程序。程序读取图像,将其转换为神经网络输入格式,并使用预训练的TensorFlow模型进行预测。展示了如何设置计算后端和目标,以及如何测量预测时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<fstream>
#include<opencv2/opencv.hpp>
#include<opencv2/dnn.hpp>

#include <Windows.h>
#pragma comment( lib, "winmm.lib")

using namespace std;
using namespace cv;
using namespace cv::dnn;

Mat predict(Net net, char * dir)
{
	Mat img = imread(dir, -1);
	Mat inputBlob = blobFromImage(img, 1.0 / 255.0, Size(340, 340), Scalar(), false, false);

	net.setInput(inputBlob);

	Mat detection = net.forward();

	return detection;
}

	
int main()
{
	string strResult, strTemp;
	char czPath[256] = { 0 };
	GetModuleFileName(NULL, czPath, 256);  //获取全路径
	string strPath = czPath;
	cout << "文件名(带全路径) : " << strPath.c_str() << endl;
	int nPos = strPath.find('x'); //查找最后一个 \ 出现的位置
	strTemp = strPath.substr(0, nPos);
	String weight = strTemp+"tmp\\new_tensor_model.pb";
	cout<< weight<< endl;

	Net net = readNetFromTensorflow(weight);

	//设置计算后台(GPU调用,如需修改按ctrl+点击setPreferableTarget)
	net.setPreferableBackend(DNN_BACKEND_OPENCV);
	net.setPreferableTarget(DNN_TARGET_OPENCL);

	/*打出节点
    vector<string> outLayersNames = net.getLayerNames();
	int lens = outLayersNames.size();
	cout << lens << endl;
	for(int i = 0; i < lens; i++)
	{ 
		cout << outLayersNames[i] << endl;
	}*/


	DWORD t1, t2;
	char dir[] = "D:/hyd/silk_baby2/si/2/l2_9.jpg";
	Mat detection1 = predict(net, dir);
	t1 = GetTickCount();
	Mat detection = predict(net, dir);
	cout << detection << endl;
	t2 = GetTickCount();
	cout<< (t2 - t1)*1.0 / 1000 << endl;
}

 

C++中使用TensorFlow调用PB(Protocol Buffers)模型进行推断,你需要先确保你已经安装了TensorFlow for C++的相关库和依赖项。以下是一个简要步骤介绍: 1. **准备PB模型文件**:确保你有一个训练好的模型,并且已经导出了为PB格式的文件,这通常包括`.pb`文件(包含模型结构)和`.pbtxt`文件(包含模型的元数据,如果有的话)。 2. **安装TensorFlow C++库**:TensorFlow提供了一个名为`tensorflow_cc`的C++ API。你需要根据你的系统配置安装这个库。 3. **加载模型**:使用TensorFlow C++ API加载`.pb`文件,创建一个`tensorflow::Session`对象。 4. **准备输入数据**:根据模型的要求准备输入数据,将数据填充到`tensorflow::Tensor`对象中。 5. **运行推断**:通过`tensorflow::Session`的`Run`方法,使用模型执行推断,传入输入数据和输出节点名称,获取推断结果。 6. **处理输出数据**:从`Run`方法返回的`tensorflow::Tensor`对象中提取输出数据。 下面是一个简单的代码示例: ```cpp #include "tensorflow/cc/client/client_session.h" #include "tensorflow/cc/saved_model/loader.h" #include "tensorflow/core/framework/tensor.h" int main() { // 创建一个SessionOptions并设置相关选项(如果需要) tensorflow::ClientSession session(tensorflow::SessionOptions()); // 加载SavedModel std::string export_dir = "/path/to/saved_model"; std::map<std::string, tensorflow::Tensor> inputs; std::vector<std::pair<string, tensorflow::Tensor>> outputs; // 从export_dir加载模型 tensorflow::Status load_status = tensorflow::LoadSavedModel( session.options(), export_dir, {"serve"}, &session, &inputs, &outputs); if (!load_status.ok()) { // 错误处理... } // 准备输入数据 // inputs["input_node_name"] = ...; // 运行模型 session.Run(inputs, {"output_node_name"}, &outputs); // 获取推断结果 // auto result_tensor = outputs["output_node_name"]; // 处理结果... return 0; } ``` 请确保将`/path/to/saved_model`替换为你的模型保存路径,并且将`input_node_name`和`output_node_name`替换为实际的输入和输出节点名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值