将 CNN tensorflow 模型冻结为 .pb 文件 [英] Freezing a CNN tensorflow model into a .pb file
问题描述
我目前正在使用 CNN 试验超分辨率.要为我的模型提供服务,我需要先将它冷冻到 .pb 文件中,对吗?作为一个新手,我真的不知道该怎么做.我的模型基本上是这样的:
I'm currently experimenting with superresolution using CNNs. To serve my model I'll need to frezze it first, into a .pb file, right? Being a newbie I don't really know how to do that. My model basically goes like this:
低分辨率输入图像 -> 双三次缩放 (2x) -> 馈入 CNN -> 具有相同 (2x) 分辨率的 CNN 输出图像.
low res input image -> bicubic scaling (2x) -> fed to CNN -> CNN output image with the same (2x) resolution.
我的模型有 3 个简单的层.输出层称为输出".您可以在此处找到模型:
My model has 3 simple layers. The output layer is called "output". You can find the model here:
https://github.com/pinae/Superresolution
它像这样保存它的进度:
It saves its progress like so:
- 检查站
- network_params.data-00000-of-00001
- network_params.index
- network_params.meta
我知道这样做的方法.
这似乎是为多个输出节点(用于识别)制作的,而不是为只有一个输出的超分辨率制作的.我不知道如何修改该脚本以供我使用.
This seems to be made for multiple output nodes (for identification) and not for superresolution which only has one output. I don't know how to modify that script for my usage.
第二:使用 freeze_graph.py
Second: Use freeze_graph.py
再一次,我完全不知道如何在我的模型中使用它.所有示例似乎都基于 MNIST 教程.
Again, I'm totally lost on how to use this with my model. All examples seem to be based on the MNIST tutorial.
谢谢!
推荐答案
不明白你的意思,但在元流文章中,他也使用了一个输出节点.您可以根据您命名 tensor
的方式添加多个.
Don't understand what you mean but in the metaflow article, he's also using one output nodes. You can add several depending on how you name your tensor
.
在你的情况下,看看 network.py
.你需要看看output_layer
:
In you case, have a look at the network.py
. You need to look at the output_layer
:
self.output = self.conv_layer("reconstruction_layer", self.layer_params[-1],
non_linear_mapping_layer, linear=True)
正如你所看到的,由于conv_layer
,它已经有了名字,所以在元流代码中,你需要做这样的事情:
As you can see it's already name due to conv_layer
, so in the metaflow code, you need to do something like this:
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"
...
注意:有时它在命名中带有前缀,例如在元流文章Accuracy/predictions
的情况下,Accuracy 是一个前缀.因此,打印出您存储在检查点中的所有变量名称是有意义的.
Note: Sometimes it has a prefix in the naming like Accuracy is a prefix in the case of the metaflow article, Accuracy/predictions
. Therefore, it would make sense to print out all the variables name that you stored in the checkpoint.
顺便说一下,从 TF 1.0 开始,您可以使用 SavedModelBuilder
保存模型.这是首选方式,因为它提供了跨多种语言的兼容性.唯一需要注意的是,它仍然不是一个单独的文件,但可以与 Tensorflow Serving 配合使用.
By the way, since TF 1.0 you can save your model with the SavedModelBuilder
. This is the preferred way as it offers compatibility across multiple languages. The only caveats is that it is still not one single file but works well with Tensorflow Serving.
这篇关于将 CNN tensorflow 模型冻结为 .pb 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!