keras(.h5)模型到tensorflow-lite(.tflite)模型转换

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

我正在尝试将 .h5 keras 模型转换为 .tflite 模型。但转换会导致核心转储错误。这是我正在运行的脚本,

import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense

# Create a simple Keras model
model = Sequential([
    Dense(64, activation='relu', input_shape=(784,)),
    Dense(64, activation='relu'),
    Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Convert the Keras model to TensorFlow Lite model
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

# Save the TensorFlow Lite model to a file
with open('model.tflite', 'wb') as f:
    f.write(tflite_model)

print("TensorFlow Lite model saved successfully!")

如果我运行脚本,我会收到此错误

2024-04-01 12:27:04.793910: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:268] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
loc(fused["ReadVariableOp:", callsite("sequential_1/dense_1/Add/ReadVariableOp@__inference_serving_default_98"
callsite("/home/spoon/Documents/GTSRB/lib/python3.9/site-packages/keras/src/ops/numpy.py":311:1 at callsite("/home/spoon/Documents/GTSRB/lib/python3.9/site-packages/keras/src/backend/tensorflow/sparse.py":491:1 at callsite("/home/spoon/Documents/GTSRB/lib/python3.9/site-packages/keras/src/backend/tensorflow/numpy.py":35:1 at "/home/spoon/Documents/GTSRB/lib/python3.9/site-packages/keras/src/backend/tensorflow/core.py":64:1)))))))))))))))))))))))))))]): error: missing attribute 'value'
LLVM ERROR: Failed to infer result type(s).
Aborted (core dumped)

操作系统:Ubuntu 20.04.6 LTS

Python版本:3.9.18

点冻结信息:

keras==3.1.1
keras-core==0.1.7
keras-cv==0.8.2
tensorboard==2.16.2
tensorboard-data-server==0.7.2
tensorflow==2.16.1
tensorflow-datasets==4.9.3
tensorflow-io-gcs-filesystem==0.36.0
tensorflow-metadata==1.14.0
tensorflow keras tensorflow-lite tflite
1个回答
0
投票

我最终做的是使用 keras 2 而不是 keras 1,这似乎解决了我的问题。不知道你的问题和我的有多么相似。

© www.soinside.com 2019 - 2024. All rights reserved.