Wednesday, November 13, 2019

Convert frozen graph to tensorflow lite model

import tensorflow as tf

convert=tf.lite.TFLiteConverter.from_frozen_graph(
    "frozen.pb",
    input_arrays=["train_x"],
    output_arrays=["output"])
convert.post_training_quantize=True
tflite_model=convert.convert()
open("model.tflite","wb").write(tflite_model)

#If you need to specify the shape of the input
import tensorflow as tf
path="./fullLayer/"

convert=tf.lite.TFLiteConverter.from_frozen_graph(
    path+"frozen.pb",
    input_arrays=["images"],output_arrays=["output"], 
    input_shapes={"images":[1,540,960,1]})

convert.post_training_quantize=True
tflite_model=convert.convert()
open(path+"quantized_model.tflite","wb").write(tflite_model)
print("finish!")

Convert CKPT to frozen graph pb

Convert checkpoint ckpt to frozen graph pb file

import tensorflow as tf


def freeze_graph(input_checkpoint, output_graph):
    output_node_names = "output"
    saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=True)
    graph = tf.get_default_graph() 
    input_graph_def = graph.as_graph_def()

    with tf.Session() as sess:
        saver.restore(sess, input_checkpoint)
        output_graph_def = tf.graph_util.convert_variables_to_constants(
            sess=sess,
            input_graph_def=input_graph_def,
            output_node_names=output_node_names.split(","))

        with tf.gfile.GFile(output_graph, "wb") as f:
            f.write(output_graph_def.SerializeToString())
        # print("%d ops in the final graph." % len(output_graph_def.node))

if __name__ == '__main__':
    modelpath="./checkPointModel/model.ckpt"
    freeze_graph(modelpath,"frozen_graph.pb")
    print("finish!")

Convert frozen graph to tensorflow lite model

import tensorflow as tf convert=tf.lite.TFLiteConverter.from_frozen_graph(     "frozen.pb",     input_arrays=["train_x...