Converted model (from h5 to tflite) doesn't work with tflite_flutter plugin


I’m new to tensorflow, I created a simple tflite model from Teachable Machine and it worked great in flutter app with tflite_flutter plugin.

Then I had to change the model with a pretrained .h5 model. I converted .h5 model to .tflite model but it crashes.

Converted pretrained model’s input shape is [1, 16, 64, 64, 3](16 frame,64×64 image, 3 color) and output shape is [1, 12]. My test model’s input shape is [1, 224, 224, 3] and output shape is [1, 3].

I created a .h5 model with input shape [1, 224, 224, 3] and converted it to tflite, it worked. Seems like conversion does the job and the problem may be the shape of the model. I couldn’t figure out how to do it.

Here is the code that I used for conversion.

from keras.models import load_model
model = load_model("/content/model.h5")

TF_LITE_MODEL_FILE_NAME = "model.tflite"
tf_lite_converter = tf.lite.TFLiteConverter.from_keras_model(model)
tf_lite_converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tf_lite_converter._experimental_lower_tensor_list_ops = False
tflite_model = tf_lite_converter.convert()
tflite_model_name = TF_LITE_MODEL_FILE_NAME
open(tflite_model_name, "wb").write(tflite_model)

# Convert the model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
converter._experimental_lower_tensor_list_ops = False
tflite_model = converter.convert()


I used convolution layers and lstm layers in training. Some operations in those layers don’t have TensorFlow Lite equivalents and convertion from .h5 to .tflite is not possible without TensorFlow Lite and TensorFlow operator compatibility.

Answered By – murat

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published