TensorFlow Model Conversion

1. TensorFlow 1.x

1.1 Frozen Graph / Protobuf

# Freeze variables to constants
output_graph_def = tf.graph_util.convert_variables_to_constants(
    sess, 
    tf.get_default_graph().as_graph_def(), 
    ["output_names"]) 

# Serialize and dump the output graph
with tf.gfile.GFile(output_graph, "wb") as f:
    f.write(output_graph_def.SerializeToString())

Reference:

1.2 Keras to Frozen Graph

Convert Keras model (.h5) into frozen graph protobuf (.pb) format.

tf.keras.backend.set_learning_phase(0)  # Disable learning phase
model = tf.keras.models.load_model(model_path)
sess = tf.keras.backend.get_session()
output_nodes = [node.op.name for node in model.outputs]

frozen_graph = freeze_session(sess, output_nodes)
tf.train.write_graph(frozen_graph, "some_directory", "my_model.pb", as_text=False)

Reference:

1.3 Saved Model to Frozen Graph

Convert Saved_Model in TF 1.x and TF 2.x to frozen graph protobuf (.pb):

from tensorflow.python.saved_model import tag_constants
from tensorflow.python.tools import freeze_graph

def freeze_model(saved_model_dir, output_node_names, output_filename):
  output_graph_filename = os.path.join(saved_model_dir, output_filename)
  initializer_nodes = ''
  freeze_graph.freeze_graph(
      input_saved_model_dir=saved_model_dir,
      output_graph=output_graph_filename,
      saved_model_tags = tag_constants.SERVING,
      output_node_names=output_node_names,
      initializer_nodes=initializer_nodes,
      input_graph=None,
      input_saver=False,
      input_binary=False,
      input_checkpoint=None,
      restore_op_name=None,
      filename_tensor_name=None,
      clear_devices=True,
      input_meta_graph=False,
  )

Reference:

For TFLite related conversion and quantization:

https://qiita.com/PINTO/items/865250ee23a15339d556