Convert .h5 Keras model into the .tflite model

Hi readers,

Due to TensorFlow versions compatibility issues, you might face issues in converting .h5 Keras model into the .tflite model. I have found a workaround for this and going to share it with you.

Step 1: Uninstall the current version of TensorFlow, by typing below command:

pip uninstall tensorflow

If you are using Google Colab, use

!pip uninstall tensorflow

Step 2: Install TensorFlow version 1.12, by typing below command:

pip install tensorflow==1.12

If you are using Google Colab, use

!pip install tensorflow==1.12

Step 3: Add the import statement

from tensorflow.contrib import lite

Step 4: Code snippet for model conversion

converter = lite.TFLiteConverter.from_keras_model_file('/content/my_model.h5')

tfmodel = converter.convert()

open("model.tflite", "wb").write(tfmodel)

And that's it. You can now use the generated model.tflite file to perform the inferences.

Thanks for reading. Please leave a comment if you found this useful.

Buy Me a Coffee -