将data-00000-of-00001文件转换为Tensorflow Lite

问题描述 投票:1回答:1

是否有任何方法可以将convert data-00000-of-00001转换为Tensorflow Lite模型?文件结构是这样的

 |-semantic_model.data-00000-of-00001
 |-semantic_model.index
 |-semantic_model.meta
python tensorflow tensorflow-lite
1个回答
0
投票

使用TensorFlow版本:1.15] >>

下面的两个步骤将其转换为.tflite模型。

1。使用.pb

生成用于推理的TensorFlow模型(冻结图answer posted here文件)

您当前拥有的是模型checkpoint(一个TensorFlow 1模型保存在3个文件中:.data ...,。meta和.index。可以根据需要对该模型进行进一步的训练)。您需要将其转换为frozen graph(一个TensorFlow 1模型保存在单个.pb文件中。该模型无法进一步训练,并且已针对推理/预测进行了优化)。

2。生成TensorFlow精简模型(.tflite文件)

A。初始化TFLiteConverter:可以.from_frozen_graph方式定义this API,可以添加的属性为here。要查找这些数组的名称,请在.pb

中可视化Netron文件
converter = tf.lite.TFLiteConverter.from_frozen_graph(
    graph_def_file='....path/to/frozen_graph.pb', 
    input_arrays=...,
    output_arrays=....,
    input_shapes={'...' : [_, _,....]}
)

B。可选:执行最简单的优化,称为post-training dynamic range quantization。您可以参考同一文档,了解其他类型的优化/量化方法。

converter.optimizations = [tf.lite.Optimize.DEFAULT]

C。将其转换为.tflite文件并保存]

tflite_model = converter.convert()

tflite_model_size = open('model.tflite', 'wb').write(tflite_model)
print('TFLite Model is %d bytes' % tflite_model_size)
    
© www.soinside.com 2019 - 2024. All rights reserved.