轉換ckpt爲tflite模型的過程

#正確步驟1
import tensorflow as tf
 
def freeze_graph(input_checkpoint, output_graph):
    output_node_names = "strided_slice_13,strided_slice_23" #獲取的節點
    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,  # 等於:sess.graph_def
            output_node_names=output_node_names.split(","))  # 如果有多個輸出節點,以逗號隔開
 
        with tf.gfile.GFile(output_graph, "w") as f:  # 保存模型
            f.write(output_graph_def.SerializeToString())  # 序列化輸出
#             f.write(output_graph_def.SerializeToOstream())  # 序列化輸出
        # print("%d ops in the final graph." % len(output_graph_def.node))  # 得到當前圖有幾個操作節點
 
if __name__ == '__main__':
    modelpath="./model"
    freeze_graph(modelpath,"frozen.pbtxt")
    print("finish!")

 

#正確步驟2
import tensorflow as tf
convert=tf.lite.TFLiteConverter.from_frozen_graph("frozen.pbtxt",input_arrays=["waveform"],output_arrays=["strided_slice_23"])
convert.post_training_quantize=False
tflite_model=convert.convert()
open("model.tflite","w").write(tflite_model)
 
#當需要給定輸入數據形式時,給出輸入格式:
# import tensorflow as tf
# convert=tf.lite.TFLiteConverter.from_frozen_graph("frozen.pbtxt",input_arrays=["waveform"],output_arrays=["strided_slice_23"],
#                                                   input_shapes={"waveform":[1,2]})
# convert.post_training_quantize=True
# tflite_model=convert.convert()
# open("model.tflite","w").write(tflite_model)
print("finish!")
 

如果轉換失敗,看失敗日誌:

Converting unsupported operation: RFFT

說明有不支持的函數、方法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章