我是tensorflow和keras的新手。我使用keras训练了一个用于句子分类的CNN,并使用以下代码导出了该模型
K.set_learning_phase(0)
config = model.get_config()
weights = model.get_weights()
new_model = Sequential.from_config(config)
new_model.set_weights(weights)
builder = saved_model_builder.SavedModelBuilder(export_path)
signature = predict_signature_def(
inputs={'input': new_model.inputs[0]},
outputs={'prob': new_model.outputs[0]})
with K.get_session() as sess:
builder.add_meta_graph_and_variables(
sess=sess,
tags=[tag_constants.SERVING],
clear_devices = True,
signature_def_map={
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature}
)
builder.save()我在variables文件夹和saved_model.pb中有ariables.data-00000-of-00001和variables.index。
我想在部署预测之前将这些文件合并到一个文件中。最后,我想将模型量化为变量文件大小非常大,我认为在使用tensorflow的量化功能之前,我需要将我的模型冻结在pb文件中。请帮帮忙
发布于 2017-11-28 06:20:28
您可以使用freeze_graph.py工具将您的文件合并为一个文件。
这将输出一个包含所有权重和体系结构的GraphDef文件。
你可以这样使用它:
bazel build tensorflow/python/tools:freeze_graph && \
bazel-bin/tensorflow/python/tools/freeze_graph \
--input_graph=some_graph_def.pb \
--input_checkpoint=model.ckpt-8361242 \
--output_graph=/tmp/frozen_graph.pb --output_node_names=softmax其中input_graph是您的saved_model.pb文件。
其中input_checkpoint是variables文件夹中的变量,它们可能如下所示:
/tmp/model/model-chkpt-8361242.data-00000-of-00002
/tmp/model/model-chkpt-8361242.data-00001-of-00002
/tmp/model/model-chkpt-8361242.index
/tmp/model/model-chkpt-8361242.meta请注意,在本例中,您将模型检查点称为model-chkpt-8361242。
在使用freeze_graph.py工具时,您需要获取每个文件的前缀。
发布于 2017-11-29 07:00:50
你打算如何为你的模型服务?TensorFlow服务本机支持SavedModelFormat,不需要执行freeze_graph.py步骤。
如果您仍然希望手动组合图形和变量(并使用freeze_graph.py),则可能需要使用上面克拉伦斯演示的较旧的ExportModel格式。
此外,在这一点上,您也可能希望切换到Estimator API。
以下是使用上述所有方法的一些示例:https://github.com/pipelineai/pipeline
https://stackoverflow.com/questions/47520446
复制相似问题