一、参考文档
我的前两篇博客中,分别介绍了Ubuntu16.04下Opencv3.4.0的编译以及Tensorflow C++ API的编译。
这篇文章就来讲一下,如何联调编译以及写对应的Cmakelist.txt文件。
参考链接:
https://blog.csdn.net/qq_30534935/article/details/96023523
https://blog.csdn.net/qq_30534935/article/details/96353857
二、代码编写
来源于:
https://blog.csdn.net/flyconley/article/details/96436323
内容如下,侵删:
#include "tensorflow/core/public/session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/protobuf/meta_graph.pb.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace tensorflow;
GraphDef CreateGraphDef()
{
Scope root = Scope::NewRootScope();
auto X = ops::Placeholder(root.WithOpName("x"), DT_FLOAT,
ops::Placeholder::Shape({ -1, 2 }));
auto A = ops::Const(root, { { 3.f, 2.f },{ -1.f, 0.f } });
auto Y = ops::MatMul(root.WithOpName("y"), A, X,
ops::MatMul::TransposeB(true));
GraphDef def;
TF_CHECK_OK(root.ToGraphDef(&def));
return def;
}
void CVMat_to_Tensor(cv::Mat img, Tensor* output_tensor, int input_rows, int input_cols)
{
img.convertTo(img, CV_32FC1);
float *p = output_tensor->flat<float>().data();
cv::Mat tempMat(input_rows, input_cols, CV_32FC1, p);
img.convertTo(tempMat, CV_32FC1);
}
int main()
{
Session* session;
Status status = NewSession(SessionOptions(), &session);
if (!status.ok()) {
std::cout << status.ToString() << std::endl;
}
else {
std::cout << "Session created successfully" << std::endl;
}
GraphDef graph_def;
status = ReadBinaryProto(Env::Default(), "chi.pb", &graph_def);
if (!status.ok()) {
std::cout << status.ToString() << std::endl;
}
else {
std::cout << "Load graph protobuf successfully" << std::endl;
}
status = session->Create(graph_def);
if (!status.ok()) {
std::cout << status.ToString() << std::endl;
}
else {
std::cout << "Add graph to session successfully" << std::endl;
}
cv::Mat img = cv::imread("char_33.jpg", 0);
if (!img.empty()) {
std::cout << "open the image successfully" << std::endl;
}
cv::Mat resize_image;
int length = img.cols * 32 / float(img.rows);
cv::resize(img, resize_image, cv::Size(length, 32));
cv::transpose(resize_image, resize_image);
cv::imwrite("test.jpg", resize_image);
Tensor input_tensor(DT_FLOAT, TensorShape({ 1,length,32,1 }));
CVMat_to_Tensor(resize_image, &input_tensor, length, 32);
cout << input_tensor.DebugString() << endl;
Tensor seq_len(DT_INT32, TensorShape({ 1 }));
int *s = seq_len.flat<int>().data();
std::vector<int> mydata;
mydata.push_back (length / 4 - 1);
copy_n(mydata.begin(), 1, s); //复制mydata到dst
cout << seq_len.DebugString() << endl;
cout << endl << "<-------------Running the model with test_image--------------->" << endl;
//前向运行,输出结果一定是一个tensor的vector
vector<tensorflow::Tensor> outputs;
string output_node = "output";
Status status_run = session->Run({ { "input", input_tensor },{"seq_len", seq_len} }, { output_node }, {}, &outputs);
clock_t start, ends;
start = clock();
//Tensor input_tensor(DT_FLOAT, TensorShape({ 1,length,32,1 }));
CVMat_to_Tensor(resize_image, &input_tensor, length, 32);
status_run = session->Run({ { "input", input_tensor },{ "seq_len", seq_len } }, { output_node }, {}, &outputs);
ends = clock();
std::cout << ends - start <<"ms"<< std::endl;
if (!status_run.ok()) {
cout << "ERROR: RUN failed..." << std::endl;
cout << status_run.ToString() << "\n";
return -1;
}
std::cout << outputs[0].DebugString() << std::endl;
outputs[0].flat<tensorflow::int64>().data()[0] ;
for (int i = 0; i < outputs[0].shape().dim_size(1); ++i)
{
std::cout << outputs[0].flat<tensorflow::int64>().data()[i] << " ";
}
cout << endl;
}
三、Cmakelist.txt编写
将参考文档的整合一起即可:
cmake_minimum_required(VERSION 2.8)
project(tftest)
find_package(OpenCV REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++11 -W")
include_directories(
./lib
/home/zhangru/tensorflow
/home/zhangru/tensorflow/bazel-genfiles
/home/zhangru/tensorflow/bazel-bin/tensorflow
/home/zhangru/tensorflow/tensorflow/contrib/makefile/downloads/nsync/public
/home/zhangru/tensorflow/tensorflow/contrib/makefile/gen/protobuf/include
/usr/local/include/eigen3
)
add_executable(tftest tfcpptest.cpp)
target_link_libraries(tftest tensorflow_cc tensorflow_framework ${OpenCV_LIBS})
这里请注意这些文件需要放到对应的路径:
usr/local/include/opencv
usr/local/include/opencv2
usr/local/lib/libopecv.so(大约51个)
usr/local/share/OpenCV
home/zhangru/tesorflow ..
usr/local/include/eigen3 ..
usr/local/include/google..
四、程序测试
依次在命令行输入以下代码:
sudo cmake ..
sudo make
sudo ./tftest