Relationship between tensorflow saver, exporter and save model
Asked Answered
E

1

10

Question:

  1. Tensorflow Saver ,Exporter, SavedModelBuilder can all be used for save models. According to https://mcmap.net/q/1169015/-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?

Elsewhere answered 20/7, 2017 at 7:49 Comment(0)
L
1

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

Lott answered 26/3, 2018 at 19:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.