I train a model and save it using:
saver = tf.train.Saver()
saver.save(session, './my_model_name')
Besides the checkpoint file, which simply contains pointers to the most recent checkpoints of the model, this creates the following 3 files in the current path:
- my_model_name.meta
- my_model_name.index
- my_model_name.data-00000-of-00001
I wonder what each of these files contains.
I'd like to load this model in C++ and run the inference. The label_image example loads the model from a single .bp file using ReadBinaryProto()
. I wonder how I can load it from these 3 files. What is the C++ equivalent of the following?
new_saver = tf.train.import_meta_graph('./my_model_name.meta')
new_saver.restore(session, './my_model_name')
What your saver creates is called "Checkpoint V2" and was introduced in TF 0.12.
I got it working quite nicely (though the docs on the C++ part are horrible, so it took me a day to solve). Some people suggest converting all variables to constants or freezing the graph, but none of these is actually needed.
Python part (saving)
If you create the
Saver
withtf.trainable_variables()
, you can save yourself some headache and storage space. But maybe some more complicated models need all data to be saved, then remove this argument toSaver
, just make sure you're creating theSaver
after your graph is created. It is also very wise to give all variables/layers unique names, otherwise you can run in different problems.C++ part (inference)
Note that
checkpointPath
isn't a path to any of the existing files, just their common prefix. If you mistakenly put there path to the.index
file, TF won't tell you that was wrong, but it will die during inference due to uninitialized variables.For completeness, here's the Python equivalent:
Inference in Python
I'm currently struggling with this myself, I've found it's not very straightforward to do currently. The two most commonly cited tutorials on the subject are: https://medium.com/jim-fleming/loading-a-tensorflow-graph-with-the-c-api-4caaff88463f#.goxwm1e5j and https://medium.com/@hamedmp/exporting-trained-tensorflow-models-to-c-the-right-way-cf24b609d183#.g1gak956i
The equivalent of
Is just
Assuming you've "frozen the graph" (Used a script with combines the graph file with the checkpoint values). Also, see the discussion here: Tensorflow Different ways to Export and Run graph in C++