Fixing input node of frozen graph, before exporting to tflite format

Viewed 372

I am able to freeze a graph using the following:

freeze_graph.freeze_graph(input_graph=f"{save_graph_path}/graph.pbtxt",
                          input_saver="",
                          input_binary=False,
                          input_checkpoint=last_ckpt,
                          output_node_names="network/output_node",
                          restore_op_name="save/restore_all",
                          filename_tensor_name="save/Const:0",
                          output_graph=output_frozen_graph_name,
                          clear_devices=True,
                          initializer_nodes="")

However, the graph has two notable input nodes, namely "input/is_training" and "input/input_node".

I would like to export this frozen graph to tflite format, but in doing so I need to fix is_training to False (since it is used for tf.layers.batch_normalization).

I am aware that setting the is_training placeholder to False will fix this but assuming I just have the frozen graph file and checkpoint, how would I go about doing this? or is that not possible?

1 Answers

You can do that by just loading the frozen graph, mapping the value in question to a constant and saving the graph again.

import tensorflow as tf

with tf.Graph().as_default():
    # Make constant False value (name does not need to match)
    is_training = tf.constant(False, dtype=tf.bool, name="input/is_training")
    # Load frozen graph
    gd = tf.GraphDef()
    with open(f"{save_graph_path}/graph.pbtxt", "r") as f:
        gd.ParseFromString(f.read())
    # Load graph mapping placeholder to constant
    tf.import_graph_def(gd, name="", input_map={"input/is_training:0": is_training})
    # Save graph again
    tf.train.write_graph(tf.get_default_graph(), save_graph_path, "graph_modified.pbtxt",
                         as_text=True)
Related