微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决从 Keras LSTM 模型转换后无法在 Android 中导入 Tensor flow lite 模型

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

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

问题的解决方案是什么?

创建模型:

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 中导入

enter image description here

解决方法

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

此修复将在 Android Studio 4.2 中推出。现在,下载测试版,它将导入 LSTM 模型 (tflite)

这是已结束的问题和我的对话:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?