tensorflow c++编译及调用

tensorflow的接口强大,同时模型及ops也多,许多顶会论文都是在tensorflow基础上发表的,但主要注重python接口,这点比不上mxnet

那么,如何编译c++库及接口,如何调用生成的模型进行推理呢?本文以label_image中的例子为主,介绍主要点,希望对大家有所帮助:

1. 环境:centos7, tensorflow-1.13, gcc 4.8.5

2. 编译:cd到tensorflow源码根目录下

./configure(配置好GPU, CUDNN安装路径等, 大部分默认,回车跳过即可)

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]: --config=mkl #可以设置mkl库优化等,此设置是针对CPU而言

configure完成后:

bazel build -c opt  //tensorflow:libtensorflow_cc.so #在bazel-bin目录下,生成核心库

bazel build -c opt  //tensorflow:libtensorflow_framework.so #在bazel-bin目录下,生成核心库

也可以如下,支持汇编指令集优化(针对CPU推理):

bazel build -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-msse4.2 //tensorflow:libtensorflow_cc.so

bazel build -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-msse4.2 //tensorflow:libtensorflow_framework.so

3. cmake编译例子,这个我觉得是最方便的方法,不用把编译生成的库和头文件拷贝来拷贝去,不容易缺少文件:

cd tensorflow/examples/label_image

CMakeLists.txt如下:

  1 #
  2 cmake_minimum_required(VERSION 3.14)
  3 #
  4 project(main)
  5 #
  6 set(CMAKE_CXX_STANDARD 11)
  7 #
  8 set(TENSORFLOW_DIR /home/wangfengming/work/tensorflow/)
  9 #
 10 include_directories(${TENSORFLOW_DIR})
 11 include_directories(${TENSORFLOW_DIR}/bazel-genfiles)
 12 include_directories(${TENSORFLOW_DIR}/tensorflow/contrib/makefile/gen/proto)
 13 include_directories(${TENSORFLOW_DIR}/tensorflow/contrib/makefile/gen/protobuf-host/include)
 14 include_directories(${TENSORFLOW_DIR}/tensorflow/contrib/makefile/downloads/eigen)
 15 include_directories(${TENSORFLOW_DIR}/tensorflow/contrib/makefile/downloads/nsync/public)
 16 include_directories(${TENSORFLOW_DIR}/tensorflow/contrib/makefile/downloads/absl)
 17 
 18 #
 19 link_directories(${TENSORFLOW_DIR}/tensorflow/contrib/makefile/gen/lib)
 20 link_directories(${TENSORFLOW_DIR}/tensorflow/contrib/makefile/gen/protobuf-host/lib)
 21 link_directories(${TENSORFLOW_DIR}/tensorflow/contrib/makefile/downloads/nsync/builds/default.linux.c++11)
 22 link_directories(${TENSORFLOW_DIR}/bazel-bin/tensorflow)
 23 
 24 add_executable(main main.cc)
 25 #
 26 target_link_libraries(main tensorflow_framework)
 27 target_link_libraries(main tensorflow_cc)

cmake .

make即可

 

4. 出现的问题:

有时候,虽然编译成功,但运行demo时出现如下问题:

duplicate registration of device factory for type gpu with the same priority 210

则检查一下,你是否用了--copt=monolithic编译选项,这个选项的意思是生成尽可能少的动态链接库,使得整个程序集中一下,但会出问题,具体原因没找到,但猜测应该是符号重复或者冲突,多次注册了GPU设备.

如果出现这个问题,去这个选项,重新编译试试,希望能解决您遇到的问题

TensorFlow 2 可以使用 C++ API 进行编译,以便在 C 或 C++调用。以下是一些基本步骤: 1. 首先,您需要安装 TensorFlow 2 的 C++ API。可以使用以下命令: ```shell pip install tensorflow-cpp ``` 2. 然后,您需要编写一个 C 或 C++ 程序,以便在其中调用 TensorFlow 2。例如,以下是一个简单的 C++ 程序,它使用 TensorFlow 2 来执行一个简单的矩阵乘法: ```c++ #include <iostream> #include <tensorflow/c/c_api.h> int main() { // 创建 TensorFlow 会话 TF_SessionOptions* session_options = TF_NewSessionOptions(); TF_Status* status = TF_NewStatus(); TF_Session* session = TF_NewSession(session_options, status); TF_DeleteSessionOptions(session_options); // 加载模型 TF_Graph* graph = TF_NewGraph(); TF_Buffer* graph_def = TF_NewBuffer(); TF_SessionRunOptions* run_options = TF_NewSessionRunOptions(); TF_SessionPRunSetup* prun_setup = TF_NewSessionPRunSetup(); TF_Buffer* error_msg = TF_NewBuffer(); TF_Status* status_load = TF_NewStatus(); TF_SessionOptions* sess_opts = TF_NewSessionOptions(); TF_GraphImportGraphDef(graph, graph_def, NULL, status_load); // 创建输入张量 const std::int64_t dims[] = {2, 2}; const std::size_t ndims = 2; float input_values[] = {1, 2, 3, 4}; TF_Tensor* input_tensor = TF_NewTensor(TF_FLOAT, dims, ndims, input_values, sizeof(input_values), NULL, NULL); // 创建输出张量 TF_Output output_op = {TF_GraphOperationByName(graph, "MatMul"), 0}; TF_Tensor* output_tensor = NULL; // 运行会话 TF_SessionRun(session, run_options, &output_op, &input_tensor, 1, &output_tensor, 1, NULL, 0, prun_setup, status); TF_DeleteSessionRunOptions(run_options); TF_DeleteSessionPRunSetup(prun_setup); // 打印结果 float* output_values = static_cast<float*>(TF_TensorData(output_tensor)); std::cout << output_values[0] << ", " << output_values[1] << std::endl; // 清理资源 TF_DeleteGraph(graph); TF_DeleteBuffer(graph_def); TF_DeleteTensor(input_tensor); TF_DeleteTensor(output_tensor); TF_DeleteStatus(status); TF_DeleteStatus(status_load); TF_DeleteSession(session, status); TF_DeleteSessionOptions(sess_opts); return 0; } ``` 3. 编译上述程序。在 Linux 上,可以使用以下命令: ```shell g++ -std=c++11 -I/usr/local/include -L/usr/local/lib -ltensorflow -o my_program my_program.cpp ``` 注意,在这个命令中,我们使用了 TensorFlow 库的标准名称“libtensorflow.so”来链接库。 4. 运行编译后的程序。 ```shell ./my_program ``` 这将输出矩阵乘积的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

seasermy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值