TensorFlow 的 Python 接口由于其方便性和实用性而大受欢迎,但实际应用中我们可能还需要其它编程语言的接口,本文将介绍如何编译 TensorFlow 的 C/C++ 接口。

安装环境:

Ubuntu 16.04

Python 3.5

CUDA 9.0

cuDNN 7

Bazel 0.17.2

TensorFlow 1.11.0

1. 安装 Bazel

  • 安装 JDK sudo apt-get install openjdk-8-jdk

  • 添加 Bazel 软件源

echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -

2. 编译 TensorFlow 库

You have bazel 0.17.2 installed.
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3.5 Found possible Python library paths:
/usr/local/lib/python3.5/dist-packages
/usr/lib/python3/dist-packages
Please input the desired Python library path to use. Default is [/usr/local/lib/python3.5/dist-packages] Do you wish to build TensorFlow with Apache Ignite support? [Y/n]: n
No Apache Ignite support will be enabled for TensorFlow. Do you wish to build TensorFlow with XLA JIT support? [Y/n]: n
No XLA JIT support will be enabled for TensorFlow. 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. Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]: Please specify the location where CUDA 9.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]: Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: Do you wish to build TensorFlow with TensorRT support? [y/N]: n
No TensorRT support will be enabled for TensorFlow. Please specify the locally installed NCCL version you want to use. [Default is to use https://github.com/nvidia/nccl]: 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]: 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]: Do you wish to build TensorFlow with MPI support? [y/N]: n
No MPI support will be enabled for TensorFlow. Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: 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=gdr # Build with GDR support.
--config=verbs # Build with libverbs support.
--config=ngraph # Build with Intel nGraph support.
Configuration finished
  • 进入 tensorflow 目录进行编译,编译成功后,在 /bazel-bin/tensorflow 目录下会出现 libtensorflow_cc.so 文件
C版本: bazel build :libtensorflow.so
C++版本: bazel build :libtensorflow_cc.so

3. 编译其他依赖

  • 进入 tensorflow/contrib/makefile 目录下,运行./build_all_linux.sh,成功后会出现一个gen文件夹

  • 若出现如下错误 /autogen.sh: 4: autoreconf: not found ,安装相应依赖即可 sudo apt-get install autoconf automake libtool

4. 测试

  • Cmaklist.txt
cmake_minimum_required(VERSION 3.8)
project(Tensorflow_test) set(CMAKE_CXX_STANDARD 11) set(SOURCE_FILES main.cpp) include_directories(
/media/lab/data/yongsen/tensorflow-master
/media/lab/data/yongsen/tensorflow-master/tensorflow/bazel-genfiles
/media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/gen/protobuf/include
/media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/gen/host_obj
/media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/gen/proto
/media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/downloads/nsync/public
/media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/downloads/eigen
/media/lab/data/yongsen/tensorflow-master/bazel-out/local_linux-py3-opt/genfiles
/media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/downloads/absl
) add_executable(Tensorflow_test ${SOURCE_FILES}) target_link_libraries(Tensorflow_test
/media/lab/data/yongsen/tensorflow-master/bazel-bin/tensorflow/libtensorflow_cc.so
/media/lab/data/yongsen/tensorflow-master/bazel-bin/tensorflow/libtensorflow_framework.so
)
  • 创建回话
#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";
return 0;
}
  • 查看 TensorFlow 版本
#include <iostream>
#include <tensorflow/c/c_api.h> int main() {
std:: cout << "Hello from TensorFlow C library version" << TF_Version();
return 0;
} // Hello from TensorFlow C library version1.11.0-rc1
  • 若提示缺少某些头文件则在 tensorflow 根目录下搜索具体路径,然后添加到 Cmakelist 里面即可。

获取更多精彩,请关注「seniusen」!

