Ubuntu下tensorflow深度学习——环境篇(C++)

前文我们搭建了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中编译运行程序,结果如下图所示,表示测试成功。
在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Ubuntu 18.04,你可以使用以下步骤在C++中使用TensorFlow库: 1. 安装依赖项: ``` sudo apt-get update sudo apt-get install build-essential curl sudo apt-get install libcurl3-dev libfreetype6-dev libpng-dev libzmq3-dev pkg-config sudo apt-get install python3-dev python3-pip python3-wheel python3-setuptools ``` 2. 安装TensorFlow C++库: - 通过pip安装TensorFlow C++库: ``` pip3 install tensorflow ``` - 或者,从源代码构建TensorFlow C++库: - 克隆TensorFlow仓库: ``` git clone https://github.com/tensorflow/tensorflow.git cd tensorflow ``` - 配置构建选项并构建TensorFlow C++库: ``` ./configure bazel build --config=opt //tensorflow:libtensorflow_cc.so ``` - 在.bashrc或.bash_profile文件中设置LD_LIBRARY_PATH环境变量: ``` export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/tensorflow/bazel-bin/tensorflow/ ``` 3. 编写C++代码并链接TensorFlow库: - 创建一个C++文件,比如example.cpp,并添加以下代码: ```cpp #include <tensorflow/cc/client/client_session.h> #include <tensorflow/cc/ops/standard_ops.h> #include <tensorflow/core/framework/tensor.h> int main() { using namespace tensorflow; using namespace tensorflow::ops; Scope root = Scope::NewRootScope(); auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} }); auto b = Const(root, { {3.f, 5.f} }); auto product = MatMul(root.WithOpName("product"), A, b, MatMul::TransposeB(true)); std::vector<Tensor> outputs; ClientSession session(root); TF_CHECK_OK(session.Run({product}, &outputs)); for (const auto& tensor : outputs) { std::cout << tensor.matrix<float>() << std::endl; } return 0; } ``` - 编译C++代码: ``` g++ -std=c++11 -I/path/to/tensorflow -L/path/to/tensorflow/bazel-bin/tensorflow example.cpp -ltensorflow_cc -o example ``` - 运行生成的可执行文件: ``` ./example ``` 这样,你就可以在Ubuntu 18.04上使用C++编写和运行TensorFlow代码了。请确保将`/path/to/tensorflow`替换为你实际的TensorFlow安装路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值