I am new to machine learning and deep learning. I am trying to use Keras LSTM model in an android app by converting it into .tflite file but when i try to import the file in android studio, the import fails with the message that 'This Tensor flow lite model is invalid'
I have used the conversion code mention on tensor flow website.
What is the solution to the problem?
Creating model:
model = keras.Sequential()
model.add(
keras.layers.Bidirectional(
keras.layers.LSTM(
units=128,
input_shape=[X_train.shape[1], X_train.shape[2]]
)
)
)
model.add(keras.layers.Dropout(rate=0.5))
model.add(keras.layers.Dense(units=128, activation='tanh'))
model.add(keras.layers.Dense(y_train.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
Model Summary:
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
bidirectional (Bidirectional (None, 256) 135168
_________________________________________________________________
dropout_3 (Dropout) (None, 256) 0
_________________________________________________________________
dense_7 (Dense) (None, 128) 32896
_________________________________________________________________
dense_8 (Dense) (None, 11) 1419
=================================================================
Total params: 169,483
Trainable params: 169,483
Non-trainable params: 0
Conversion from to tflite:
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
# Save the model.
with open('model_tensor.tflite', 'wb') as f:
f.write(tflite_model)
Importing it in Android
The issue is related to Android Studio unable to import LSTM models.
The fix is coming in Android Studio 4.2. For now, download the beta version and it will import the LSTM model (tflite)
Here is the closed issue and my conversation: