c++ - 在没有 Bazel 的情况下使用 TensorFlow 编译 C++ 代码

标签 c++ tensorflow compilation

我希望针对我的计算力学目的调整 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。我的构建文件 - 也在 ~/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

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

最佳答案

在不使用 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. Next 在 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
      

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

关于c++ - 在没有 Bazel 的情况下使用 TensorFlow 编译 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45336683/

相关文章:

c++ - 继承的友元

c++ - 什么是关于句柄的不透明标识符?

object - “张量”对象没有属性 'assign_add'

python - 在 Python 中变量后面使用括号有什么作用?

ios - ios如何将Objective-C转换为C语言

c++ - 如何测试分配器是否使用 std::allocate 进行内存分配?

c++ - 你如何将 LuaPlus 包含到你的项目中?

tensorflow - TensorFlow/Keras 的 fit() 函数的 class_weight 参数如何工作?

java - 使用java代码以编程方式创建android应用程序

php - 使用 GD 为 iPhone OS 4.1 编译 PHP