前文我们搭建了python的环境,并且安装了cuda与cuDNN。接下来我们编译tensorflow的c++接口,与python不同,tensorflow的c++接口各版本间并没有什么显著的不同(tensorflow2.3除外),所以你可以安装除2.3外的任何一个版本,但要和cuda的版本相匹配,这里我编译的是tensorflow2.0,其余版本的编译与本文相同。
1、下载安装编译工具bazel,bazel与tensorflow的版本关系如图所示:
如图所示,我们需要下载bazel0.26.1,进入https://github.com/bazelbuild/bazel/tags?after=0.28.1,下载bazel-0.26.1-installer-linux-x86_64.sh,同样,如果下载速度太慢建议使用github代下载网站。
下载完成后,按照官网:https://docs.bazel.build/versions/0.26.0/install-ubuntu.html 的安装教程完成安装。注意,版本选择0.26
2、编译安装tensorflow
#下载tensorflow源码
git clone https://github.com/tensorflow/tensorflow.git
# 进入tensorflow文件夹
cd tensorflow
# 切换到1.7版本:
git checkout r2.0
#当然,你也可以直接在github上用代下载网站下载tensorflow-r2.0的压缩包,这样快一些。
# 执行configure
sudo ./configure
接下来会要求选择配置,选择如下(顺序可能不同,但大体上差不多,如果我出现我这里没有的选项,建议百度):
Found possible Python library paths:
/home/lvfengkun/anaconda3/lib/python3.6/site-packages
Please input the desired Python library path to use. Default is [/home/lvfengkun/anaconda3/lib/python3.6/site-packages]
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 ROCm support? [y/N]: N
No ROCm 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.
Do you wish to build TensorFlow with TensorRT support? [y/N]: N
No TensorRT support will be enabled for TensorFlow.
Found CUDA 10.1 in:
/usr/local/cuda-10.1/targets/x86_64-linux/lib
/usr/local/cuda-10.1/targets/x86_64-linux/include
Found cuDNN 7 in:
/usr/local/cuda-10.1/targets/x86_64-linux/lib
/usr/local/cuda-10.1/targets/x86_64-linux/include
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. Each capability can be specified as "x.y" or "compute_xy" to include both virtual and binary GPU code, or as "sm_xy" to only include the binary code.
Please note that each additional compute capability significantly increases your build time and binary size, and that TensorFlow only supports compute capabilities >= 3.5 [Default is: 6.1]:
Do you want to use clang as CUDA compiler? [y/N]: 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]:
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]:
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 .bazelrc for more details.
--config=mkl # Build with MKL support.
--config=monolithic # Config for mostly static monolithic build.
--config=ngraph # Build with Intel nGraph support.
--config=numa # Build with NUMA support.
--config=dynamic_kernels # (Experimental) Build kernels into separate shared objects.
--config=v2 # Build TensorFlow 2.x instead of 1.x.
Preconfigured Bazel build configs to DISABLE default on features:
--config=noaws # Disable AWS S3 filesystem support.
--config=nogcp # Disable GCP support.
--config=nohdfs # Disable HDFS support.
--config=nonccl # Disable NVIDIA NCCL support.
Configuration finished
此时你已经选择完了配置,开始使用bazel编译
bazel build --config=opt //tensorflow:libtensorflow_cc.so // 无显卡,cpu
bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so // 有显卡
编译比较慢并且对网络有比较大的要求,建议在网速好的时候进行编译
最后显示类似如下的信息,说明编译成功了:
....
Target //tensorflow:libtensorflow_cc.so up-to-date:
bazel-bin/tensorflow/libtensorflow_cc.so
INFO: Elapsed time: 1192.883s, Critical Path: 174.02s
INFO: 654 processes: 654 local.
INFO: Build completed successfully, 656 total actions
回到tensorflow的目录下执行:
./tensorflow/contrib/makefile/download_dependencies.sh
完成后会有一个download文件夹在makefile文件夹中。
cd tensorflow/contrib/makefile
./build_all_linux.sh
成功后会出现一个gen文件夹
操作完成后需要把"tensorflow/bazel-genfiles/tensorflow/"中的cc和core文件夹中的内容copy到"tensorflow/tensorflow/"中,然后完成覆盖即可,这一步是为了复制.pb.h和.cc文件。
完成这一步之后, 再把必要.h头文件以及编译出来.so的动态链接库文件复制到指定的一些路径下:
sudo 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 bazel-bin/tensorflow/libtensorflow_cc.so /usr/local/lib/
sudo cp bazel-bin/tensorflow/libtensorflow_framework.so /usr/local/lib
OK到此为止,tensorflow C++的接口已经搞定!
3、测试
测试所需文件在我的github中:https://github.com/lvfengkun/tensorflow-model-demo/tree/master
使用时需将CMakeLists中的路径进行更改。创建并进入build中编译运行程序,结果如下图所示,表示测试成功。