Tensorflow安装版本:Tensorflow-gpu 1.6(其他版本类似)
Tensorflow安装方法: 源码编译
系统:ubuntu 16.04.4
安装步骤:
一、确定系统、GPU Compute Capability、cuda、cudnn、gcc、python、Bazel 0.9.0等版本要求,参考文献2,3
系统:ubuntu 16.04.4 (查看命令:cat /etc/issue)
GPU:Quadro P6000(GPU Compute Capability:6.1)
Cuda:9.0.176 (查看命令:cat /etc/issue)
Cudnn:7.0.5(查看命令:cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2)
Python:2.7、3.3-3.6
编译器:GCC 4.8(查看命令:gcc -v; 切换gcc版本命令:sudo update-alternatives --config gcc)
具体操作参考参考文献(6)
编译工具:Bazel 0.9.0
其他安装方法略,简单叙述Bazel 安装:
有多种Bazel安装方法,本文选用Using the binary installer方法,如下:
1.Bazel安装需要C++编译器和unzip/zip,命令如下(已安装可省略)
sudo apt install g++ unzip zip
2.根据bazel版本下载对应安装文件bazel-<version>-installer-linux-x86_64.sh
下载网址:https://github.com/bazelbuild/bazel/releases
3.执行安装:
chmod +x bazel-<version>-installer-linux-x86_64.sh
./bazel-<version>-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.
4.设置环境变量
export PATH="$PATH:$HOME/bin"
二、下载tensorflow源码
1.使用 Git 克隆 TensorFlow 代码库:
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
代码库默认为 master 开发分支。您也可以检出要编译的版本分支:
git checkout branch_name # r1.9, r1.10, etc.
2.配置编译系统
通过在 TensorFlow 源代码树的根目录下运行以下命令来配置编译系统:
./configure
编译选项很多,注意:
(1)除特别需求,只在cuda、cudnn上选择y,其他均选择n
(2)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]: 2.3.7。 该项根据实际NCLL根据实际安装情况确定,如没有2.2可选择1.3
(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: 6.1] 6.1
该项根据实际GPU确定
3.编译.so文件
bazel build --config=opt --config=cuda --config=monolithic tensorflow:libtensorflow_cc.so
bazel build --config=opt --config=cuda --config=monolithic tensorflow:libtensorflow_framework.so
注意:如果不加–config=monlithic,编译出来的库会导致cv::imread()为空;这样导入库时只需导入libtensorflow_cc.so,不再需要libtensorflow_framework.so。如果需要重新编译,情先bazel clean清除,否则可能会出错。
4.编译其他依赖:
(1)在tensorflow/contrib/makefile下,执行./build_all_linux.sh文件,成功后会出现一个gen文件夹。
注意:若出现如下错误 :./autogen.sh: 37: ./autogen.sh: autoreconf: not found
,安装相应依赖即可 sudo apt-get install autoconf automake libtool
然后,重新执行命令:./build_all_linux.sh
若是最后又报错:./tensorflow/core/lib/io/zlib_outputbuffer.h:19:18: fatal error: zlib.h: No such file or directory
则需要安装 zlib1g-dev:sudo apt-get install zlib1g-dev
最后,再次重新执行命令:./build_all_linux.sh
(2)如需Eigen库,则进入tensorflow/contrib/makefile/downloads/eigen,执行:
mkdir build
cd build
cmake ..
make
sudo make install
安装完毕后,在usr/local/include目录下会出现eigen3文件夹。
5.整理库文件(lib)和头文件(include)
lib:
/xx/tensorflow/bazel-bin/tensorflow/libtensorflow_framework.so
/xx/tensorflow/bazel-bin/tensorflow/libtensorflow_cc.so
/xx/tensorflow/tensorflow/contrib/makefile/gen/protobuf/lib/libprotobuf.a
include:
/xx/tensorflow/tensorflow/contrib/makefile/gen/protobuf/include
/xx/tensorflow/tensorflow/contrib/makefile/gen/host_obj
/xx/tensorflow/tensorflow/contrib/makefile/downloads/nsync/public
/xx/tensorflow/tensorflow/contrib/makefile/downloads/eigen
/xx/tensorflow/tensorflow/contrib/makefile/downloads/absl
/xx/tensorflow/bazel-genfiles
/xx/tensorflow/tensorflow (实际主要用tensorflow下的cc和core)
/xx/tensorflow/third_party
/usr/local/include/eigen3
参考文献:
(1)、官网:https://tensorflow.google.cn/
(2)、查看tensorflow各个版本的CUDA以及Cudnn版本对应关系:https://blog.csdn.net/qq_27825451/article/details/89082978
(3)、查看 GPU Compute Capability:https://developer.nvidia.com/cuda-gpus
(4)、bazel安装:https://docs.bazel.build/versions/master/install-ubuntu.html
(5)、编译步骤说明:https://blog.csdn.net/gulingfengze/article/details/95658237
(6)、cc/g++安装及切换:https://www.cnblogs.com/yangwithtao/p/9091142.html