Freezing a CNN tensorflow model into a .pb file

2024/10/13 11:24:15

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:

low res input image -> bicubic scaling (2x) -> fed to CNN -> CNN output image with the same (2x) resolution.

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:

  • checkpoint
  • network_params.data-00000-of-00001
  • network_params.index
  • 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

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

Again, I'm totally lost on how to use this with my model. All examples seem to be based on the MNIST tutorial.

Thanks!

Answer

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.

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)

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 fullpathcheckpoint = tf.train.get_checkpoint_state(model_folder)input_checkpoint = checkpoint.model_checkpoint_path# We precise the file fullname of our freezed graphabsolute_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 nodesoutput_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. 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. 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.

https://en.xdnf.cn/q/118085.html

Related Q&A

flattening a list or a tuple in python. Not sure what the error is

def flatten(t):list = []for i in t:if(type(i) != list and type(i) != tuple):list.append(i)else:list.extend(flatten(i))return listHere is the function that Ive written to flatten a list or a tuple that …

Counting word occurrences in csv and determine row appearances

I have a csv file such as the following in one column. The symbols and numbers are only to show that the file does not just contain text. I have two objectives:count the number of occurrences of a wo…

Converting Python 3.6 script to .exe? [duplicate]

This question already has answers here:How can I convert a .py to .exe for Python?(8 answers)Closed 6 years ago.I would like to convert a .py file to an .exe. I am using Python 3.6. I already tried py…

Error while reading csv file in python

I tried to run following programme in ubuntu terminal but I am getting some error. But it is not giving any error in jupyter notebookFile "imsl.py", line 5 SyntaxError: Non-ASCII character \x…

Searching for a USB in Python is returning there is no disk in drive

I wrote up a function in Python that looks for a USB drive based on a key identifier file, however when called upon it returns There is no disk in the drive. Please insert a disk into drive D:/ (which …

Counting phrases in Python using NLTK

I am trying to get a phrase count from a text file but so far I am only able to obtain a word count (see below). I need to extend this logic to count the number of times a two-word phrase appears in th…

Break python list into multiple lists, shuffle each lists separately [duplicate]

This question already has answers here:Shuffling a list of objects [duplicate](26 answers)Closed 7 years ago.Lets say I have posts in ordered list according to their date.[<Post: 6>, <Post: 5&…

AlterField on auto generated _ptr field in migration causes FieldError

I have two models:# app1 class ParentModel(models.Model):# some fieldsNow, in another app, I have child model:# app2 from app1.models import ParentModelclass ChildModel(ParentModel):# some fields here …

How do I replace values in 2D numpy array using a dictionary of {value:(row#,column#)} pairs

import numpy as npthe array looks like so:array = np.zeros((10,10))array = [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.][ 0…

Processing items with Scrapy pipeline

Im running Scrapy from a Python script.I was told that in Scrapy, responses are built in parse()and further processed in pipeline.py. This is how my framework is set so far:Python scriptdef script(self…