Reputation: 464
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.
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.
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.
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?
Upvotes: 1
Views: 3523
Reputation: 861
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 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.
Dive a bit into code and you can find prepared load model function at keras utils.
After you get a model object, you can convert it into tflite.
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)
Good luck!
Upvotes: 3