【问题标题】:Converting h5 to tflite [closed]将 h5 转换为 tflite [关闭]
【发布时间】:2022-12-22 02:02:34
【问题描述】:
标签:
tensorflow
tensorflow-lite
huggingface-transformers
text-classification
huggingface
【解决方案1】:
[将 TFLite 从保存的 .h5 模型转换为 TFLite 模型]
转换使用tflite convert有多种方式
- TF-Lite 转换器TF-Lite convertor
- TF.Lite.TFLiteConverter 或其他
从目前提供的链接中,他们尝试将保存的模型 .h5 转换为 TFLite,以确认他们的问题。
[ 样本 ]:
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape=( 32, 32, 3 )),
tf.keras.layers.Dense(128, activation='relu'),
])
model.compile(optimizer='sgd', loss='mean_squared_error') # compile the model
model.summary()
model.save_weights(checkpoint_path)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: FileWriter
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if exists(checkpoint_path) :
model.load_weights(checkpoint_path)
print("model load: " + checkpoint_path)
tf_lite_model_converter = tf.lite.TFLiteConverter.from_keras_model(
model
) # <tensorflow.lite.python.lite.TFLiteKerasModelConverterV2 object at 0x0000021095194E80>
tflite_model = tf_lite_model_converter.convert()
# Save the model.
with open(checkpoint_dir + '\model.tflite', 'wb') as f:
f.write(tflite_model)