开启计算图
头文件
#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;
}