【问题标题】:Compiling C++ code with TensorFlow without Bazel在没有 Bazel 的情况下使用 TensorFlow 编译 C++ 代码
【发布时间】:2017-07-26 20:20:52
【问题描述】:

我希望将 TensorFlow 及其 ANN 功能用于我的计算力学目的,其中我的大部分代码都使用 C++。是否可以在不使用 Bazel 的情况下在包含 TensorFlow .h 文件的情况下在 c++ 中编译?如果是这样,我真的很感激一个例子(到目前为止还没有在网上找到)。 谢谢

编辑:我做到了,但我无法跟随。让我举个例子,也许我们可以从那里开始。我正在使用 ubuntu 16.10、gcc (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005 和 Python 2.7.12+。我已经从源代码安装了 bazel,并且还克隆了 TF 存储库(~/Desktop/tensorflow)。以 (https://www.tensorflow.org/api_guides/cc/guide) 中的一个稍作修改的例子,我在 example.cc 中有:

#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"
#include <iostream>

int main() {
  using namespace tensorflow;
  using namespace tensorflow::ops;
  using namespace std;
  Scope root = Scope::NewRootScope();
  // Matrix A = [3 2; -1 0]
  auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f}});
  // Vector b = [3 5]
  auto b = Const(root, { {3.f, 5.f}});
  // v = Ab^T
  auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
  std::vector<Tensor> outputs;
  ClientSession session(root);
  // Run and fetch v
  TF_CHECK_OK(session.Run({v}, &outputs));
  // Expect outputs[0] == [19; -3]
  LOG(INFO) << outputs[0].matrix<float>();
  return 0;
  cout<<"compiled correctly!"<<endl;
}

它位于 ~/Desktop/tensorflow/tensorflow/cc/example。我的 BUILD 文件 - 也在 ~/Desktop/tensorflow/tensorflow/cc/example 中 - 内容为:

cc_binary(
    name = "example",
    srcs = ["example.cc"],
    deps = [
        "//tensorflow/cc:cc_ops",
        "//tensorflow/cc:client_session",
        "//tensorflow/core:tensorflow",
    ],
)

我尝试使用 ~/Desktop/tensorflow 编译:

bazel build tensorflow/cc/example/...

这是我得到的:

INFO: Found 1 target...
Target //tensorflow/cc/example:example up-to-date:
  bazel-bin/tensorflow/cc/example/example
INFO: Elapsed time: 0.381s, Critical Path: 0.00s

然后当我转到 ~/Desktop/tensorflow/bazel-bin/tensorflow/cc/example 并运行时:

./example 

我明白了:

2017-07-27 09:58:39.906578: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-27 09:58:39.906628: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-27 09:58:39.906636: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-07-27 09:58:39.906641: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-27 09:58:39.906646: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2017-07-27 09:58:39.907751: I tensorflow/cc/example/example.cc:22] 19
-3

任何帮助将不胜感激,因为我正试图解决这个问题。谢谢你的耐心。

【问题讨论】:

标签: c++ tensorflow compilation


【解决方案1】:

在不使用任何构建工具(如 Bazel 等)的情况下构建基于 tensorflow 框架的 c++ 代码的步骤。

  1. 通过以下链接从 github 克隆/下载 tensorflow。

    由于pip install tensorflow 只会为 python 安装 tensorflow。

    https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/makefile

  2. 要运行your_code.cc(您要构建的c++文件,对于c和c++使用.cc.cpp由于某种原因不起作用)

    将.cc文件和.pb文件放在tensorflow/tensorflow/tools/benchmark/中

    在 vim 编辑器的 Makefile 中编辑以下行 (tensorflow/tensorflow/contrib/makefile/Makefile)

    BENCHMARK_NAME := $(BINDIR)benchmark
    

    给出可执行文件的名称,而不是基准测试

    BENCHMARK_SRCS := \
    tensorflow/core/util/reporter.cc \
    tensorflow/tools/benchmark/benchmark_model.cc \
    tensorflow/tools/benchmark/benchmark_model_main.cc
    

    删除这些文件并在此处添加源代码 (.cc)。

  3. 接下来在 Makefile 中注释这一行:

    all: $(LIB_PATH) $(BENCHMARK_NAME)
    

    并添加以下行:

    all: $(BENCHMARK_NAME) 
    
  4. 接下来要运行 make 文件,转到根目录 (tesnroflow/) 并键入以下命令。

    make -f tensorflow/contrib/makefile/Makefile
    
  5. 如果您遇到任何错误,只需键入以下命令,

    export HOST_NSYNC_LIB=`/home/nivedita_s/Downloads/tensorflow-master/tensorflow/contrib/makefile/compile_nsync.sh`
    export TARGET_NSYNC_LIB="$HOST_NSYNC_LIB"
    
  6. 如果您已正确执行所有过程,则可执行文件将在下面提到的此文件夹中创建,

    tensorflow/tensorflow/contrib/makefile/gen/bin/
    
  7. 将以您为可执行文件指定的名称创建输出。 如果./executable_file 给出了任何库问题,请执行进一步的步骤(a 和 b)。

    • vim ~/.profile在末尾添加这一行,

      export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path_to_the_tensorflow/tensorflow/contrib/makefile/downloads/protobuf/src/.libs
      
    • 运行此命令

      source ~/.profile
      

