为了账号安全,请及时绑定邮箱和手机立即绑定

如何将 h5 文件转换为 tflite 文件?

如何将 h5 文件转换为 tflite 文件?

拉风的咖菲猫 2023-06-27 13:21:51
我正在尝试在 Android 上运行车牌检测。可以找到wpod-net.h5所以我尝试使用以下命令将其转换为 TensorFlow lite:import tensorflow as tfmodel = tf.keras.models.load_model('wpod-net.h5')converter = tf.lite.TFLiteConverter.from_keras_model(model)converter.post_training_quantize = Truetflite_model = converter.convert()open("wpod-net.tflite", "wb").write(tflite_model)但是当我运行这个时,我遇到了这个错误:  File "converter.py", line 3, in <module>    model = tf.keras.models.load_model('License_character_recognition.h5')  File "/home/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/save.py", line 184, in load_model    return hdf5_format.load_model_from_hdf5(filepath, custom_objects,  File "/home/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 175, in load_model_from_hdf5    raise ValueError('No model found in config file.')ValueError: No model found in config file.我也尝试过使用 API tflite_convert --keras_model_file=License_character_recognition.h5 --output_file=test.tflite ,但它给了我同样的错误。这是否意味着如果我自己没有训练模型,我无法将其转换为 tflite ?或者还有其他方法来转换 .h5 吗?
查看完整描述

1 回答

?
HUWWW

TA贡献1874条经验 获得超12个赞

TensorFlow Lite 模型包含权重和模型代码本身。您需要加载 Keras 模型(带权重),然后您将能够转换为 tflite 模型。

获取作者repo的副本,并执行get-networks.sh。您只需要data/lp-detector/wpod-net_update1.h5车牌检测器,这样您就可以提前停止下载。

深入研究代码,您可以在keras utils找到准备好的负载模型函数。

获得模型对象后,可以将其转换为 tflite。

Python3、TF2.4测试:

import sys, os

import tensorflow as tf

import traceback


from os.path                    import splitext, basename


print(tf.__version__)


mod_path = "data/lp-detector/wpod-net_update1.h5"


def load_model(path,custom_objects={},verbose=0):

    #from tf.keras.models import model_from_json


    path = splitext(path)[0]

    with open('%s.json' % path,'r') as json_file:

        model_json = json_file.read()

    model = tf.keras.models.model_from_json(model_json, custom_objects=custom_objects)

    model.load_weights('%s.h5' % path)

    if verbose: print('Loaded from %s' % path)

    return model


keras_mod = load_model(mod_path)


converter = tf.lite.TFLiteConverter.from_keras_model(keras_mod)

tflite_model = converter.convert()


# Save the TF Lite model.

with tf.io.gfile.GFile('model.tflite', 'wb') as f:

    f.write(tflite_model)

祝你好运!


查看完整回答
反对 回复 2023-06-27
  • 1 回答
  • 0 关注
  • 220 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号