references
https://blog.csdn.net/asialee_bird/article/details/100990483
https://www.jianshu.com/p/7f6b8a290f81
Foreword
Because of the speed server can not be used to download tensorflow source, that is, through the official website method
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
Download the source code of tensorflow default master code inside, you want to compile tensorflow other version, need to use
git checkout branch_name # r1.9, r1.10, etc.
Then think of is downloaded directly fixed tensorflow version, and then spread to the server to be compiled. Where the fixed version tensorflow Download , after some tensorflow version download is complete only a few megabytes B, like this can not be the source file is compiled (missing files), the following tensorflow1.13 is over 30 trillion, can be successfully compiled.
Server environment
Said that under the operating environment,
Ubuntu18.04,
python3.7
CUDA 10.0,
cuDNN 7.6.3
View cuda and cudnn version
nvcc -V
cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
1, software download
(1) tensorflow-1.13.1 source ( tensorflow 1.13.1 )
(2)bazel-0.19.2-installer-linux-x86_64.sh(bazel-0.19.2-installer-linux-x86_64.sh)
(4 And) protobuf-3612targz
These files to Baidu Cloud Inside
Link: https: //pan.baidu.com/s/1wIX9u_mNVgDo8RydT7sNIA
extraction code: yt5x
During also tried a lot of things, here are some of the downloaded file
Link: https: //pan.baidu.com/s/1hOiY_ClmPAnT1s1totjg8Q
extraction code: ozc1
2, Bazel compiler installation
bazel compiler and tensorflow version must match, or not compile, can refer to the official website for information
bazel compiler I installed directly to my own user directory: lower (such as / home / zyt5 /), and is not installed in the root directory
chmod +x bazel-0.19.2-installer-linux-x86_64.sh
./bazel-0.19.2-installer-linux-x86_64.sh --user
After the installation is complete, you need to add environment variables inside bashrc
vim ~/.bashrc
Then type
export PATH="$PATH:$HOME/bin"
After saving it to take effect, enter the command
source ~/.bashrc
You can view bazel version
bazel version
3, installation protobuf
Installation Protobuf It is important, to correspond to the compiled version of tensorflow, this can refer to the official website
unzip files
tar zxvf protobuf-3.6.1.2.tar.gz
Then compiled to create a new folder
mkdir protobuf_bin
Once you have downloaded into the protobuf-3.6.1.2 directory, enter the following command to install
cd protobuf-3.6.1.2/
./autogen.sh
If the
./autogen.sh: 32: ./autogen.sh: autoreconf: not found
Ask the administrator to perform
sudo apt-get install autoconf automake libtool
Executed in protobuf-3.6.1.2 directory
./configure --prefix=$HOME/protobuf_bin (自己新建的目录)
make -j4
make install
Add the environment variable (according to their own installation path) after a good compiler
vim ~/.bashrc
export PATH=$PATH:$HOME/tools/protobuf_bin/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/tools/protobuf_bin/lib
export CPLUS_INCLUDE_PATH="/home/zhouxd/tools/protobuf_bin/include"
source ~/.bashrc
View version to verify installation
protoc --version
If you want to uninstall protobuf
cd protobuf-3.6.1.2/
make uninstall
4, tensorflow source compiler
tar zxvf tensorflow-1.13.1.tar.gz
cd tensorflow-1.13.1/
./configure
Accordance with the following picture of the way, as long as CUDA support select Y, the other basically default and N, where the default is 10.0, if it is not written on the corresponding version in the back, I cudnn is 7.6.3, the default is 7, direct default is OK.
After the setup is complete you need to compile the
// 有显卡
bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so
// 无显卡
bazel build --config=opt //tensorflow:libtensorflow_cc.so
Note: Some bloggers say there is no use --config = monolithic, will not use opencv, but I still can not add
This will take some time to complete the installation, it may be error during compilation because bazel tensorflow source also need to rely on other items, because of the speed can not be downloaded completely it will complain, no solution, recompile (ie above bazel instructions) until it succeeds. (Tried these dependencies during the download is complete in the desired position, but still will be downloaded, or need to be recompiled)
ERROR: error loading package '': Encountered error while reading extension file 'container/container.bzl': no such package '@io_bazel_rules_docker//container': java.io.IOException: Error downloading [https://github.com/bazelbuild/rules_docker/archive/a9bb1dab84cdf46e34d1b34b53a17bda129b5eba.tar.gz] to /home/zyt5/.cache/bazel/_bazel_zyt5/957c869dd334b8c29712fd21f3584066/external/io_bazel_rules_docker/a9bb1dab84cdf46e34d1b34b53a17bda129b5eba.tar.gz: Tried to reconnect at offset 403,461 but server didn't support it
After successful compilation, there will be several files bazel-xxx under tensorflow-1.13.1 directory, there will be libtensorflow_cc.so and libtensorflow_framework.so dynamic library files in tensorflow-1.13.1 / bazel-bin / tensorflow file
Compile and install other dependencies
cd ~/tensorflow-1.13.1/tensorflow/contrib/makefile/
./build_all_linux.sh
Note: build_all_linux.sh This command is included ./download_dependencies.sh, but alone execute this command is not successful ./download_dependencies.sh
After the successful implementation, in tensorflow-1.13.1 / tensorflow / contrib / makefile directory: downloads folder to store header files and static libraries rely on a third party, such as absl cub double_conversion eigen fft2d gemmlowp googletest nsync re2. These third-party libraries that can install yourself, you can refer to: https://www.jianshu.com/p/7f6b8a290f81
Eigen3 need to be compiled in order to use
First open the file downloads on a folder-by-step, there will be an eigen folders, folder opens into the eigen sequentially input terminal instructions for:
mkdir build
cd build
cmake ..
make
sudo make install
After installation, under usr / local / include directory will appear eigen3 folder . The first installed in the root directory of the best to install their own user directory
Description: Eigen is a high-level C ++ library, to support efficient linear algebra, matrix and vector operations, numerical analysis and related algorithms; Protocol Buffers (referred Protobuf) is a Google open source cross-language, cross-platform, expansion good tool sequences compared to XML and JSON popular encoding format, this data structure requires protoc language compiler.
5, test tensorflow C ++ API
In his catalog cpptest create a new folder, the folder directory is as follows:
For main.cpp file, which reads as follows:
#include <tensorflow/core/platform/env.h>
#include <tensorflow/core/public/session.h>
#include <iostream>
using namespace std;
using namespace tensorflow;
int main()
{
Session* session;
Status status = NewSession(SessionOptions(), &session);
if (!status.ok()) {
cout << status.ToString() << "\n";
return 1;
}
cout << "Session successfully created.\n";
}
CMakeLists.txt which files are as follows
#指定 cmake 的最小版本
cmake_minimum_required(VERSION 3.10.2)
#项目名称/工程名
project(cpptest)
#设置c++编译器
set(CMAKE_CXX_STANDARD 14)
#aux_source_directory(./src DIR_SRCS) # 搜索当前目录下的所有.cpp文件
#设置TENSORFLOW_DIR变量,变量内容为安装的tensorflow文件夹路径
set(TENSORFLOW_DIR /home/zyt5/tensorflow-1.13.1)
include_directories(${TENSORFLOW_DIR}
${TENSORFLOW_DIR}/bazel-genfiles
${TENSORFLOW_DIR}/tensorflow/contrib/makefile/downloads/absl
${TENSORFLOW_DIR}/tensorflow/contrib/makefile/downloads/nsync/public
/usr/local/include/eigen3
/home/zyt5/protobuf_bin/include)
link_directories(${TENSORFLOW_DIR} /home/zyt5/tensorflow-1.13.1/bazel-bin/tensorflow) #动态链接库目录
#add_executable(cpptest ${DIR_SRCS}) ## 生成可执行文件
add_executable(cpptest main)
#添加可执行文件所需要的库,连接libtensorflow_cc.so和libtensorflow_framework库,链接动态链接库
target_link_libraries(cpptest tensorflow_cc tensorflow_framework)
Compile and run
mkdir build #创建build文件,是为了将编译程序放到build文件中
cd build
cmake .. #使用cmake构建生成make文件
make #使用make编译
./cpptest #运行可执行文件
Results are as follows, indicating the end of the compilation tensorflow