[Tensorflow] Learning Note: Try to downsize graph.pb model
In this topic, I will introduce more details about how to downsize training model if we want to use it on device.
(NOTE: Tensorflow also supports the mobile version.
I have not studied yet, but I think it is a good reference for you. )
Downsize model by using freeze graph
Why freezing ?
In Tensorflow-tool-develop-freezing said that:"What this does is load the GraphDef, pull in the values for all the variables from the latest checkpoint file, and then replace each Variable op with a Const that has the numerical data for the weights stored in its attributes It then strips away all the extraneous nodes that aren't used for forward inference, and saves out the resulting GraphDef into an output file
How to freeze graph ?
Use the freez_graph.py, and run the following commands:bazel build tensorflow/python/tools:freeze_graph && \
bazel-bin/tensorflow/python/tools/freeze_graph \
--input_graph=some_graph_def.pb \
--input_checkpoint=model.ckpt-8361242 \
--output_graph=/tmp/frozen_graph.pb --output_node_names=softmax
In my example on github (https://github.com/JackyTung/tensorgraph)
I will use graph.pb and model.ckpt to generate freeze graph
bazel build tensorflow/python/tools:freeze_graph && \
bazel-bin/tensorflow/python/tools/freeze_graph \
--input_graph=graph.pb \
--input_checkpoint=model.ckpt \
--output_graph=/tmp/frozen_graph.pb --output_node_names=softmax
Note:The model size will have not so big different in the mnist example.
However, once using more complex example, you will see the power of freeze_graph.py.
How to extract tensors from ckpt file
Following steps:
$ cd tensorflow/tensorflow/python/tools // list all tensors $ python inspect_checkpoint.py --file_name=$your_ckpt_file_path // print the value from specific tensor $ python inspect_checkpoint.py --file_name=$your_ckpt_file_path --tensor_name=$specific_tensorFollowing demo is from my example:
python inspect_checkpoint.py --file_name=$your_ckpt_file_path
python inspect_checkpoint.py --file_name=$your_ckpt_file_path --tensor_name=$specific_tensor
Be honestly, I am a new beginner in machine learning.
If I wrote something wrong or have a better suggestion, leave message to me.
I'll revise and update my article :)