TensorFlow saving into/loading a graph from a file
Solution 1
There are many ways to approach the problem of saving a model in TensorFlow, which can make it a bit confusing. Taking each of your sub-questions in turn:
The checkpoint files (produced e.g. by calling
saver.save()
on atf.train.Saver
object) contain only the weights, and any other variables defined in the same program. To use them in another program, you must re-create the associated graph structure (e.g. by running code to build it again, or callingtf.import_graph_def()
), which tells TensorFlow what to do with those weights. Note that callingsaver.save()
also produces a file containing aMetaGraphDef
, which contains a graph and details of how to associate the weights from a checkpoint with that graph. See the tutorial for more details.tf.train.write_graph()
only writes the graph structure; not the weights.Bazel is unrelated to reading or writing TensorFlow graphs. (Perhaps I misunderstand your question: feel free to clarify it in a comment.)
A frozen graph can be loaded using
tf.import_graph_def()
. In this case, the weights are (typically) embedded in the graph, so you don't need to load a separate checkpoint.The main change would be to update the names of the tensor(s) that are fed into the model, and the names of the tensor(s) that are fetched from the model. In the TensorFlow Android demo, this would correspond to the
inputName
andoutputName
strings that are passed toTensorFlowClassifier.initializeTensorFlow()
.The
GraphDef
is the program structure, which typically does not change through the training process. The checkpoint is a snapshot of the state of a training process, which typically changes at every step of the training process. As a result, TensorFlow uses different storage formats for these types of data, and the low-level API provides different ways to save and load them. Higher-level libraries, such as theMetaGraphDef
libraries, Keras, and skflow build on these mechanisms to provide more convenient ways to save and restore an entire model.
Solution 2
You can try the following code:
with tf.gfile.FastGFile('model/frozen_inference_graph.pb', "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
g_in = tf.import_graph_def(graph_def, name="")
sess = tf.Session(graph=g_in)
Related videos on Youtube
Comments
-
Technicolor over 3 years
From what I've gathered so far, there are several different ways of dumping a TensorFlow graph into a file and then loading it into another program, but I haven't been able to find clear examples/information on how they work. What I already know is this:
- Save the model's variables into a checkpoint file (.ckpt) using a
tf.train.Saver()
and restore them later (source) - Save a model into a .pb file and load it back in using
tf.train.write_graph()
andtf.import_graph_def()
(source) - Load in a model from a .pb file, retrain it, and dump it into a new .pb file using Bazel (source)
- Freeze the graph to save the graph and weights together (source)
- Use
as_graph_def()
to save the model, and for weights/variables, map them into constants (source)
However, I haven't been able to clear up several questions regarding these different methods:
- Regarding checkpoint files, do they only save the trained weights of a model? Could checkpoint files be loaded into a new program, and be used to run the model, or do they simply serve as ways to save the weights in a model at a certain time/stage?
- Regarding
tf.train.write_graph()
, are the weights/variables saved as well? - Regarding Bazel, can it only save into/load from .pb files for retraining? Is there a simple Bazel command just to dump a graph into a .pb?
- Regarding freezing, can a frozen graph be loaded in using
tf.import_graph_def()
? - The Android demo for TensorFlow loads in Google's Inception model from a .pb file. If I wanted to substitute my own .pb file, how would I go about doing that? Would I need to change any native code/methods?
- In general, what exactly is the difference between all these methods? Or more broadly, what is the difference between
as_graph_def()
/.ckpt/.pb?
In short, what I'm looking for is a method to save both a graph (as in, the various operations and such) and its weights/variables into a file, which can then be used to load the graph and weights into another program, for use (not necessarily continuing/retraining).
Documentation about this topic isn't very straightforward, so any answers/information would be greatly appreciated.
-
Yaroslav Bulatov over 7 yearsThe newest/most complete API is meta graph, that'll gives you a way to save all three at once -- 1) graph 2) parameter values 3) collections: tensorflow.org/versions/r0.10/how_tos/meta_graph/index.html
- Save the model's variables into a checkpoint file (.ckpt) using a
-
mnicky over 7 yearsDoes this mean that the C++ API documentation lies, when it says that you can load the graph saved with
tf.train.write_graph()
and then execute it? -
mrry over 7 yearsThe C++ API documentation does not lie, but it is missing a few details. The most important detail is that, in addition to the
GraphDef
saved bytf.train.write_graph()
, you also need to remember the names of the tensors that you want to feed and fetch when executing the graph (item 5 above). -
Hossein over 7 years@mrry: I tried to use the tensorflows DeepDream example. but it seems it needs pretrained models in pb format! I ran the Cifar10 example, but it only creates checkpoints! I couldnt find any pb files or whatsoever! how can I convert my checkpoints to the pb format which deepdream example uses?
-
David Ortiz about 7 years@Coderx7 I really think you cannot convert a .ckpt to a .pb since the checkpoint only contains the weights and variables and does not know anything about the structure of the graph
-
Moondra over 6 years@mrry Is the.pb output file considered a frozen file? I can't seem to extract the weight despite using
import_graph_def
stackoverflow.com/questions/46696859/… -
Kong almost 6 yearsis there a simple code to load a .pb file and then run it ?
-
ComputerScientist over 5 yearsThe tutorial link is dead.
-
mrry over 5 yearsUpdated all the tensorflow.org links to the current pages.