Tenosrflow c++编译详细写写

上次的随便写写讲了一些常规操作,这次的话写一下tensorflow如何编译,然后用C++运行,版本为1.8,比较老了,新版本可能有点不一样。以后有需求更新新版本

克隆1.8版本的tensorflow到任意位置

git clone -b r1.8 https://github.com/tensorflow/tensorflow.git

protobuf 和 eigen不确定要不要装。反正我是装了,据说bazel的 workspace configuration中包括了下载和安装这些东西。

安装对应的protobuf版本!!!!!! (很重要)

在tensorflow/workspace.bzl 下我们可以确认一下需要什么protobuf版本,1.8版本的下载以下链接

https://mirror.bazel.build/github.com/google/protobuf/archive/396336eb961b75f03b25824fe86cf6490fb75e3a.tar.gz
 cd protobuf
 ./autogen.sh
 ./configure
 make
 make check
 sudo make install
 sudo ldconfig
 check version again
 protoc --version
 libprotoc 3.5.0

卸载 protobuf

protoc --version
cd protobuf
~/tensorflow_realtime_support_package/protobuf$ sudo make uninstall
protoc --version again
 libprotoc 3.7.1
bash: /usr/local/bin/protoc: No such file or directory
uninstall succeed!

安装Bazel

在安装前要核对一下要装的tensorflow对应的bazel版本,大概是看下面的链接

https://www.tensorflow.org/install/sourcewww.tensorflow.org

比如我的就是

tensorflow_gpu-1.8.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.10.0 7 9

然后按照bazel官网指示

第一步: 安装必要的库

First, install the prerequisites: pkg-config, zip, g++, zlib1g-dev, unzip, and python.

sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python

第二步: 下载Bazel

Next, download the Bazel binary installer named bazel-<version>-installer-linux-x86_64.sh from the Bazel releases page on GitHub.
第三步: 运行安装程序



chmod +x bazel-0.10.1-installer-linux-x86_64.sh 
./bazel-0.10.0-installer-linux-x86_64.sh --user


The --user flag installs Bazel to the $HOME/bin directory on your system and sets the .bazelrc path to $HOME/.bazelrc. Use the --help command to see additional installation options.
第四步: 设置环境变量

If you ran the Bazel installer with the --user flag as above, the Bazel executable is installed in your $HOME/bin directory. It’s a good idea to add this directory to your default paths, as follows:

export PATH="$PATH:$HOME/bin"

正式开始编译tensorflow c++

需要注意的是如果选择了cuda support则是GPU版本,不选默认CPU版本。

安装包路径下:
pc1@pc1:~/tensorflow$ 

查看 cudnn 版本
locate cudnn.h

cat /home/elabws1/Desktop/elab-disk/install-cuda-files/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2


配置bazel build
pc1@pc1:~/tensorflow$ ./configure 
You have bazel 0.10.0 installed.
Please specify the location of python. [Default is /home/elabws1/anaconda3/bin/python]: 

Found possible Python library paths:
  /home/elabws1/anaconda3/lib/python3.6/site-packages
Please input the desired Python library path to use.  Default is [/home/elabws1/anaconda3/lib/python3.6/site-packages]

Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: n
No jemalloc as malloc support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: n
No Google Cloud Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: n
No Hadoop File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: n
No Amazon S3 File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Apache Kafka Platform support? [Y/n]: n
No Apache Kafka Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with XLA JIT support? [y/N]: n
No XLA JIT support will be enabled for TensorFlow.

Do you wish to build TensorFlow with GDR support? [y/N]: n
No GDR support will be enabled for TensorFlow.

Do you wish to build TensorFlow with VERBS support? [y/N]: n
No VERBS support will be enabled for TensorFlow.

Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: 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, e.g. 7.0. [Leave empty to default to CUDA 9.0]: 


Please specify the location where CUDA 9.0 toolkit is installed. Refer to CORE 2 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.1.4


Please specify the location where cuDNN 7 library is installed. Refer to CORE 2 for more details. [Default is /usr/local/cuda]:


Do you wish to build TensorFlow with TensorRT support? [y/N]: n
No TensorRT support will be enabled for TensorFlow.

Please specify the NCCL version you want to use. [Leave empty to default to NCCL 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: CUDA GPUs.
Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 6.1]


Do you want to use clang as CUDA compiler? [y/N]: y
Clang will be used as CUDA compiler.

Do you wish to download a fresh release of clang? (Experimental) [y/N]: y
Clang will be downloaded and used to compile tensorflow.

Do you wish to build TensorFlow with MPI support? [y/N]: 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]: 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 build //tensorflow:libtensorflow_cc.so

编译完了以后:

大概过了半小时吧,然后就会发觉多了一些新的文件夹bazel-bind,bazel-genfiles等等

然后把东西移到系统目录下面就ok。

    mkdir /usr/local/include/tf
    sudo cp -r bazel-genfiles/ /usr/local/include/tf/
    sudo cp -r tensorflow /usr/local/include/tf/
    sudo cp -r third_party /usr/local/include/tf/
    sudo cp -r bazel-tensorflow /usr/local/include/tf/
    sudo cp -r bazel-bin/tensorflow/*.so /usr/local/lib/(拷贝 libtensorflow_framework.so 和libtensorflow_cc.so)

最后附赠一个我自己的cmakelist

cmake_minimum_required(VERSION 3.13) project(project)  set(CMAKE_CXX_STANDARD 11)  set(INC_DIR
        /usr/local/include/tf
        /usr/local/include/tf/bazel-genfiles
        /usr/local/include/tf/bazel-tensorflow/external/eigen_archive
        /usr/local/include/tf/bazel-tensorflow/external/protobuf_archive/src)  set(LINK_DIR
        /usr/local/lib)  include_directories(${INC_DIR})  link_directories(${LINK_DIR})   add_executable(project main.cpp) target_link_libraries(project tensorflow_cc tensorflow_framework)

    原文作者:宁默
    原文地址: https://zhuanlan.zhihu.com/p/68507512
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