将 Keras 模型转换为 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36720498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Keras model to C++
提问by pplonski
I am using Keras (with Theano) to train my CNN model. Does anyone has idea how can I use it in my C++ application? Does anyone tried something similar? I have idea to write some python code that will generate a c++ code with network functions - any suggestion on it?
我正在使用 Keras(和 Theano)来训练我的 CNN 模型。有谁知道如何在我的 C++ 应用程序中使用它?有没有人尝试过类似的东西?我有想法写一些python代码来生成一个带有网络功能的c++代码——有什么建议吗?
I found a similar question herehow to use Tensorflow Keras model in C++ but without answer.
我在这里发现了一个类似的问题how to use Tensorflow Keras model in C++ 但没有答案。
回答by pplonski
To answer my own question and have a solution - I wrote a plain c++ solution called keras2cpp(its code available on github).
为了回答我自己的问题并有一个解决方案 - 我编写了一个名为keras2cpp的普通 C++ 解决方案(其代码可在 github 上获得)。
In this solution you store network architecture (in json) and weights (in hdf5). Then you can dump a network to a plain text file with provided script. You can use obtained text file with network in pure c++ code. There are no dependencies on python libraries or hdf5. It should work for theano and tensorflow backend.
在此解决方案中,您存储网络架构(在 json 中)和权重(在 hdf5 中)。然后,您可以使用提供的脚本将网络转储到纯文本文件中。您可以在纯 C++ 代码中使用网络获取的文本文件。不依赖于 python 库或 hdf5。它应该适用于 theano 和 tensorflow 后端。
回答by Tobias Hermann
I found myself in a similar situation but needed to not only support forward passes of sequential Keras models in C++ but also of more complex models build with the functional API.
我发现自己处于类似的情况,但不仅需要支持 C++ 中顺序 Keras 模型的前向传递,还需要支持使用函数式 API构建的更复杂模型。
So I wrote a new library called frugally-deep. You can find it on GitHub and it is published under the MIT License: https://github.com/Dobiasd/frugally-deep
所以我写了一个名为 frugally-deep 的新库。你可以在 GitHub 上找到它,它是在 MIT 许可证下发布的:https: //github.com/Dobiasd/frugally-deep
Additionally to supporting many common layer types it can keep up with (and sometimes even beat) the performance of TensorFlow on a single CPU. You can find up-to-date benchmark results for some common model in the repo.
除了支持许多常见的层类型之外,它还可以在单个 CPU 上跟上(有时甚至超过)TensorFlow 的性能。您可以在repo 中找到一些常见模型的最新基准测试结果。
By automatic testing frugally-deep guarantees that the output of a model used with it in C++ is exactly the same as if run with Keras in Python.
通过自动测试,深度保证在 C++ 中与它一起使用的模型的输出与在 Python 中使用 Keras 运行时完全相同。
回答by AHA
If your keras model is trained using tensorflow backend, you can save the keras model as a tensorflow model following this code: https://github.com/amir-abdi/keras_to_tensorflow
如果您的 keras 模型是使用 tensorflow 后端训练的,您可以按照以下代码将 keras 模型保存为 tensorflow 模型:https: //github.com/amir-abdi/keras_to_tensorflow
Here is a shorter version of the code:
这是代码的较短版本:
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'))
回答by Georgy Perevozchikov
You Can try this one https://github.com/gosha20777/keras2cpp
你可以试试这个 https://github.com/gosha20777/keras2cpp
Keras2cpp is a small library for running trained Keras models from a C++ application without any dependencies.
Keras2cpp 是一个小型库,用于从 C++ 应用程序运行经过训练的 Keras 模型,无需任何依赖。
Supported Keras layers: - Dense - Convolution1D - Convolution2D - Convolution3D - Flatten - ELU - Activation - MaxPooling2D - Embedding - LocallyConnected1D - LocallyConnected2D - LSTM - GRU - CNN - BatchNormalization
支持的 Keras 层: - Dense - Convolution1D - Convolution2D - Convolution3D - Flatten - ELU - Activation - MaxPooling2D - Embedding - LocallyConnected1D - LocallyConnected2D - LSTM - GRU - CNN - BatchNormalization
Supported activation: - linear - relu - softplus - tanh - sigmoid - hard_sigmoid - elu - softsign - softmax
支持的激活: - 线性 - relu - softplus - tanh - sigmoid - hard_sigmoid - elu - softsign - softmax
Design goals:
设计目标:
- Compatibility with networks generated by Keras using TensorFlow backend.
- CPU only.
- No external dependencies, standard library, C++17.
- Model stored in memory.
- 与 Keras 使用 TensorFlow 后端生成的网络兼容。
- 仅限 CPU。
- 无外部依赖,标准库,C++17。
- 模型存储在内存中。
回答by TFreitas
The solutions found here are quite good, but if your model has some different types of layers not supported by these libraries, I would recommend doing the following:
此处找到的解决方案非常好,但如果您的模型具有这些库不支持的某些不同类型的层,我建议您执行以下操作:
- Converting the Keras model to a tensorflow model.
- Freeze the model and use Tranform graph tool provided by tensorflow (you'll have to build it from source with bazel)
- Compile the C++ API tensorflow library to use it in your project.
- Use the C++ API tensorflow library and link the libraries to your project.
- 将 Keras 模型转换为 tensorflow 模型。
- 冻结模型并使用 tensorflow 提供的转换图工具(您必须使用 bazel 从源代码构建它)
- 编译 C++ API tensorflow 库以在您的项目中使用它。
- 使用 C++ API tensorflow 库并将这些库链接到您的项目。
If you want to use a something differentcompiler than bazel (like g++ for example) you can follow this great tuturial:
如果您想使用与 bazel 不同的编译器(例如 g++),您可以遵循这个伟大的教程:
http://tuatini.me/building-tensorflow-as-a-standalone-project/
http://tuatini.me/building-tensorflow-as-a-standalone-project/
回答by 1''
The easiest way is probably to make a system call to a Python script that writes the predictions to a binary or HDF5file, which can be read in from C++. You can also directly integrate Python into C++.
最简单的方法可能是对 Python 脚本进行系统调用,该脚本将预测写入二进制文件或HDF5文件,该文件可以从 C++ 读入。您也可以直接将 Python 集成到 C++ 中。
If you need to deploy and distribute this easily, you can look into self-contained Python installations like Anaconda, but your best bet may be to avoid Keras and use the C++ interface to Caffeor 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 libraryso you can't really go wrong.
如果您需要轻松地部署和分发它,您可以查看像Anaconda这样的自包含 Python 安装,但最好的办法可能是避免使用Keras并使用 C++ 接口连接到Caffe或 Tensorflow。我不会推荐 Tensorflow,因为从 C++ 使用它不是标准的;看到这个讨论。Caffe 可以说是第二受欢迎的深度学习库,所以你绝对不会出错。
回答by moof2k
I had a similar need--I wanted to embed Keras models in a C++ application--and decided to write my own library: Kerasify
我有类似的需求——我想将 Keras 模型嵌入到 C++ 应用程序中——并决定编写自己的库:Kerasify
Design goals of Kerasify:
Kerasify 的设计目标:
- Compatibility with image processing Sequential networks generated by Keras using Theano backend. (Could work with Tensorflow if you switch around matrix col/row ordering).
- No external dependencies, standard library, C++11 features OK.
- Model stored on disk in binary format that can be quickly read.
- Model stored in memory in contiguous block for better cache performance.
- Doesn't throw exceptions, returns only bool on error.
- CPU only, no GPU
- 与 Keras 使用 Theano 后端生成的图像处理顺序网络的兼容性。(如果您切换矩阵列/行排序,则可以与 Tensorflow 一起使用)。
- 无外部依赖,标准库,C++11 特性OK。
- 模型以二进制格式存储在磁盘上,可以快速读取。
- 模型存储在内存中的连续块中,以获得更好的缓存性能。
- 不抛出异常,仅在出错时返回 bool。
- 只有 CPU,没有 GPU
Example code, unit tests, etc. at the github link. It's not fully complete, it only supports the narrow subset of Keras functions I'm using, but it should be extensible with a little effort.
github 链接中的示例代码、单元测试等。它不是完全完整的,它只支持我正在使用的 Keras 函数的一个狭窄子集,但它应该可以通过一些努力进行扩展。