编译 TensorFlow 的 C/C++ 接口的更多相关文章

  1. Ubuntu16.04编译tensorflow的C++接口

    原文:https://www.bearoom.xyz/2018/09/27/ubuntu1604buildtf4cpp/ 之前有一篇介绍到在windows下利用VS2015编译tensorflow的C ...

  2. ubuntu14 编译tensorflow C++ 接口

    tensorflow1.11 bazel 0.15.2 protobuf 3.6.0 eigen 3.3.5 wget -t 0 -c https://github.com/eigenteam/eig ...

  3. 编译TensorFlow源码

      编译TensorFlow源码 参考: https://www.tensorflow.org/install/install_sources https://github.com/tensorflo ...

  4. 在Windows*上编译Tensorflow教程

    背景介绍 最简单的 Tensorflow 的安装方法是在 pip 一键式安装官方预编译好的包 pip install tensorflow 通常这种预编译的包的编译参数选择是为了最大兼容性而不是为了最 ...

  5. 编译TensorFlow CPU指令集优化版

    编译TensorFlow CPU指令集优化版 如题,CPU指令集优化版,说的是针对某种特定的CPU型号进行过优化的版本.通常官方给的版本是没有针对特定CPU进行过优化的,有网友称,优化过的版本相比优化 ...

  6. CentOS 6 编译 TensorFlow for Java 以及 Maven Pom

    我们的系统环境 CentOS 6.5, JDK 1.8 更新yum源 $ yum update 安装 Python 2.7 $ yum install python27 python27-numpy ...

  7. YOLOv4: Darknet 如何于 Ubuntu 编译,及使用 Python 接口

    本文将介绍 YOLOv4 官方 Darknet 实现,如何于 Ubuntu 18.04 编译,及使用 Python 接口. 主要内容有: 准备基础环境: Nvidia Driver, CUDA, cu ...

  8. win10编译tensorflow C++接口

    ​原文地址:https://www.bearoom.xyz/2018/08/28/win10-build-tf-cc/ 首先,我觉得这是一个比较DT的活,因为,tensorflow支持最好的编程语言应 ...

  9. caffe 在window下编译(windows7, cuda8.0,matlab接口编译)

    1. 环境:Windows7,Cuda8.0,显卡GTX1080,Matlab2016a,VS2013 (ps:老板说服务器要装windows系统,没办法,又要折腾一番,在VS下编译好像在cuda8. ...

随机推荐

  1. EF6 AddOrUpdate之后,数据没有改变而是新增了一条数据解决办法

    EF:修改不是查询出来的对象dbContext.Web_User.AddOrUpdate(user);dbContext.SaveChanges(); 上面的写法有时候可能不起作用,而且把这条数据重复 ...

  2. Anaconda的使用—Spyder常用快捷键

    Ctrl + 1: 注释/反注释 Ctrl + 4/5: 块注释/块反注释 Ctrl + L: 跳转到行号 Tab/Shift + Tab: 代码缩进/反缩进 Ctrl +I:显示帮助

  3. shell脚本中 [-eq] [-ne] [-gt] [-lt] [ge] [le]

    -eq //等于 -ne //不等于 -gt //大于 (greater ) -lt //小于 (less) -ge //大于等于 -le //小于等于 在linux 中 命令执行状态:0 为真,其他 ...

  4. hibernate中配置单向多对一关联,和双向一对多,双向多对多

    什么是一对多,多对一? 一对多,比如你去找一个父亲的所有孩子,孩子可能有两个,三个甚至四个孩子. 这就是一对多 父亲是1 孩子是多 多对一,比如你到了两个孩子,它们都是有一个共同的父亲. 此时孩子就是 ...

  5. 《linux设备驱动开发详解》笔记——15 linux i2c驱动

    结合实际代码和书中描述,可能跟书上有一定出入.本文后续芯片相关代码参考ZYNQ. 15.1 总体结构 如下图,i2c驱动分为如下几个重要模块 核心层core,完成i2c总线.设备.驱动模型,对用户提供 ...

  6. linux文件操作篇 (四) 目录操作

    #include <sys/stat.h>#include <unistd.h>#include <dirent.h> //创建文件夹 路径 掩码 int mkdi ...

  7. IO复用——select系统调用

    1.select函数 此函数用于在一段时间内,监听用户感兴趣的文件描述符上的可读.可写和异常等事件. #include<sys/select.h> int select(int nfds, ...

  8. abap<itab>refresh,clear,free

    在ABAP开发过程中,clear,refresh,free都有用来清空内表的作用,但用法还是有区别的. clear itab,清空内表行以及工作区,但保存内存区. clear itab[],清空内表行 ...

  9. 【转】让Moodle支持多个域名

    默认情况下,moodle仅能绑定一个域名.但是由于学校网络分内网和外网,总希望如果是外网访问的,用外网的域名,用内网访问的,就转到内网的ip.这样访问的速度会更快一些,也减低对防火墙的压力.尤其是当外 ...

  10. 基于Ubuntu Server 16.04 LTS版本安装和部署Django之(一):安装Python3-pip和Django

    近期开始学习基于Linux平台的Django开发,想配置一台可以发布的服务器,经过近一个月的努力,终于掌握了基于Apache和mod-wsgi插件的部署模式,自己也写了一个教程,一是让自己有个记录,二 ...