What's the diff between tf.import_graph_def and tf.train.import_meta_graph - tensorflow

When training in model folder there are auto saved meta graph files.
So what's the diff between graph and meta graph.
If I want to load model and do inference witout building graph from scratch,
use tf.train.import_meta_grah is fine?

Well, I find the answer from
https://www.tensorflow.org/versions/r0.9/how_tos/meta_graph/index.html
and
How to load several identical models from save files into one session in Tensorflow
And you can also refer to this:
https://github.com/tensorflow/tensorflow/issues/4658

Related

What is difference between a regular model checkpoint and a saved model in tensorflow?

There's a fairly clear difference between a model and a frozen model. As described in model_files, relevant part: Freezing
...so there's the freeze_graph.py script that takes a graph definition and a set of checkpoints and freezes them together into a single file.
Is a "saved_model" most similar to a "frozen_model" (and not a saved
"model_checkpoint")?
Is this defined somewhere in docs I'm missing?
In prior versions of tensorflow we would save and restore model
weights, but this seems to be in context of a "model_checkpoint" not
a "saved_model", is that still correct?
I'm asking more for the design overview here, not implementation specifics.
Checkpoint file only contains variables for specific model and should be loaded with either exactly same, predefined graph or with specific assignment_map to load only chosen variables. See https://www.tensorflow.org/api_docs/python/tf/train/init_from_checkpoint
Saved model is more broad cause it contains graph that can be loaded within a session and training could be continued. Frozen graph, however, is serialized and could not be used to continue training.
You can find all the info here https://www.tensorflow.org/guide/saved_model

How do I convert a Tensorflow model to .mlmodel?

I want to convert a Tensorflow model with the following structure to a .mlmodel file for use in an iOS app:
cub_image_experiment/
logdir/
val_summaries/
test_summaries/
finetune/
val_summaries/
cmds.txt
config_train.yaml
config_test.yaml
I'm following this tutorial: https://github.com/visipedia/tf_classification/wiki/CUB-200-Image-Classification
However, I'm having trouble understanding the structure of the project. Which files are important and how do I convert all the separate config files and everything into a single .mlmodel file so that I can use in my application?
I've looked online and all I could find was how to convert .caffemodel to .mlmodel or .pb file to .mlmodel. These are all single files, however my project has multiple files. I found a tutorial on how to convert a tf model into a single .pb file, however, that model's structure was different and it did not contain any yaml files. My project is not focused on creating a model at the moment, but merely integrating a model into an iOS app. I found this model interesting for an app idea and wanted to know if it can be integrated. If there are any tutorials out there that might help me in this sort of problem please let me know.
None of that stuff is used by the Core ML model. The yaml files etc are used only to train the TF model.
All you need to provide is a frozen graph (a .pb file) and then convert it to an mlmodel using tfcoreml.
It looks like your project doesn't have a frozen graph but checkpoints. There is a TF utility that you can use to convert the checkpoint to a frozen graph, see https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py

I want to use a graph with Tensorboard that was created using the GraphDef and associated routines

I want to use Tensorboard with a non tensorflow app. I can see how to make the graph using GraphDef and associated classes but not sure how to write it out so that Tensorboard will read it. This means that I have the graph in a serialized form and it's not the python graph class from tensorflow.
For seeing graph in tensorboard, you need weight, tensor name and structure of the graph.
I dont understand your question totally, but if you are able to create graph.pb file then , it will be simple to see that thing in tensorboard you have to run this file here.
actually here we are creating dummy graph structure using
graph_def = graph_pb2.GraphDef()
and then giving our .pb file to set all these weight and name
graph_def.ParseFromString('pb file directory to read'.read())
import_graph_def(graph_def)
let me know more detail so that i can help you better way.

What are the assets in tensorflow?

I am reading tensorflow tutorial on saving and restoring model and came across the following statement:
If assets need to be saved and written or copied to disk,
they can be provided when the first MetaGraphDef is added. If multiple
MetaGraphDefs are associated with an asset of the same name,
only the first version is retained.
What does assets in this context mean?
Also another paragraphs says:
We provide a Python implementation of the SavedModel builder.
The SavedModelBuilder class provides functionality to save multiple MetaGraphDefs.
A MetaGraph is a dataflow graph, plus its associated variables, assets,
and signatures. A MetaGraphDef is the protocol buffer representation of
a MetaGraph. A signature is the set of inputs to and outputs from a graph.
What is dataflow graph and how is it different from graph?
Here is the tutorial
TL;DR
All you need to know when saving a Tensorflow model is that there are two files that are created (potentially more if you're using checkpoints):
file
file.meta
you save 'file' and restore 'file.meta'.
More info here: Tensorflow: how to save/restore a model?
#########
More to your question:
what you define in Tensorflow before you run your session is called a graph.
When you are saving your graph, a MetaGraph is created. This is the graph itself, and all the other metadata necessary for computations in this graph, as well as some user info that can be saved and version specification.
Assets are external files, such as vocabularies that were created with the graph.

model saver meta file is huge, can I configure tensorflow to not write it?

I am doing transfer learning in tensorflow with vgg16. I am only training one small layer on top of the 500MB of weights that I got from the numpy npz file on that website. When I save my model, I am specifying just the weights I am training, so the model file is small - however the meta file is 500MB. From reading stackoverflow: what-is-the-tensorflow-checkpoint-meta-file it sounds safe to remove the meta files, but can I configure tensorflow to not write them?
You could try the tf.train.Saver.save with the optional write_meta_graph=False param.
https://www.tensorflow.org/versions/r0.11/api_docs/python/state_ops.html#Saver