现在您应该可以在该终端中运行您的程序了。

【讨论】:

  • 为什么选择 tensorflow/tensorflow/tools/benchmark/ 路径?为什么没有自定义路径?
【解决方案2】:

如果您不想过多地修改您的环境,有两种可能性。一种是使用 Floop 的 tensorflow_cc 项目编译 C++ API 并安装在您的系统上。另一种可能性是我的打包项目tensorflow_cpp_packaging 为 Tensorflow 的 C 和 C++ API 提供 Debian 包。这两个项目都使用 CMake(而不是 Bazel)来支持源文件的 C++ 编译。有时,我会编译最新的 Tensorflow 版本,它们可在 release 部分中找到,用于 64 位 CPU 和 Raspberry Pi。

从 Tensorflow 的角度来看,tensorflow_cc 的优势在于您可以根据需要构建 GPU 支持,而我的项目只能使用 CPU 进行推理。

如果您选择任何一个项目,我仍然可以推荐 my short tutorial 我写的如何在 Python 中冻结 Tensorflow 模型并使用 C 或 C++ API 加载并用于推理。

【讨论】:

    【解决方案3】:

    最简单的方法是简单地将所有必要的文件复制到本地项目:

    cd /git/github.com/tensorflow
    git clone git@github.com:tensorflow/tensorflow.git
    # build TensorFlow once (tensorflow:libtensorflow_cc.so, tensorflow:libtensorflow.so)
    cd project
    mkdir tensorflow
    cp /git/github.com/tensorflow/tensorflow/bazel-tensorflow/tensorflow/cc tensorflow/cc
    cp /git/github.com/tensorflow/tensorflow/bazel-genfiles/tensorflow/cc tensorflow
    

    有了这些文件,一个非常基本的 CMakeLists.txt 就可以完成这项工作:

    cmake_minimum_required( VERSION 2.8 )
    
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    set(TensorFlow_ABI 0)
    set(TensorFlow_INCLUDE_DIRS "${HOME}/.local/lib/python2.7/site-packages/tensorflow/include")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=${TensorFlow_ABI}")
    set(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=${TensorFlow_ABI}" )
    
    
    project( TFMyProject )
    
    include_directories(SYSTEM "${TensorFlow_INCLUDE_DIRS}/external/nsync/public")
    include_directories(SYSTEM ${TensorFlow_INCLUDE_DIRS})
    include_directories(SYSTEM ".")
    
    add_executable (example example.cc)
    TARGET_LINK_LIBRARIES(example -Wl,--allow-multiple-definition -Wl,--whole-archive "/git/github.com/tensorflow/bazel-bin/tensorflow/libtensorflow_cc.so" -Wl,--no-whole-archive)
    TARGET_LINK_LIBRARIES(example -Wl,--allow-multiple-definition -Wl,--whole-archive "/git/github.com/tensorflow/bazel-bin/tensorflow/libtensorflow_framework.so" -Wl,--no-whole-archive)
    

    甚至可以使用a python script 设置这些设置。

    【讨论】:

      猜你喜欢
      • 2013-07-16
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-05
      • 2022-12-04
      相关资源
      最近更新 更多