亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 h5 文件轉換為 tflite 文件?

如何將 h5 文件轉換為 tflite 文件?

拉風的咖菲貓 2023-06-27 13:21:51
我正在嘗試在 Android 上運行車牌檢測??梢哉业絯pod-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 關注
  • 260 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號