将 CNN 张量流模型冻结到 .pb 文件中
Freezing a CNN tensorflow model into a .pb file
我目前正在使用 CNN 进行超分辨率实验。要为我的模型提供服务,我需要先将其冻结到 .pb 文件中,对吗?
作为一个新手,我真的不知道该怎么做。
我的模型基本上是这样的:
低分辨率输入图像 -> 双三次缩放 (2x) -> 馈送到 CNN -> 具有相同 (2x) 分辨率的 CNN 输出图像。
我的模型有 3 个简单层。输出层称为"output"。您可以在这里找到模型:
https://github.com/pinae/Superresolution
它像这样保存进度:
- 检查点
- network_params.data-00000-of-00001
- network_params.index
- network_params.meta
我知道如何做到这一点。
这似乎是为多个输出节点(用于识别)而设计的,而不是为只有一个输出的超分辨率设计的。我不知道如何修改该脚本以供我使用。
第二种:使用freeze_graph.py
同样,我完全不知道如何将它用于我的模型。所有示例似乎都基于 MNIST 教程。
谢谢!
不明白你的意思,但是在元流文章中,他也使用了一个输出节点。您可以添加多个,具体取决于您如何命名 tensor
.
在你的情况下,看看 network.py
。您需要查看 output_layer
:
self.output = self.conv_layer("reconstruction_layer", self.layer_params[-1],
non_linear_mapping_layer, linear=True)
如您所见,由于 conv_layer
,它已经命名,因此在元流代码中,您需要执行如下操作:
def freeze_graph(model_folder):
# We retrieve our checkpoint fullpath
checkpoint = tf.train.get_checkpoint_state(model_folder)
input_checkpoint = checkpoint.model_checkpoint_path
# We precise the file fullname of our freezed graph
absolute_model_folder = "/".join(input_checkpoint.split('/')[:-1])
output_graph = absolute_model_folder + "/frozen_model.pb"
# Before exporting our graph, we need to precise what is our output node
# This is how TF decides what part of the Graph he has to keep and what part it can dump
# NOTE: this variable is plural, because you can have multiple output nodes
output_node_names = "reconstruction_layer"
...
注意:有时它在命名中有一个前缀,例如在 Metaflow 文章中 Accuracy 是一个前缀,Accuracy/predictions
。因此,打印出您存储在检查点中的所有变量名称是有意义的。
顺便说一下,从 TF 1.0 开始,您可以使用 SavedModelBuilder
保存您的模型。这是首选方式,因为它提供了跨多种语言的兼容性。唯一需要注意的是,它仍然不是一个单独的文件,但可以与 Tensorflow Serving 配合使用。
我目前正在使用 CNN 进行超分辨率实验。要为我的模型提供服务,我需要先将其冻结到 .pb 文件中,对吗? 作为一个新手,我真的不知道该怎么做。 我的模型基本上是这样的:
低分辨率输入图像 -> 双三次缩放 (2x) -> 馈送到 CNN -> 具有相同 (2x) 分辨率的 CNN 输出图像。
我的模型有 3 个简单层。输出层称为"output"。您可以在这里找到模型:
https://github.com/pinae/Superresolution
它像这样保存进度:
- 检查点
- network_params.data-00000-of-00001
- network_params.index
- network_params.meta
我知道如何做到这一点。
这似乎是为多个输出节点(用于识别)而设计的,而不是为只有一个输出的超分辨率设计的。我不知道如何修改该脚本以供我使用。
第二种:使用freeze_graph.py
同样,我完全不知道如何将它用于我的模型。所有示例似乎都基于 MNIST 教程。
谢谢!
不明白你的意思,但是在元流文章中,他也使用了一个输出节点。您可以添加多个,具体取决于您如何命名 tensor
.
在你的情况下,看看 network.py
。您需要查看 output_layer
:
self.output = self.conv_layer("reconstruction_layer", self.layer_params[-1],
non_linear_mapping_layer, linear=True)
如您所见,由于 conv_layer
,它已经命名,因此在元流代码中,您需要执行如下操作:
def freeze_graph(model_folder):
# We retrieve our checkpoint fullpath
checkpoint = tf.train.get_checkpoint_state(model_folder)
input_checkpoint = checkpoint.model_checkpoint_path
# We precise the file fullname of our freezed graph
absolute_model_folder = "/".join(input_checkpoint.split('/')[:-1])
output_graph = absolute_model_folder + "/frozen_model.pb"
# Before exporting our graph, we need to precise what is our output node
# This is how TF decides what part of the Graph he has to keep and what part it can dump
# NOTE: this variable is plural, because you can have multiple output nodes
output_node_names = "reconstruction_layer"
...
注意:有时它在命名中有一个前缀,例如在 Metaflow 文章中 Accuracy 是一个前缀,Accuracy/predictions
。因此,打印出您存储在检查点中的所有变量名称是有意义的。
顺便说一下,从 TF 1.0 开始,您可以使用 SavedModelBuilder
保存您的模型。这是首选方式,因为它提供了跨多种语言的兼容性。唯一需要注意的是,它仍然不是一个单独的文件,但可以与 Tensorflow Serving 配合使用。