QUERY: Save / Freeze model : Saver vs SavedModelBuilder

656 views
Skip to first unread message

Vibhor Jain

unread,
Feb 1, 2018, 6:08:55 PM2/1/18
to Discuss
Hi, I'm relatively new to TensorFlow and learning how to save / free models and restore.

Can anyone clarify pls:
  1. any difference between "saving a model" vs. "freezing a model" ? or just different in terminologies, different people using different terms for same things..
  2. I found 2 ways to achieve this : 
a) Saver class
    • Create a saver object = tf.train.Saver()
    • call the saver.save() method to save variables to checkpoint files
b) SavedModelBuilder class
    • create a builder object = tf.saved_model.builder.SavedModelBuilder(export_dir)
    • use builder.add_meta_graph_and_variables(sess, [tag_constants.TRAINING],  signature_def_map=foo_signatures,  assets_collection=foo_assets)
    • call builder.save() method
are the two more or less same (one perhaps new, other legacy / old).. what pros & cons, when will you use one over the other

Also I noticed, one saves graph & model in .meta, .index, .data, .checkpoint files, while the other saves .pb|pbtxt, .index, .data and no .checkpoint file...

Any clarification in this regard will be highly appreciatble :)

Thanks a lot

Cheers!

/Vibhor

Martin Wicke

unread,
Feb 1, 2018, 7:21:54 PM2/1/18
to Vibhor Jain, Discuss
Saving a model usually means saving its variable values to disk. If you use Saver, it will *only* save the variable values in a checkpoint.

If you use SavedModel, it also saves the graph and "signatures" which tell consumers of the SavedModel (such as tensorflow/serving) how it can be used (which inputs/outputs to apply), and any other files you need to use the model (assets, e.g., vocabulary files). 

A model saved in either of these ways can still be trained (with Saver, you have to have the original python code. It is possible to train a SavedModel directly, although most users I have seen still use the original python code to continue training).

Freezing a model means to replace variables in the graph with constants. This is commonly done for mobile deployment using the freeze_graph.py script. 



--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+unsubscribe@tensorflow.org.
To post to this group, send email to dis...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/1ea72c14-fa24-4b48-b085-cff42fe3aa3b%40tensorflow.org.

Vibhor Jain

unread,
Feb 1, 2018, 7:36:35 PM2/1/18
to Discuss
Thanks Martin!

yeah I read in docs & SO that Saver.save will *only* save the Variables values (Tensors).
but from own tests, after I restored the model tf.train.import_meta_graph() > saver.restore()
  • I'm able to restore the whole original graph (al the ops nodes) 
  • as well as the constant values (just as Variable values) using sess.run('const:0')

Martin Wicke

unread,
Feb 1, 2018, 7:47:58 PM2/1/18
to Vibhor Jain, Discuss
True, the graph is saved if you give it. You still need to know all the tensor names, SavedModel will give you that information (and makes sure you have all the auxiliary files you need). 

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+unsubscribe@tensorflow.org.
To post to this group, send email to dis...@tensorflow.org.

Sathyamoorthy R

unread,
Mar 4, 2019, 6:51:19 AM3/4/19
to Discuss
How can we save assets using SavedModelBuilder?
Are the assets saved automatically (or) do we need to explicitly specify?

foo_assets in the documentation does not give any clue.
What is that actually? A file name? Tensor name? or something? no clue??

Any links would be helpful?
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.
Reply all
Reply to author
Forward
0 new messages