37

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?

gogasca
  • 9,283
  • 6
  • 80
  • 125
Frank.Fan
  • 929
  • 3
  • 14
  • 23

1 Answers1

46
  • 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.

gdelab
  • 6,124
  • 2
  • 26
  • 59
  • 1
    Thank you very much . Now if I want to load .ckpt-meta and .ckpt-data files to identified a picture's class , do you know how to realize this ? or do you have something about this? – Frank.Fan Jun 14 '17 at 01:26
  • 2
    You can look at the question and answer here: https://stackoverflow.com/a/43909969/7456923 The idea is to use `saver = tf.train.import_meta_graph(path_to_ckpt_meta) saver.restore(sess, path_to_ckpt_data)` and then use a collection or variable name to get back a handle on your important input and output tensors, then run inference just like if you had not saved and loaded, with sthg like `sess.run([prediction_class],feed_dict={x_input: data})` – gdelab Jun 14 '17 at 09:25