我真的很渴望开始使用谷歌在c++中的新Tensorflow库。网站和文档在如何构建项目的c++ API方面真的不清楚,我不知道从哪里开始。
是否有更有经验的人可以通过发现和分享使用tensorflow的c++ API的指南来帮助您?
我真的很渴望开始使用谷歌在c++中的新Tensorflow库。网站和文档在如何构建项目的c++ API方面真的不清楚,我不知道从哪里开始。
是否有更有经验的人可以通过发现和分享使用tensorflow的c++ API的指南来帮助您?
当前回答
如果你不介意使用CMake,还有tensorflow_cc项目可以为你构建和安装TF c++ API,以及方便的CMake目标,你可以链接到它。项目README包含一个示例和Dockerfiles,您可以轻松地遵循。
其他回答
如果你不想自己构建Tensorflow,而且你的操作系统是Debian或Ubuntu,你可以下载带有Tensorflow C/ c++库的预构建包。这个发行版可以用于CPU的C/ c++推理,GPU支持不包括在内:
https://github.com/kecsap/tensorflow_cpp_packaging/releases
这里有一些关于如何在Tensorflow (TFLearn)中冻结检查点的说明,并使用C/ c++ API加载这个模型进行推理:
https://github.com/kecsap/tensorflow_cpp_packaging/blob/master/README.md
注意:我是这个Github项目的开发者。
为了补充@mrry的帖子,我整理了一个教程,解释如何用c++ API加载TensorFlow图。它非常简单,应该帮助您理解所有的部分是如何组合在一起的。这是它的核心:
要求:
巴泽尔安装 克隆TensorFlow回购
文件夹结构:
tensorflow tensorflow / | | /项目名称 Tensorflow / Tensorflow /|项目名称|/|项目名称|。Cc(例如https://gist.github.com/jimfleming/4202e529042c401b17b7) tensorflow / tensorflow / | |项目名称/构建
构建:
cc_binary(
name = "<project name>",
srcs = ["<project name>.cc"],
deps = [
"//tensorflow/core:tensorflow",
]
)
有两点需要注意,但可能有变通办法:
现在,构建需要在TensorFlow回购中进行。 编译后的二进制文件很大(103MB)。
https://medium.com/@jimfleming/loading-a-tensorflow-graph-with-the-c-api-4caaff88463f
首先,您应该按照这里的说明从Github下载源代码(您需要Bazel和最新版本的GCC)。
The C++ API (and the backend of the system) is in tensorflow/core. Right now, only the C++ Session interface, and the C API are being supported. You can use either of these to execute TensorFlow graphs that have been built using the Python API and serialized to a GraphDef protocol buffer. There is also an experimental feature for building graphs in C++, but this is currently not quite as full-featured as the Python API (e.g. no support for auto-differentiation at present). You can see an example program that builds a small graph in C++ here.
c++ API的第二部分是用于添加新的OpKernel的API,这是一个包含CPU和GPU的数值内核实现的类。在tensorflow/core/kernels中有许多如何构建这些操作的示例,以及在c++中添加新操作的教程。
Tensorflow本身只提供了关于c++ api的非常基本的示例。 这是一个很好的资源,包括数据集的例子,rnn, lstm, cnn等 Tensorflow c++的例子
我发现使用Tensorflow c++ API的一个替代方案是使用cppflow。
它是一个围绕Tensorflow C API的轻量级c++包装器。你得到非常小的可执行文件,它链接到libtensorflow。已经编译好的文件。还有一些使用的例子,您使用CMAKE而不是Bazel。