Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does freezing a graph in TensorFlow mean?

Tags:

tensorflow

I am a beginner in NN APIs and TensorFlow.

I am trying to save my trained model in protobuff format (.pb), there are many blogs explaining how to save the model as protobuff. One thing I did not understand is what is the importance of freezing the graph before saving it as protobuff? I read that freezing coverts variable to constants, does that mean the model is not trainable anymore? What else will freezing do on models? What is that model loses after freezing? Can anyone please explain or give some pointers on details of freezing?

like image 874
DTharun Avatar asked Jan 25 '18 11:01

DTharun


People also ask

What is frozen detection graph?

frozen_inference_graph.pb, is a frozen graph that cannot be trained anymore, it defines the graphdef and is actually a serialized graph and can be loaded with this code: def load_graph(frozen_graph_filename): with tf.gfile.GFile(frozen_graph_filename, "rb") as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f. ...

What is the use of frozen inference graph?

Frozen graphs are commonly used for inference in TensorFlow and are stepping stones for inference for other frameworks. TensorFlow 1. x provided an interface to freeze models via tf. Session , and I previously had a blog on how to use frozen models for inference in TensorFlow 1.

How do I freeze a TensorFlow model?

Let's explore the different steps we have to perform: Retrieve our saved graph: we need to load the previously saved meta-graph in the default graph and retrieve its graph_def (the ProtoBuf definition of our graph) Restore the weights: we start a Session and restore the weights of our graph inside that Session.


1 Answers

This is only a partial answer to your question.

A freezed graph is easily optimizable. When doing inference (forward propagation) for instance you can fuse some of the layers together. This you can't do with a graph separated between variables and operations (a not frozen graph). Why would you want to fuse layers together? There are multiple reasons. Going hardware specific: it might be easier to compute a number of operations together in a group of tensors, specific to the structure of your cpu or gpu. TensorRT is a graph optimizer for instance that works starting from a frozen graph (here more info on graph optimizations done by tensorRT: https://devblogs.nvidia.com/tensorrt-integration-speeds-tensorflow-inference/ ). This software does graph optimizations as well as hardware specific ones.

As far as I understand you can unfreeze a graph. I have only worked optimizing them, so I haven't use this feature. But here there is code for it: https://gist.github.com/tokestermw/795cc1fd6d0c9069b20204cbd133e36b

Here is another question that might be useful: TensorFlow: Is there a way to convert a frozen graph into a checkpoint model? It has not yet been answered though.

like image 136
Nyla Worker Avatar answered Sep 21 '22 12:09

Nyla Worker