1.编译
bazel build //tensorflow:libtensorflow_cc.so
出现linking of rule failed的问题,更改tensorflow版本1.4解决问题
bazel-bin/tensorflow/目录下会出现libtensorflow_cc.so文件
2.配置其他依赖
tensorflow/contrib/makefile目录下,执行build_all_linux.sh文件,成功后会在makefile目录下出现一个gen文件夹
出现./autogen.sh: 48: ./autogen.sh: autoreconf: not found
解决sudo apt-get install autoconf automake libtool
3.测试
#include
#include
#include
using namespace std;
using namespace tensorflow;
int main(int argc, char **argv) {
Session* session;
Status status = NewSession(SessionOptions(), &session);
if(!status.ok()){
cout << status.ToString() << endl;
}
cout << "Session successfully created." << endl;
return 0;
}
4.cmake
cmake_minimum_required(VERSION 2.6)
project(DeeperDepthPrediction)
set( CMAKE_CXX_FLAGS "-std=c++11" )
add_executable(FirstTry FirstTry.cpp)
include_directories( /home/ai/Programs/TensorFlow
/home/ai/Programs/TensorFlow/bazel-genfiles
/home/ai/Programs/TensorFlow/tensorflow/contrib/makefile/gen/protobuf/include
/home/ai/Programs/TensorFlow/tensorflow/contrib/makefile/downloads/nsync/public
/home/ai/Programs/TensorFlow/tensorflow/contrib/makefile/downloads/eigen
)
target_link_libraries( FirstTry
/home/ai/Programs/TensorFlow/bazel-bin/tensorflow/libtensorflow_cc.so
/home/ai/Programs/TensorFlow/bazel-bin/tensorflow/libtensorflow_framework.so
)
5.常用语句
模型加载
GraphDef graphdef;
Status status_load =ReadBinaryProto(Env::Default(), fire_path, &graphdef);
创建会话
Session* session;
Status status = NewSession(SessionOptions(), &session);
Status status_creat = session->Create(graphdef);
输入Mat–tensor
Tensor inputImg(DT_FLOAT, TensorShape({*, *, *, *}));
auto inputImageMapped = inputImg.tensor<float, 4>();
for (int i=0; i< *; i++){
Mat image = images[i];
for(int y=0; y<*; y++){
uchar* pixel = image.ptr<uchar>(y);
for(int x=0; x<28; x++){
inputImageMapped(i,y,x,0) = pixel[x] *1/255.0;
}
}
}
会话运行
Status status_run = session->Run({{"inputImg",inputImg}}, {"result1"},{}, &pred_tensor);
if(!status_run.ok())
cout <<"run failed"<< endl;
输出tensor–Mat
Tensor result = pred_tensor[0];
auto result_map = result.tensor<float, 2>();
for(int y=0; y<*; y++){
float* pixel = pred.ptr<float>(y);
for(int x=0; x<*; x++){
pixel[x] = result_map(y,x);
}
}