Tensorflow : What is the relationship between .ckpt file and .ckpt.meta and .ckpt.index , and .pb file

Question:

I used saver=tf.train.Saver() to save the model that I trained, and I get three kinds of files named:

  • .ckpt.meta
  • .ckpt.index
  • .ckpt.data

And a file called:

  • checkpoint

What is the connection with the .ckpt file?

I saw someone saved model with only .ckpt file, I don’t know how to make it.
How can I save model as a .pb file?

Asked By: Frank.Fan

||

Answers:

  • the .ckpt file is the old version output of saver.save(sess), which is the equivalent of your .ckpt-data (see below)

  • the “checkpoint” file is only here to tell some TF functions which is the latest checkpoint file.

  • .ckpt-meta contains the metagraph, i.e. the structure of your computation graph, without the values of the variables (basically what you can see in tensorboard/graph).

  • .ckpt-data contains the values for all the variables, without the structure. To restore a model in python, you’ll usually use the meta and data files with (but you can also use the .pb file):

    saver = tf.train.import_meta_graph(path_to_ckpt_meta)
    saver.restore(sess, path_to_ckpt_data)
    
  • I don’t know exactly for .ckpt-index, I guess it’s some kind of index needed internally to map the two previous files correctly. Anyway it’s not really necessary usually, you can restore a model with only .ckpt-meta and .ckpt-data.

  • the .pb file can save your whole graph (meta + data). To load and use (but not train) a graph in c++ you’ll usually use it, created with freeze_graph, which creates the .pb file from the meta and data. Be careful, (at least in previous TF versions and for some people) the py function provided by freeze_graph did not work properly, so you’d have to use the script version. Tensorflow also provides a tf.train.Saver.to_proto() method, but I don’t know what it does exactly.

There are a lot of questions here about how to save and restore a graph. See the answer here for instance, but be careful that the two cited tutorials, though really helpful, are far from perfect, and a lot of people still seem to struggle to import a model in c++.

EDIT:
it looks like you can also use the .ckpt files in c++ now, so I guess you don’t necessarily need the .pb file any more.

Answered By: gdelab
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.