从 Keras LSTM 模型转换后无法在 Android 中导入 Tensor Flow Lite 模型

Unable to import Tensor flow lite model in Android after converting from Keras LSTM model

我是机器学习和深度学习的新手。我正在尝试通过将 android 应用程序转换为 .tflite 文件来在 android 应用程序中使用 Keras LSTM 模型,但是当我尝试在 android studio 中导入文件时,导入失败并显示消息 'This Tensor flow lite model is invalid'

我使用了张量流网站上提到的转换代码。

解决问题的方法是什么?

正在创建模型:

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: "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

从 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)

正在将其导入 Android

此问题与 Android Studio 无法导入 LSTM 模型有关。

AndroidStudio 4.2 中即将修复。现在,下载测试版,它将导入 LSTM 模型 (tflite)

这是已关闭的问题和我的对话:

https://github.com/tensorflow/tensorflow/issues/47571