【问题标题】:Can't convert Keras model to tflite无法将 Keras 模型转换为 tflite
【发布时间】:2019-09-05 15:14:19
【问题描述】:

我保存了一个 Keras 模型,其中包含以下行:

tf.keras.models.save_model(model, "path/to/model.h5")

稍后,我尝试将其转换为tflite 文件,如下所示:

converter = tf.contrib.lite.TFLiteConverter.from_keras_model_file('path/to/model.h5')
tflite_model = converter.convert()
open("path/to/model.tflite", "wb").write(tflite_model)

但我得到一个奇怪的错误:

您正在尝试将包含 35 层的权重文件加载到具有 0 层的模型中。

我知道我的模型运行良好。我能够加载它并得出推论。此错误仅在尝试将其保存为 tflite 模型时出现。

TensorFlow 版本:tensorflow-gpu 1.12.0

我正在使用 tf.keras。

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    事实证明,问题是由于明确定义了 InputLayer 和一些 input_shape

    我的模型是这样的:

    InputLayer(input_shape=(...))

    BatchNormalization()

    ....剩余层

    我改成:

    BatchNormalization(input_shape=(...))

    ....剩余层

    并在此处转移先前模型的权重。现在它完美地工作了。

    【讨论】:

      猜你喜欢
      • 2020-09-10
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多