TensorFlow源码编译

5 篇文章 0 订阅
4 篇文章 0 订阅
1、版本tensorflow-1.15.5

https://github.com/tensorflow/tensorflow/releases/tag/v1.15.5

Note that this is the last patch release for the TensorFlow 1.x series.
本文将源代码下载在/home/svn路径下,并在编译完成后,将编译所得tensorflow环境,统一放置在/home/svn/tensorflow路径。

2、使用对应版本的python
cd /usr/bin/
ln -s python3.6 python
3、安装对应版本的bazel

https://github.com/bazelbuild/bazel/releases?page=6

wget https://github.com/bazelbuild/bazel/releases/download/0.26.1/bazel-0.26.1-installer-linux-x86_64.sh 
# 查看安装设置
./bazel-0.26.1-installer-linux-x86_64.sh --help
# 执行安装
./bazel-0.26.1-installer-linux-x86_64.sh

如果bazel版本不匹配,在编译TensorFlow时会报如下错误:
bazel版本与tensorflow版本不匹配

4、安装protobuf

https://github.com/protocolbuffers/protobuf/blob/master/src/README.md

wget https://github.com/protocolbuffers/protobuf/releases/download/v3.9.0/protobuf-all-3.9.0.tar.gz 
tar zxvf protobuf-all-3.9.0.tar.gz
cd protobuf-3.9.0
# generated the configure script
./autogen.sh
# 默认安装位置为/usr/local/,可以根据需要修改
./configure --prefix=/home/svn/third/third64/protobuf-3.9.0
make
make check
make install
# 查看是否安装成功
protoc --version

TensorFlow的protobuf版本兼容问题
详见:

https://blog.csdn.net/Li_suhuan/article/details/121041248

5、编译Tensorflow
wget https://github.com/tensorflow/tensorflow/archive/refs/tags/v1.15.5.tar.gz 
tar zxvf v1.15.5.tar.gz
cd tensorflow-1.15.5
./configure

编译设置选项如图:
tensorflow编译设置
执行编译:

# 有显卡
bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so
# 无显卡
bazel build --config=opt //tensorflow:libtensorflow_cc.so
6、编译第三方库
cd /home/svn/tensorflow-1.15.5/tensorflow/contrib/makefile
./build_all_linux.sh

GCC 8 编译时会报错,解决方法如下:

gcc8编译tensorflow报错解决
https://blog.felgo.com/cross-platform-development/machine-learning-add-image-classification-for-ios-and-android-with-qt-and-tensorflow

编译eigen3
cd /home/svn/tensorflow-1.15.5/tensorflow/contrib/makefile/downloads/eigen/
mkdir build
cd build
cmake ..
make
make install

编译出的eigen3文件夹在/usr/local/include目录下,可以移动到需要的地方

7、环境准备

在/home/svn/下建立tensorflow文件夹,并在其下建立include和lib
其中lib存放编译出的so

cd /home/svn/tensorflow/lib/
cp /home/svn/tensorflow-1.15.5/bazel-genfiles/tensorflow/libtensorflow_cc.so ./
cp /home/svn/tensorflow-1.15.5/bazel-genfiles/tensorflow/libtensorflow_framework.so.1 ./
mv libtensorflow_framework.so.1 libtensorflow_framework.so

include存放所需的头文件

cp -r /home/svn/tensorflow-1.15.5/tensorflow /home/svn/tensorflow/include/
cp -r /home/svn/tensorflow-1.15.5/bazel-genfiles/tensorflow/* /home/svn/tensorflow/include/tensorflow/
# so文件放在lib中即可,这里不需要
rm /home/svn/tensorflow/include/tensorflow/libtensorflow_*
# 第三方库
mkdir /home/svn/tensorflow/include/tensorflow/third_party
cp -r /home/svn/tensorflow-1.15.5/tensorflow/contrib/makefile/downloads/eigen /home/svn/tensorflow/include/tensorflow/third_party/eigen3
# 编译的时候缺啥补啥
cp /home/svn/tensorflow-1.15.5/bazel-tensorflow-1.15.5/third_party/eigen3/unsupported/Eigen/CXX11/FixedPoint  /home/svn/tensorflow/include/tensorflow/third_party/eigen3/unsupported/Eigen/CXX11/
cp -r /usr/local/include/eigen3/unsupported/Eigen/CXX11/* /home/svn/tensorflow/include/tensorflow/third_party/eigen3/unsupported/Eigen/CXX11/
cp -r /home/svn/tensorflow-1.15.5/bazel-tensorflow-1.15.5/third_party/eigen3/unsupported/Eigen/CXX11/src /home/svn/tensorflow/include/tensorflow/third_party/eigen3/unsupported/Eigen/CXX11/

https://www.cnblogs.com/dzzy/p/13493876.html

至此安装完成,使用时添加相应的include和lib路径即可

# tensorflow
INCLUDE   += -I/home/svn/tensorflow/include/
INCLUDE   += -I/home/svn/tensorflow/include/tensorflow/third_party/eigen3/
INCLUDE   += -I/home/svn/tensorflow/include/tensorflow/
INCLUDE   += -I/home/svn/tensorflow/include/tensorflow/contrib/makefile/downloads/absl

LIB       += -L/home/svn/tensorflow/lib/ -ltensorflow_framework -ltensorflow_cc -lz
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值