tensorflow之freeze_gragh

The main understanding of usage under freeze_graph

Under freeze_graph_test and understand some of the relevant knowledge (said to have good learning value)

freeze_graph.py Source link:

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py

freeze_graph_test.py Source link:

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph_test.py

Basic introduction tf model

Tensorflow all document formats are based on Protocol Buffer, namely protobuf

Defined data structure, protobuf tool generates C, Python and other languages ​​classes in a text document, these classes can be friendly to load, save and orientation data

Tensorflow in the Graph object is calculated on the basis

It can store the network nodes, each node represents an action, as the input and output links and each other

The class is ProtoBuf GraphDef

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/graph.proto

As a basis for defining the object was created. Protobuf tool parses the text document and generate a user loading, storing and manipulating the code defined in FIG.

The document is loaded into grapf_def variable, you can access the data

Can use the following code to traverse these nodes are substantially important part is stored in the node.

1
2
graph_def = graph_pb2.GraphDef()
for node in graph_def.node

Each node is in a node_def.proto NodeDef object definition, these nodes are the basic building block of FIG Tensorflow, each building block defines an operation and whose input is connected. NodeDef members as follows:

  • Unique identifier for a node name, the identifier will not be used by any other node on the way
  • op defines the action to run, such as Add, MatMul, Conv2D
  • input character list tables, each string is the name of another node, such as the two input [ "input_1: 0", "input_2: 0"]
  • device defines the position to run in a distributed environment
  • attr contains all the attributes of a node in the key-value pair storage area

More members can be accessed by node.name node.op etc.

Because tf during training at the right weight is typically not stored in the document format, but stored in a separate checkpoint, and Variable operating diagram can load the latest value during initialization.

When deployed to a production environment, use a separate document often is not very convenient, so we need a script freeze_graph.py

These checkpoints, freeze the document to a document.

Specific operation is to load GraphDef, extract the value of all the variables from the latest checkpoint in the document, and then replaced each operation Variable Const (which includes the weight stored in its properties numerical data). Then, it will release all the nodes are not independent for forward inference, and the resulting GraphDef stored into the output document.

freeze_graph.py

First understand the following parameters:

  • input_graph model documents, text or binary pb meta, distinguished by input_binary
  • input_saver to load documents Tensorflow saver
  • input_checkpoint checkpoint file for recovery model variable values
  • Checkpoint_version variable document format (saver_pb2.SaverDef.V1 or saver_pb2.SaverDef.V2)
  • output_graph frozen after the completion of the write path
  • Whether input_binary input document is a binary True Or False
  • Name output_node_names output node, a plurality of nodes separated by commas
  • restore_op_name Obsolete
  • filename_tensor_name Obsolete
  • clear_devices default is True, training equipment clear whether the node
  • initializer_nodes need to initialize the node
  • variable_names_whitelist specified variable needs to be restored
  • variable_names_blacklist not specified variable recovery
  • input_meta_graph to load MetaGraphDef
  • Path input_saved_model_dir SavedModel documents and variables
  • saved_model_tags loading MetaGraphDef the tag group, a comma-separated (MetaGraphDef tags can be used to distinguish different calculation map)

First, resolve checkpoint version:

1
2
3
4
5
6
7
if flags.checkpoint_version == 1:
checkpoint_version = saver_pb2.SaverDef.V1
elif flags.checkpoint_version == 2:
checkpoint_version = saver_pb2.SaverDef.V2
else:
raise ValueError("Invalid checkpoint version (must be '1' or '2'): %d" %
flags.checkpoint_version)

Save two ways to checkpoint as follows:

v1 v2
model.ckpt-0001 model.ckpt-0001.index
model.ckpt-0001.meta model.ckpt-0001.meta
model.ckpt-0001.data-00000-of-00001

Then parse graphDef input:

continue…

reference:

https://www.tensorflow.org/guide/extend/model_files#freezing

https://blog.csdn.net/czq7511/article/details/72452985

Original link large column  https://www.dazhuanlan.com/2019/08/24/5d612ad619bfd/

Guess you like

Origin www.cnblogs.com/chinatrump/p/11415213.html