因为项目上有要求,所以得配置一下tensorflow c++版本接口,因为LZ只要测试下算法inference的时间,所以对于接口可能还不是那么熟悉,但是还是摸索出来配置tensorflow接口的一套过程,当然其中参考博客也挺多的,可能不能全都写到参考链接里。
第一步,确定你要安装的tensorflow的版本,这个很重要!很重要!很重要!
因为不同版本对应的bazel和protocbuf的版本是不一样的,LZ选择是tensorflow 1.14.1, 对应的bazel的版本是0.24.1
bazel是用来编译tensorflow的,所以bazel安装教程:https://docs.bazel.build/versions/master/install.html#installing-menu
安装bazel:
安装依赖项
sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python3centos:
sudo yum install pkg-config zip g++ zlib1g-dev unzip python3下载需要安装的版本,bazel的下载地址github上也有:https://github.com/bazelbuild/bazel/releases/tag/0.24.1,
然后进行安装:
chmod +x bazel-<version>-installer-linux-x86_64.sh ./bazel-<version>-installer-linux-x86_64.sh --user设置对应环境
export PATH="$PATH:$HOME/bin"到此准备工作基本完成
第二步,下载eigen
看LZ前以前博客就知道,eigen的版本很重要,因为一定要3.3版本以上的,eigen很好装,就不多说了。
mkdir build cd build cmake .. make install补充:
还要安装protobuf,LZ下载的是protobuf-3.7.1.tar.gz
安装依赖项:
// centos: sudo yum install autoconf automake libtool curl make g++ unzip //ubuntu: sudo apt-get install autoconf automake libtool curl make g++ unzip解压安装:
tar -xzvf protobuf-3.7.1.tar.gz //配置脚本 ./autogen.sh //编译与安装 ./configure make make check sudo make install sudo ldconfig # refresh shared library cache.第三步:下载tensorflow源码编译
git clone https://github.com/tensorflow/tensorflow.git cd tensorflow git checkout branch_name # r1.9, r1.10, etc.然后cd到tensorflow中进行配置
./configure具体配置可以参考一下:
./configure You have bazel 0.15.0 installed. Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python2.7 Found possible Python library paths: /usr/local/lib/python2.7/dist-packages /usr/lib/python2.7/dist-packages Please input the desired Python library path to use. Default is [/usr/lib/python2.7/dist-packages] Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: jemalloc as malloc support will be enabled for TensorFlow. Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: Google Cloud Platform support will be enabled for TensorFlow. Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: Hadoop File System support will be enabled for TensorFlow. Do you wish to build TensorFlow with Amazon AWS Platform support? [Y/n]: Amazon AWS Platform support will be enabled for TensorFlow. Do you wish to build TensorFlow with Apache Kafka Platform support? [Y/n]: Apache Kafka Platform support will be enabled for TensorFlow. Do you wish to build TensorFlow with XLA JIT support? [y/N]: No XLA JIT support will be enabled for TensorFlow. Do you wish to build TensorFlow with GDR support? [y/N]: No GDR support will be enabled for TensorFlow. Do you wish to build TensorFlow with VERBS support? [y/N]: No VERBS support will be enabled for TensorFlow. Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: No OpenCL SYCL support will be enabled for TensorFlow. Do you wish to build TensorFlow with CUDA support? [y/N]: Y CUDA support will be enabled for TensorFlow. Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]: 9.0 Please specify the location where CUDA 9.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 7.0 Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: Do you wish to build TensorFlow with TensorRT support? [y/N]: No TensorRT support will be enabled for TensorFlow. Please specify the NCCL version you want to use. If NCLL 2.2 is not installed, then you can use version 1.3 that can be fetched automatically but it may have worse performance with multiple GPUs. [Default is 2.2]: 1.3 Please specify a list of comma-separated Cuda compute capabilities you want to build with. You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus. Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 3.5,7.0] 6.1 Do you want to use clang as CUDA compiler? [y/N]: nvcc will be used as CUDA compiler. Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: Do you wish to build TensorFlow with MPI support? [y/N]: No MPI support will be enabled for TensorFlow. Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: Not configuring the WORKSPACE for Android builds. Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See tools/bazel.rc for more details. --config=mkl # Build with MKL support. --config=monolithic # Config for mostly static monolithic build. Configuration finished配置完成后,用bazel编译C++接口:
如果使用cuda的话
bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so单纯CPU的话:
bazel build --config=opt //tensorflow:libtensorflow_cc.so第四步:很重要,非常重要,编译成功后,其实还有一堆包没有编译
cd tensorflow/contrib/makefile sh build_all_linux.sh一定要保证网比较好的情况!不然下载很痛苦的/(ㄒoㄒ)/~~
第五步:测试代码
// tensorflow/cc/example/example.cc#include<tensorflow/c/c_api.h>#include<tensorflow/core/platform/env.h>#include<tensorflow/core/public/session.h>#include<iostream>using namespace std;using namespace tensorflow;intmain(){Session*session;Status status=NewSession(SessionOptions(),&session);if(!status.ok()){cout<<status.ToString()<<"\n";return1;}cout<<"Session successfully created.\n";std::cout<<"Hello from TensorFlow C library version "<<TF_Version();return0;}CMakeLists.txt的内容:
set(CMAKE_CXX_STANDARD 11) set(SOURCE_FILES main.cpp) include_directories( /home/felaim/Documents/software/tensorflow-r1.14 /home/felaim/Documents/software/tensorflow-r1.14/tensorflow/bazel-genfiles /home/felaim/Documents/software/tensorflow-r1.14/tensorflow/contrib/makefile/gen/protobuf/include /home/felaim/Documents/software/tensorflow-r1.14/tensorflow/contrib/makefile/gen/host_obj /home/felaim/Documents/software/tensorflow-r1.14/tensorflow/contrib/makefile/gen/proto /home/felaim/Documents/software/tensorflow-r1.14/tensorflow/contrib/makefile/downloads/nsync/public /home/felaim/Documents/software/tensorflow-r1.14/tensorflow/contrib/makefile/downloads/eigen /home/felaim/Documents/software/tensorflow-r1.14/bazel-out/local_linux-py3-opt/genfiles /home/felaim/Documents/software/tensorflow-r1.14/tensorflow/contrib/makefile/downloads/absl ) add_executable(Tensorflow_test ${SOURCE_FILES}) target_link_libraries(Tensorflow_test /home/felaim/Documents/software/tensorflow-r1.14/bazel-bin/tensorflow/libtensorflow_cc.so /home/felaim/Documents/software/tensorflow-r1.14/bazel-bin/tensorflow/libtensorflow_framework.so.1 )最后运行的结果
希望小伙伴都能少走弯路!↖(ω)↗
ERROR合集:
1 ImportError: No module named builtins
pip install future2.permission denied
要对build_all_linux.sh, download_dependencies赋权限
参考链接:
https://tensorflow.google.cn/install/source