TFLite 模型无法在 android studio 中加载

问题描述 投票:0回答:1

我正在张量流版本 2.17.0-nightly 上构建和训练神经网络模型。之后我将其转换为 tflite 模型。 但是当我尝试加载模型时,我收到以下消息:

Didn't find op for builtin opcode 'FULLY_CONNECTED' version '12'. An older version of this builtin might be supported. Are you using an old TFLite binary with a newer model?
型号说明:

self.model = tf.keras.models.Sequential([
            tf.keras.layers.LSTM(80, input_shape=input_shape, return_sequences=True),
            tf.keras.layers.LSTM(128, activation='tanh', return_sequences=False),
            tf.keras.layers.Dense(80, activation='relu'),
            tf.keras.layers.Dense(64, activation='relu'),
            tf.keras.layers.Dense(32, activation='relu'),
            tf.keras.layers.Dense(10, activation='sigmoid'),
            tf.keras.layers.Dense(4, activation='sigmoid')
        ])

模型转换代码:

lstm_model = tf.keras.models.load_model('lstm_model_TEST.keras', custom_objects={'F1_score': F1_score})

# Convert the model.
run_model = tf.function(lambda x: lstm_model(x))

BATCH_SIZE = 1
STEPS = 10
INPUT_SIZE = 5

concrete_func = run_model.get_concrete_function(
    tf.TensorSpec([BATCH_SIZE, STEPS, INPUT_SIZE], lstm_model.inputs[0].dtype))

converter = tf.lite.TFLiteConverter.from_keras_model(lstm_model)

converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [
    tf.lite.OpsSet.TFLITE_BUILTINS,
    tf.lite.OpsSet.SELECT_TF_OPS
]
converter.experimental_new_converter = True

tflite_model = converter.convert()

# Save the model.
with open('model_LSTM.tflite', 'wb') as f:
    f.write(tflite_model)

tf.__version__ = 2.17.0-dev20240514

Android Studio 中的依赖项:

 implementation("org.tensorflow:tensorflow-lite-support:0.4.4")
 implementation("org.tensorflow:tensorflow-lite-metadata:0.4.4")
 implementation("org.tensorflow:tensorflow-lite:2.16.1")
 implementation("org.tensorflow:tensorflow-lite-gpu:2.16.1")

模型下载代码:

val model = ModelLstm.newInstance(this)

我尝试使用tensorflow版本2.16.1转换没有tf-nightly的模型,但模型没有转换

python kotlin android-studio tensorflow tensorflow-lite
1个回答
0
投票

最近我也遇到了同样的问题。分享可能会有所帮助,所以我在下面发布了一些 Colab 代码。我目前在 Google Colab 和 Android Studio 上都使用了 Tensorflow 2.15.0。需要以下代码才能成功将 Tensorflow 降级到 2.15.0。至少在我的环境中是需要的。

!pip install orbax-checkpoint==0.4.0
!pip install tensorstore==0.1.40
!pip install tf-keras==2.15.0
!pip install tensorflow==2.15.0
!pip install ml-dtypes==0.2.0

验证 Tensorflow 版本:

print(tf.__version__)

转换模型:

# Convert Keras model to TF Lite format.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_float_model = converter.convert()

# Show model size in KBs.
float_model_size = len(tflite_float_model) / 1024
print('Float model size = %dKBs.' % float_model_size)

# Re-convert the model to TF Lite using quantization.
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_quantized_model = converter.convert()

# Show model size in KBs.
quantized_model_size = len(tflite_quantized_model) / 1024
print('Quantized model size = %dKBs,' % quantized_model_size)
print('which is about %d%% of the float model size.'\
      % (quantized_model_size * 100 / float_model_size))

下载模型:

# Save the quantized model to file to the Downloads directory
f = open('hasyv2.tflite', "wb")
f.write(tflite_quantized_model)
f.close()

# Download the digit classification model
files.download('hasyv2.tflite')

print('`hasyv2.tflite` has been downloaded')
© www.soinside.com 2019 - 2024. All rights reserved.