35

From SavedModel Docs,

SavedModel, the universal serialization format for TensorFlow models.

and

SavedModel wraps a TensorFlow Saver. The Saver is primarily used to generate the variable checkpoints.

From my understanding, SavedModel is must if someone wants use TensorFlow Serving. However, I can deploy Tensorflow Model to service server without SavedModel: Freeze graph and export it as GraphDef, and load graph into Session using ReadBinaryProto and Create in C++ or Import in Go.

What is the purpose of SavedModel? Should users prefer SavedModel over Checkpoint or GraphDef to aggregate more data related to the model?

Byoungchan Lee
  • 1,372
  • 13
  • 28
  • Most people use checkpoints, I think, and `tf.saved_model` is a new feature that we introduced. It should be nicer to use. There is a ddoc [here](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md). – drpng Feb 14 '17 at 07:29
  • 1
    @drpng I'd like to know what is the "best practtces" for dealing with saving model. – Byoungchan Lee Feb 15 '17 at 02:02
  • see this official document [TensorFlow SavedModel](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md), it has a clear comparison between Saver in the Features section. – arganzheng Jul 13 '19 at 08:36

1 Answers1

55

A checkpoint contains the value of (some of the) variables in a TensorFlow model. It is created by a Saver, which is either given specific Variables to save, or by default saves all (non-local) Variables.

To use a checkpoint, you need to have a compatible TensorFlow Graph, whose Variables have the same names as the Variables in the checkpoint. (If you don't have a compatible Graph, you can still load the values stored in a checkpoint into selected Variables using the init_from_checkpoint utilities in contrib.)

SavedModel is much more comprehensive: It contains a set of Graphs (MetaGraphs, in fact, saving collections and such), as well as a checkpoint which is supposed to be compatible with these Graphs, and any asset files that are needed to run the model (e.g. Vocabulary files). For each MetaGraph it contains, it also stores a set of signatures. Signatures define (named) input and output tensors.

This means that given only a SavedModel, you can write tools (such as tensorflow/serving, or the new saved_model command line utility that will appear in tools/ shortly) that interpret or execute the graphs inside. All you have to provide is the data.

If in doubt, I would always err on the side of writing a SavedModel, not just a checkpoint. Not only does this allow you to use tensorflow/serving (and other neat utilities that will grow in number), it makes sure that you have all the information necessary to run the model. Nothing is more frustrating than a checkpoint you cannot use any more because you modified your model and now it is incompatible with checkpoint files and all you want to do is run some predictions through it for comparison.

saeta
  • 639
  • 4
  • 8
wicke
  • 739
  • 7
  • 7
  • 3
    Can I hook in here and ask you to update your answer with some information around using `SavedModel` for training? Checkpoints are great because they allow you to keep multiple model versions. And because they allow the training to be aborted. `SavedModel` seems to only save one checkpoint once (when calling `add_meta_graph_and_variables`) and never again. Is there a way of writing checkpoints regularly? – P.R. May 24 '17 at 14:05
  • 4
    We usually write checkpoints regularly, and then wrap one of them (the best one, typically) into a SavedModel. You could equally simply make each checkpoint into a SavedModel, there's not reason to make only one. – wicke Jun 06 '17 at 17:06
  • SavedModel seems to take much much longer to load, please see here https://stackoverflow.com/questions/44238347/loading-savedmodel-is-a-lot-slower-than-loading-a-tf-train-saver-checkpoint – bw4sz Sep 19 '17 at 17:04
  • But the `Saver` also saves the graph in a .meta file, so I have the complete information (graph + variables) using the `Saver`, too, don't I? – Alex Oct 01 '17 at 14:11
  • No, the .meta file does not contain the variables. You have the checkpoint containing the variables, but it's a separate file without strong guarantees that it would fit any particular (Meta)GraphDef. – wicke Oct 18 '17 at 22:15
  • @wicke, with checkpoints, I can set my callbacks to save the best only one, How should I save the "Savedmodel" at best metric? Should I load the best checkpoint after I trained model and save the "Savedmodel"? Appreciate your comment – Hamid K Oct 07 '19 at 11:07