问题描述
我正在尝试在Android上运行车牌检测.所以首先我找到了本教程: https://medium.com/@quangnhatnguyenle/detect-and-recognize-vehicles-license-plate-with-machine-learning-and-python-part-1-detection-795fda47e922 真的很棒.
I'm trying to run license plate detection on Android. So first of all I find this tutorial: https://medium.com/@quangnhatnguyenle/detect-and-recognize-vehicles-license-plate-with-machine-learning-and-python-part-1-detection-795fda47e922 which is really great by the way.
在本教程中,我们可以找到 wpod-net.h5
,因此我尝试使用以下命令将其转换为TensorFlow lite:
In the tutorial, we can find wpod-net.h5
so I tried to convert it to TensorFlow lite using the following :
import tensorflow as tf
model = tf.keras.models.load_model('wpod-net.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.post_training_quantize = True
tflite_model = converter.convert()
open("wpod-net.tflite", "wb").write(tflite_model)
但是当我运行它时,我出现了这个错误:
But when I run this I have this error :
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
,但它给了我同样的错误.
I also tried using the API tflite_convert --keras_model_file=License_character_recognition.h5 --output_file=test.tflite
but it gave me the same error.
这是否意味着如果我自己不训练模型,就无法将其转换为tflite吗?还是有另一种方法来转换.h5?
Does that mean that if I didn't train the model myself I can't convert it to tflite ? Or is there another way to convert the .h5?
推荐答案
TensorFlow Lite模型结合了权重和模型代码本身.您需要加载Keras模型(带有权重),然后才能转换为tflite模型.
TensorFlow Lite model incorporates both weights and model code itself. You need to load Keras model(with weights) and then you will be able to convert into tflite model.
获取作者的回购的副本,并执行 get-networks.sh .牌照检测器只需要 data/lp-detector/wpod-net_update1.h5
,这样您就可以早些停止下载.
Get a copy of authors' repo, and execute get-networks.sh. You need only data/lp-detector/wpod-net_update1.h5
for license plates detector so you can stop download earlier.
深入研究代码,您可以在 keras utils .
Dive a bit into code and you can find prepared load model function at keras utils.
获得模型对象后,可以将其转换为tflite.
After you get a model object, you can convert it into tflite.
已测试TF2.4的Python3:
Python3, TF2.4 tested:
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)
祝你好运!
这篇关于如何只将h5文件转换为tflite文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!