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研究与应用,tensorflow,c++,bazel,builf,Duplicate,regidtration,of,devi)