Exporting a stateful Keras model to C++

Part 2: Saving Tensorflow graphs developed with Keras

Posted on July 13, 2017

Now that we have access to the nodes we need, we can use Tensorflow’s freeze_graph.py script to export the graph. The script creates a convenient protobuf (pb) file with both the model architecture and weights. It takes an input graph file with the model architecture and a checkpoint file with the model weights and converts the weight variables to constants according to the checkpoint file. Using test_freeze_graph.py as reference:

The freeze script determines which nodes to save according to which output nodes are passed, allowing it to omit nodes that are used only during training. In addition to the output node of our graph, we must include reset_ops and update_ops in the output node list so they can be preserved. We need to provide the names of the nodes as a comma-separated string.

The default behavior of freeze_graph.py is to convert all variables to constants. Since the states will change between each run, they must remain variables in C++. In order to freeze the weights without freezing the states, provide a comma-separated string with the names of the state variables.

Lastly, run the script to save the Tensorflow graph.

A final subtlety: freeze_graph expects the name of the node rather than the tensor. The access functions used above return the name of the tensor, which is usually suffixed by “:0”. Simply replace the suffix to get the name of the node.

The names of the nodes will become important in C++. To verify the names of the nodes or see all the nodes in the graph, use the following script: