'Proper way of converting Keras LSTM model into TFLite for Android

I have created a simple LSTM model that I want to use in my Android application. I converted it into TFLite model and saved it in a .tflite file. I imported the model into my project accoring to this guide and everything seems okay - Android Studio shows me the sample codes I should use when I want to use it, except when I attempt to, I get this error when building:

error: package com.example.app.ml does not exist import com.example.app.ml.SnoreModel

What I found out is that when I use a model that does not contain a LSTM layer, it works fine. I tried two different ways of converting my Keras model into TFLite, one was from saved model (as shown bellow) and the other was from loaded model. In both cases I had to set supported_ops and _experimental_lower_tensor_list_ops = false otherwise the conversion would fail.

    TFLITE_SAVE_FILE = "SnoreModel.tflite"
    converter = lite.TFLiteConverter.from_saved_model(filepath)
    converter.target_spec.supported_ops = [lite.OpsSet.TFLITE_BUILTINS, lite.OpsSet.SELECT_TF_OPS] 
    converter._experimental_lower_tensor_list_ops = False

    tflite_model = converter.convert()
    with open(TFLITE_SAVE_FILE, 'wb') as tflite_model_file:
        tflite_model_file.write(tflite_model)

The keras model was saved like so:

    model.save(KERAS_SAVE_FILE)

I am using the latest version of Android Studio and am compiling for min Android API 27. In build.gradle for the application I use the following for tflite, the error was the same whether I used org.tensorflow:tensorflow-lite:2.7.0 or not, the rest was automatically added by Android Studio when I imported the model:

    implementation 'org.tensorflow:tensorflow-lite:2.7.0'
    implementation 'org.tensorflow:tensorflow-lite-metadata:0.3.0'
    implementation 'org.tensorflow:tensorflow-lite-task-vision:0.3.0'
    implementation 'org.tensorflow:tensorflow-lite-task-text:0.3.0'
    implementation 'org.tensorflow:tensorflow-lite-task-audio:0.3.0'
    implementation 'org.tensorflow:tensorflow-lite-support:0.3.0'

What I would like to ask is: Is there something wrong with the way I convert my model? And if yes, how would I fix it, since most of the issues I found were fairly old and were linked to Android Studio version. Thanks in advance



Solution 1:[1]

The error message doesn't show any details on reason for failure or if it is even related to TFLite.

One thing missing is if you are converting your model with SELECT_TF_OPS and the model has some Select TF ops, then you will need to add dependency like

implementation 'org.tensorflow:tensorflow-lite-select-tf-ops:0.0.0-nightly-SNAPSHOT'

See more information here to be able to cut the binary size.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Karim Nosseir