简体   繁体   English

将CNN张量流模型冻结到.pb文件中

[英]Freezing a CNN tensorflow model into a .pb file

I'm currently experimenting with superresolution using CNNs. 我目前正在尝试使用CNN进行超分辨率。 To serve my model I'll need to frezze it first, into a .pb file, right? 要为我的模型提供服务,我需要先将其保存为.pb文件,对吗? Being a newbie I don't really know how to do that. 作为一个新手,我真的不知道该怎么做。 My model basically goes like this: 我的模型基本上是这样的:

low res input image -> bicubic scaling (2x) -> fed to CNN -> CNN output image with the same (2x) resolution. 低分辨率输入图像->双三次缩放(2x)->馈入CNN->具有相同(2x)分辨率的CNN输出图像。

My model has 3 simple layers. 我的模型有3个简单层。 The output layer is called "output". 输出层称为“输出”。 You can find the model here: 您可以在这里找到模型:

https://github.com/pinae/Superresolution https://github.com/pinae/Superresolution

It saves its progress like so: 这样可以保存进度:

  • checkpoint 检查站
  • network_params.data-00000-of-00001 network_params.data 00000-的-00001
  • network_params.index network_params.index
  • network_params.meta network_params.meta

I see to ways of doing this. 我看到这样做的方法。

First: Follow this tutorial: https://blog.metaflow.fr/tensorflow-how-to-freeze-a-model-and-serve-it-with-a-python-api-d4f3596b3adc 首先:遵循本教程: https : //blog.metaflow.fr/tensorflow-how-to-freeze-a-model-and-serve-it-with-a-python-api-d4f3596b3adc

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. 我不知道如何修改该脚本以供使用。

Second: Use freeze_graph.py 第二:使用freeze_graph.py

Again, I'm totally lost on how to use this with my model. 同样,我完全迷失了如何在模型中使用它。 All examples seem to be based on the MNIST tutorial. 所有示例似乎都基于MNIST教程。

Thanks! 谢谢!

Don't understand what you mean but in the metaflow article, he's also using one output nodes. 不明白您的意思,但是在metaflow文章中,他还使用了一个输出节点。 You can add several depending on how you name your tensor . 您可以根据tensor命名方式添加多个。

In you case, have a look at the network.py . 在这种情况下,请查看network.py You need to look at the output_layer : 您需要查看output_layer

    self.output = self.conv_layer("reconstruction_layer", self.layer_params[-1],
                                  non_linear_mapping_layer, linear=True)

As you can see it's already name due to conv_layer , so in the metaflow code, you need to do something like this: 如您所见,由于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"
    ...

Note: Sometimes it has a prefix in the naming like Accuracy is a prefix in the case of the metaflow article, Accuracy/predictions . 注意:有时它在命名中带有前缀,例如Accuracy是元流文章Accuracy/predictions的前缀。 Therefore, it would make sense to print out all the variables name that you stored in the checkpoint. 因此,打印出您存储在检查点中的所有变量名称是有意义的。

By the way, since TF 1.0 you can save your model with the SavedModelBuilder . 顺便说一句,从TF 1.0开始,您可以使用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. 唯一需要注意的是,它仍然不是一个文件,但可以与Tensorflow Serving一起很好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM