c++ - 如何从 C++ 使用 Keras SavedModel

标签 c++ tensorflow keras deep-learning computer-vision

我训练了一个用于图像分类的经典 CNN(预训练移动网络)。我现在想在 C++ 中使用这个模型。根据我的理解,我需要创建一个模型库,它可以接受输入并返回其输出。我将模型保存为 .pb (SavedModel) 格式。

我已经试过了,CppFlow ,错误表明它无法读取我的模型。我认为这是由于与 TF 2.0 不兼容造成的。

我也得到了SavedModel的命令行界面工作,但我不知道如何在我的 cpp 应用程序中使用它。

我想知道如何构建我的模型库并使用该库以便它可以即时做出预测。任何指导都会有所帮助。如果需要任何其他信息,请告诉我。

最佳答案

在 C++ 中使用 keras 模型的一种方法是将其转换为 TensorFlow .pb 格式。我刚刚在下面编写了一个用于执行此操作的脚本。

用法:python script.py keras_model.hdf5

它将tensorflow模型输出为附加.pb的输入文件名。

然后你可以使用TF C++ api用于阅读模型和进行推理。在 C++ TF 中使用图像识别模型标记图像的详细示例是 located here .

另一种选择 - 您可以通过从 C++ 调用 Python API 直接使用 Keras,这并不难,有 standalone python这是静态编译的,意味着完全没有 dll/共享库依赖性,因此 python 解释器可以完全编译成 C++ 单个二进制文件。 Internet 上也有许多库可以帮助您轻松地从 C++ 运行 Python。

import sys, os
from keras import backend as K
from keras.models import load_model
import tensorflow as tf

def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
    """
    Freezes the state of a session into a pruned computation graph.

    Creates a new computation graph where variable nodes are replaced by
    constants taking their current value in the session. The new graph will be
    pruned so subgraphs that are not necessary to compute the requested
    outputs are removed.
    @param session The TensorFlow session to be frozen.
    @param keep_var_names A list of variable names that should not be frozen,
                          or None to freeze all the variables in the graph.
    @param output_names Names of the relevant graph outputs.
    @param clear_devices Remove the device directives from the graph for better portability.
    @return The frozen graph definition.
    """
    from tensorflow.python.framework.graph_util import convert_variables_to_constants
    graph = session.graph
    with graph.as_default():
        freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
        output_names = output_names or []
        output_names += [v.op.name for v in tf.global_variables()]
        # Graph -> GraphDef ProtoBuf
        input_graph_def = graph.as_graph_def()
        if clear_devices:
            for node in input_graph_def.node:
                node.device = ""
        frozen_graph = convert_variables_to_constants(session, input_graph_def,
                                                      output_names, freeze_var_names)
        return frozen_graph
        
if len(sys.argv) <= 1:
    print('Usage: python script.py keras_model.hdf5')
    sys.exit(0)
else:
    ifname = sys.argv[1]

model = load_model(ifname)

frozen_graph = freeze_session(
    K.get_session(),
    output_names = [out.op.name for out in model.outputs],
)

tf.io.write_graph(frozen_graph, os.path.dirname(ifname), ifname + '.pb', as_text = False)

关于c++ - 如何从 C++ 使用 Keras SavedModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63991218/

相关文章:

c++ - Bigint +运算符

c++ - 跨平台制作包含子文件夹的目录的方法?

c++ - 在tensorflow c++ api中,如何从张量中保存图片或数组

python - 获取keras模型的学习率

python - "tensorflow.math.multiply"和 "tensorflow.keras.layers.multiply"和有什么区别?

c++ - "usual arithmetic conversions"和 "integer promotions"是一回事吗?

c++ - 引用一个不存在的变量将是一个错误,但为什么这不会导致任何错误呢?

python - 添加 2 个不同阶的张量

tensorflow - 如何在 Julia 中将 TensorFlow 升级到 2.0?

python - LSTM 时间序列 - 奇怪的 val_accuracy,使用哪种归一化方法以及模型拟合后在生产中做什么