Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relationship between tensorflow saver, exporter and save model

Question:

  1. Tensorflow Saver ,Exporter, SavedModelBuilder can all be used for save models. According to https://stackoverflow.com/questions/41740101/tensorflow-difference-between-saving-model-via-exporter-and-tf-train-write-graph, and tensor flow serving, I understand that Saver is used for saving training checkpoints and Exporter and SavedModelBuilder are used for serving.

    However,I don't know the differences of their outputs. Are variable.data-???-of--??? and variable.index files generated by SavedModelBuilder the same as cpkt-xxx.index and cpkt-xxx.data-???-of-??? generated by Saver?

  2. I still feel confused about the meaning of the model files of tensorflow. I've read http://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/ and Tensorflow: how to save/restore a model? which makes me feel more confused.

    There are 4 files in the model directory:

    1. graph.pbtxt
    2. model.ckpt-number.data-00000-of-00001
    3. model.ckpt-number.meta
    4. model.ckpt-number.index

    File 2 and 4 store the weights of variables. File 3 stores the graph. Then what does 1 store?

  3. How can I convert the outputs of Saver to SavedModelBuilder. I have the checkpoints directory and want to export the model for serving. According to https://github.com/tensorflow/tensorflow/tree/master/tensorflow/python/saved_model

it should be like this

export_dir = ...
...
builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
with tf.Session(graph=tf.Graph()) as sess:
  ...
  builder.add_meta_graph_and_variables(sess,
                                       [tf.saved_model.tag_constants.TRAINING],
                                       signature_def_map=foo_signatures,
                                       assets_collection=foo_assets)
...
with tf.Session(graph=tf.Graph()) as sess:
  ...
  builder.add_meta_graph(["bar-tag", "baz-tag"])
...
builder.save()

So, I first need to load the checkpoints with :

saver = tf.train.import_meta_graph('model-number.meta')
saver.restore(sess, tf.train.latest_checkpoint('./'))

And then use this sess for builder.

Am I right?

like image 419
lionel Avatar asked Jul 20 '17 07:07

lionel


1 Answers

SavedModel is the format used for serving, created via SavedModelBuilder. The best practice is to have your training code invoke SavedModelBuilder, and to feed the resulting output files to TF-Serving. If you do that you don't need to understand the details of what files are produced :)

The document at [1] talks about the structure of the files inside a SavedModel directory.

[1] https://www.tensorflow.org/programmers_guide/saved_model

like image 73
Christopher Olston Avatar answered Nov 17 '22 14:11

Christopher Olston