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 软件源

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

2. 编译 TensorFlow 库

  1. You have bazel 0.17.2 installed.
  2. Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3.5
  3. Found possible Python library paths:
  4. /usr/local/lib/python3.5/dist-packages
  5. /usr/lib/python3/dist-packages
  6. Please input the desired Python library path to use. Default is [/usr/local/lib/python3.5/dist-packages]
  7. Do you wish to build TensorFlow with Apache Ignite support? [Y/n]: n
  8. No Apache Ignite support will be enabled for TensorFlow.
  9. Do you wish to build TensorFlow with XLA JIT support? [Y/n]: n
  10. No XLA JIT support will be enabled for TensorFlow.
  11. Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: n
  12. No OpenCL SYCL support will be enabled for TensorFlow.
  13. Do you wish to build TensorFlow with ROCm support? [y/N]: n
  14. No ROCm support will be enabled for TensorFlow.
  15. Do you wish to build TensorFlow with CUDA support? [y/N]: y
  16. CUDA support will be enabled for TensorFlow.
  17. Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]:
  18. Please specify the location where CUDA 9.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
  19. Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]:
  20. Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
  21. Do you wish to build TensorFlow with TensorRT support? [y/N]: n
  22. No TensorRT support will be enabled for TensorFlow.
  23. Please specify the locally installed NCCL version you want to use. [Default is to use https://github.com/nvidia/nccl]:
  24. Please specify a list of comma-separated Cuda compute capabilities you want to build with.
  25. You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
  26. Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 6.1]:
  27. Do you want to use clang as CUDA compiler? [y/N]: n
  28. nvcc will be used as CUDA compiler.
  29. Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:
  30. Do you wish to build TensorFlow with MPI support? [y/N]: n
  31. No MPI support will be enabled for TensorFlow.
  32. Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:
  33. Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: n
  34. Not configuring the WORKSPACE for Android builds.
  35. Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details.
  36. --config=mkl # Build with MKL support.
  37. --config=monolithic # Config for mostly static monolithic build.
  38. --config=gdr # Build with GDR support.
  39. --config=verbs # Build with libverbs support.
  40. --config=ngraph # Build with Intel nGraph support.
  41. Configuration finished
  • 进入 tensorflow 目录进行编译,编译成功后,在 /bazel-bin/tensorflow 目录下会出现 libtensorflow_cc.so 文件
  1. C版本: bazel build :libtensorflow.so
  2. 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
  1. cmake_minimum_required(VERSION 3.8)
  2. project(Tensorflow_test)
  3. set(CMAKE_CXX_STANDARD 11)
  4. set(SOURCE_FILES main.cpp)
  5. include_directories(
  6. /media/lab/data/yongsen/tensorflow-master
  7. /media/lab/data/yongsen/tensorflow-master/tensorflow/bazel-genfiles
  8. /media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/gen/protobuf/include
  9. /media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/gen/host_obj
  10. /media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/gen/proto
  11. /media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/downloads/nsync/public
  12. /media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/downloads/eigen
  13. /media/lab/data/yongsen/tensorflow-master/bazel-out/local_linux-py3-opt/genfiles
  14. /media/lab/data/yongsen/tensorflow-master/tensorflow/contrib/makefile/downloads/absl
  15. )
  16. add_executable(Tensorflow_test ${SOURCE_FILES})
  17. target_link_libraries(Tensorflow_test
  18. /media/lab/data/yongsen/tensorflow-master/bazel-bin/tensorflow/libtensorflow_cc.so
  19. /media/lab/data/yongsen/tensorflow-master/bazel-bin/tensorflow/libtensorflow_framework.so
  20. )
  • 创建回话
  1. #include <tensorflow/core/platform/env.h>
  2. #include <tensorflow/core/public/session.h>
  3. #include <iostream>
  4. using namespace std;
  5. using namespace tensorflow;
  6. int main()
  7. {
  8. Session* session;
  9. Status status = NewSession(SessionOptions(), &session);
  10. if (!status.ok()) {
  11. cout << status.ToString() << "\n";
  12. return 1;
  13. }
  14. cout << "Session successfully created.\n";
  15. return 0;
  16. }
  • 查看 TensorFlow 版本
  1. #include <iostream>
  2. #include <tensorflow/c/c_api.h>
  3. int main() {
  4. std:: cout << "Hello from TensorFlow C library version" << TF_Version();
  5. return 0;
  6. }
  7. // 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. python 并发编程之多线程

    一.线程理论 1.什么是线程 ​ 多线程(即多个控制线程)的概念是,在一个进程中存在多个线程,多个线程共享该进程的地址空间,相当于一个车间内有多条流水线,都共用一个车间的资源. 所以,进程只是用来把资 ...

  2. MVC5 Attribute(特性)

    AuthorizeAttribute:一般用来判断权限 ActionFilterAttribute:方法执行前后动作 OutputCacheAttribute:输出缓存设置 注:我们创建名称的时候请带 ...

  3. html、css和js原生写一个模态弹出框,顺便解决父元素半透明子元素不透明效果

    模态框: html部分: <!-- 按钮 --> <button id="box" onclick="pop_box()">弹出框< ...

  4. Restframework的版本及分页

    1.版本 1.1基于url的get传参方式 1.创建django项目(起名我的是version),再创建一个app01应用 创建完成,通过python3 manage.py startapp api ...

  5. SQL里的concat() 以及group_concat() 函数的使用

    实例参考:https://blog.csdn.net/mary19920410/article/details/76545053 一 concat()函数 1.功能:将多个字符串连接成一个字符串. 2 ...

  6. 使用第三方《UITableView+FDTemplateLayoutCell》自动计算UITableViewCell高度(Masonry约束)

    直接上代码: 1:先自定义cell .h文件中 #import <UIKit/UIKit.h> #import "LBDNewMsgListModel.h" #impo ...

  7. Navicat for Mysql修改MySQL数据库密码,图文详解

    1.创建一个连接 2.打开连接 3.按照图示123依次点击 4.输入新密码 5.查看实现修改密码功能的SQL语句(此步骤非必须) 6.最关键的一步:点击保存 7.出现如下现象,恭喜你,修改密码成功! ...

  8. 揭开redux,react-redux的神秘面纱

    16年开始使用react-redux,迄今也已两年多.这时候再来阅读和读懂redux/react-redux源码,虽已没有当初的新鲜感,但依然觉得略有收获.把要点简单写下来,一方面供感兴趣的读者参考, ...

  9. linux运维、架构之路-shell编程(二)

    一.流程控制语句 1.if语句 ①if单分支:一个条件一个结果 1 2 3 4 if 条件   then      命令 fi ②if双分支:一个条件两个结果 1 2 3 4 5 6 if 条件    ...

  10. laravel构造函数跳转失败

    <?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Requests;us ...