将 Keras 模型转换为 C++

Convert Keras model to C++

我正在使用 Keras(与 Theano)来训练我的 CNN 模型。有谁知道如何在我的 C++ 应用程序中使用它?有没有人尝试过类似的东西?我想写一些 python 代码来生成带有网络功能的 C++ 代码 - 有什么建议吗?

我发现了一个类似的问题here how to use Tensorflow Keras model in C++ 但没有答案。

最简单的方法可能是对 Python 脚本进行系统调用,将预测写入二进制文件或 HDF5 file, which can be read in from C++. You can also directly integrate Python into C++.

如果您需要轻松部署和分发它,您可以查看像 Anaconda, but your best bet may be to avoid Keras and use the C++ interface to Caffe or Tensorflow. I wouldn't recommend Tensorflow since using it from C++ isn't standard; see this discussion. Caffe is arguably the second most-popular deep learning library 这样的自包含 Python 安装,这样就不会出错。

为了回答我自己的问题并找到解决方案 - 我编写了一个名为 keras2cpp 的普通 C++ 解决方案(其代码可在 github 上找到)。

在此解决方案中,您存储网络架构(在 json 中)和权重(在 hdf5 中)。然后您可以使用提供的脚本将网络转储到纯文本文件。您可以在纯 C++ 代码中将获得的文本文件与网络一起使用。 python 库或 hdf5 没有依赖性。它应该适用于 theano 和 tensorflow 后端。

我有类似的需求——我想将 Keras 模型嵌入到 C++ 应用程序中——并决定编写我自己的库:Kerasify

Kerasify 的设计目标:

  • 与 Keras 使用 Theano 后端生成的图像处理顺序网络的兼容性。 (如果您切换矩阵 col/row 排序,则可以与 Tensorflow 一起使用)。
  • 无外部依赖,标准库,C++11 特性正常。
  • 模型以二进制格式存储在磁盘上,可以快速读取。
  • 模型以连续块的形式存储在内存中以获得更好的缓存性能。
  • 不抛出异常,returns 仅在出错时返回 bool。
  • CPU,无 GPU

github link 处的示例代码、单元测试等。它并不完全完整,它只支持我正在使用的 Keras 函数的一小部分,但它应该可以通过一些努力进行扩展。

如果您的keras模型是使用tensorflow后端训练的,您可以按照以下代码将keras模型保存为tensorflow模型: https://github.com/amir-abdi/keras_to_tensorflow

这是代码的较短版本:

from keras import backend as K
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io

weight_file_path = 'path to your keras model'
net_model = load_model(weight_file_path)
sess = K.get_session()

constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), 'name of the output tensor')
graph_io.write_graph(constant_graph, 'output_folder_path', 'output.pb', as_text=False)
print('saved the constant graph (ready for inference) at: ', osp.join('output_folder_path', 'output.pb'))

这里找到的解决方案非常好,但是如果您的模型有一些这些库不支持的不同类型的层,我建议您执行以下操作:

  • 正在将 Keras 模型转换为张量流模型。
  • 冻结模型并使用 tensorflow 提供的 Tranform 图形工具(您必须使用 bazel 从源代码构建它)
  • 编译 C++ API tensorflow 库以在您的项目中使用它。
  • 将 C++ API tensorflow 库和 link 库用于您的项目。

如果您想使用不同于 bazel 的编译器(例如 g++),您可以遵循这个很棒的教程:

http://tuatini.me/building-tensorflow-as-a-standalone-project/

我发现自己处于类似情况,但不仅需要支持 C++ 中顺序 Keras 模型的前向传播,还需要支持使用 functional API.

构建的更复杂模型

所以我写了一个名为 frugally-deep 的新库。您可以在 GitHub 上找到它,它是根据 MIT 许可证发布的:https://github.com/Dobiasd/frugally-deep

除了支持许多常见层类型外,它还可以在单​​个 CPU 上跟上(有时甚至击败)TensorFlow 的性能。您可以在 repo.

中找到一些常见模型的最新基准测试结果

通过自动测试 frugally-deep 保证在 C++ 中使用它的模型的输出与 运行 在 Python 中使用 Keras 完全相同。

你可以试试这个 https://github.com/gosha20777/keras2cpp

Keras2cpp 是一个小型库,用于 运行 从 C++ 应用程序训练的 Keras 模型,没有任何依赖项。

支持的 Keras 图层: - 密集 - 卷积一维 - 卷积二维 - 卷积三维 - 展平 - ELU - 激活 - MaxPooling2D - 嵌入 - LocallyConnected1D - LocallyConnected2D - 长短期记忆网络 - 格鲁乌 - 美国有线电视新闻网 - 批量归一化

支持的激活: - 线性 - 热鲁 - 软加 - tanh - 乙状结肠 - hard_sigmoid - 埃卢 - 软件签名 - 软最大

设计目标:

  • 与 Keras 使用 TensorFlow 后端生成的网络的兼容性。
  • CPU。
  • 无外部依赖,标准库,C++17。
  • 模型存储在内存中。