我希望将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
任何帮助都会非常感激,因为我试图用手包围这个。谢谢你的耐心等待。
答案 0 :(得分:1)
在不使用Bazel等任何构建工具的情况下构建基于tensorflow框架的c ++代码的步骤。
从以下链接克隆/下载github的张量流。
由于pip install tensorflow
只会为python安装tensorflow。
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/makefile
要运行your_code.cc
(您要构建的c ++文件,请对c和c ++使用.cc
,.cpp
由于某种原因不起作用)
将.cc文件和.pb文件放在tensorflow / tensorflow / tools / benchmark /
中在vim编辑器中编辑Makefile中的以下行 (tensorflow / tensorflow /了contrib /生成文件/生成文件)
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
)。
下一步在Makefile中注释这一行:
all: $(LIB_PATH) $(BENCHMARK_NAME)
并添加以下行:
all: $(BENCHMARK_NAME)
接下来运行make文件,转到根目录(tesnroflow /)并输入以下命令。
make -f tensorflow/contrib/makefile/Makefile
如果您遇到任何错误,只需输入以下命令,
export HOST_NSYNC_LIB=`/home/nivedita_s/Downloads/tensorflow-master/tensorflow/contrib/makefile/compile_nsync.sh`
export TARGET_NSYNC_LIB="$HOST_NSYNC_LIB"
如果您已正确执行了所有过程,那么将在下面提到的此文件夹中创建可执行文件,
tensorflow/tensorflow/contrib/makefile/gen/bin/
将以您为可执行文件指定的名称创建输出。
如果./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
现在您应该能够在该终端中运行您的程序。
答案 1 :(得分:0)
最简单的方法是将所有必要的文件复制到本地项目中:
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设置这些设置。
答案 2 :(得分:0)
如果您不想过多地修改环境,则有两种可能。一种是使用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加载并用于推理的内容。