Ubuntu 16.04 Tensorflow 1.4 C++接口 (CUDA 8.0 CUDNN 6.0 OPENCV 4.0.1)

导语:经过了几个星期的折腾,我终于完成了ubuntu上的tensorflow 1.4 C++ 接口的配置,真是一部血泪史,特此记录一下。

结尾放了一些资源的连接

下期预告,yoloV3+deepsort 的ubuntu的移植

 

硬件

我的电脑配置:

OS: Ubuntu 16.04 LTS

GPU: GTX1080

NVIDIA 驱动: 410.104

 

前期准备

装英伟达显卡请参考:

https://blog.csdn.net/xunan003/article/details/81665835

 

CUDA 8.0 CUDNN 6.0 参考:

https://blog.csdn.net/qq_40806289/article/details/90643603

 

OPENCV 4.0,1参考:

https://www.cnblogs.com/laizhenghong2012/p/11329916.html

https://blog.csdn.net/PecoHe/article/details/90581526

 

notes:

1.解决ipcv问题:https://www.cnblogs.com/yongy1030/p/10293178.html

2.解决face_landmark_model.dat问题: https://www.jianshu.com/p/162f2cdf4f88

3.解决 vgg_generated_48.i  等问题 : 打开 opencv-4.0.1/build/CMakeDownloadLog.txt 文件,下载对应文件

放置到 opencv_contrib-4.0.1/modules/xfeatures2d/src/ 下

注意: boostdesc_bgm.i , vgg_generated_48.i 等 必须要自己从自己的opencv-4.0.1/build/CMakeDownloadLog.txt下载链接上下载,网上上传的可能没有用,因为它会校验kmd5码。

 

重要:一定不要先安装protobuf 因为tensorflow 里也带了这个,如果你装了,后面即使你安装成功了,编译自己的代码,生成可执行文件了,也运行不了

会出现:

This program was compiled against version 2.6.1 of the Protocol Buffer runtime library, which is not compatible with the installed version (3.6.1).  Contact the program author for an update.  If you compiled the program yourself, make sure that your headers are from the same version of Protocol Buffers as your link-time library.

处理起来比较复杂

 

编译tensorflow 1.4 C++ 接口

参考:

 

https://blog.csdn.net/gulingfengze/article/details/95658237

 

https://blog.csdn.net/flyfish1986/article/details/89406211

 

正式开始

  1. 一些必要的依赖

如果机器没有安装Python,可以使用如下命令安装

 

sudo apt install python-dev python-pip

或者

sudo apt install python3-dev python3-pip

 

安装 TensorFlow pip 软件包依赖项

pip install -U --user pip six numpy wheel mock

pip install -U --user keras_applications==1.0.6 --no-deps

pip install -U --user keras_preprocessing==1.0.5 --no-deps

2安装bazel

注意:一定要下载合适的版本,否则会出现各种莫名其妙的错误!

版本对应表

 

推荐使用二进制安装 (可以安装任意本吧,这里以0.5.4版本安装为例)

1) 安装需要的包

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

 

2)下载地址(https://github.com/bazelbuild/bazel/releases/tag/0.5.4)

3)执行安装

chmod +x bazel-0.5.4-installer-linux-x86_64.sh

./bazel-0.5.4-installer-linux-x86_64.sh –user

4)设置环境变化

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

 

安装完成后可以查看一下版本号

bazel version

输出

Build label: 0.5.4

Buildtarget:bazel-out/local-fastbuild/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar

Build time: Fri Aug 25 10:00:00 2017 (1503655200)

Build timestamp: 1503655200

Build timestamp as int: 1503655200

 

3.tensorflow安装

1)下载源码

git clone https://github.com/tensorflow/tensorflow.git

cd tensorflow

2) 切换到我们要编译的分支

~/code/tensorflow$ git checkout r1.4

输出

分支 ‘r1.4’ 设置为跟踪来自 ‘origin’ 的远程分支 ‘r1.4’。

切换到一个新分支 ‘r1.4’

3)配置编译系统

-1) 因为我在Ubuntu中安装了python2.7和python3.6。默认使用的是Python2.7版本,我期望默认使用Python3.6版本

whereis python

sudo rm /usr/bin/python

sudo ln -s /usr/bin/python3.6 /usr/bin/p

-2) 执行命令./configure

Found possible Python library paths:

/usr/lib/python3/dist-packages

/usr/local/lib/python3.6/dist-packages

Please input the desired Python library path to use. Default is [/usr/lib/python3/dist-packages]

因为我当时没截图,后面的就

如果你想安装tensorflow-cpu版,直接回车,遇到选择需要输入y或者n时,都选择n;

如果想安装GPU版,只需要在

Do you wish to build TensorFlow with CUDA support? [y/N]: Y

CUDA support will be enabled for TensorFlow.

输入Y,其他还是输入n或者回车。

-3)编译libtensorflow_cc.so 以及 libtensorflow_framework.so

编译命令:

bazel build --config=opt --config=cuda tensorflow:libtensorflow_cc.so

bazel build --config=opt --config=cuda tensorflow:libtensorflow_framework.so

 

注意:网上有人说不加--config=monolithic,编译出来的库会导致cv::imread()为空,我第一次是加了--config=monolithic的,但是后面编译自己的代码生成可执行文件后,运行会出现

Duplicate registration of device factory for type GPU with the same priority210 错误

 

-4)编译其他依赖

进入 tensorflow/contrib/makefile 目录下,运行./build_all_linux.sh

会自动下载所缺的库

 

这就没了?

怎么会没了,好戏才开始...(如果你能直接完成,那恭喜你,还真就没了)

 

问题1.如果出现tar, zip异常的结尾,那就是你的网不好,文件没有完整下载下来。

复制上面的地址然后用浏览器下载下来,并修改

/tensorflow/contrib/makefile/download_dependencies.sh 的download_and_extract()函数

和69-75行

并注释掉/tensorflow/contrib/makefile/build_all_linux.sh

第三十行

 

问题2. 在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

链接:https://pan.baidu.com/s/1ZBA-u7YIOqIoFLJy0AsnYg  提取码:xhfk

 

发布了4 篇原创文章 · 获赞 4 · 访问量 3676

猜你喜欢

转载自blog.csdn.net/tdh2017/article/details/105139097