【问题标题】:Unable to convert a .h5 fileto a tflite model无法将 .h5 文件转换为 tflite 模型
【发布时间】:2020-03-13 18:27:51
【问题描述】:

我已经训练了一个模型并将其保存为 h5 文件。由于我想在我的 android 应用程序中使用它,我希望将它转换为 Colab 上的 tflite。这是我的代码:

import tensorflow as tf
model = tf.keras.models.load_model('Final_model.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

我得到的错误:

AttributeError: type object 'TFLiteConverter' has no attribute 'from_keras_model'

我该如何解决这个问题?

【问题讨论】:

  • 您好,欢迎来到StackOverflow!你的tensorflow 是什么版本?您只需print(tf.__version__) 即可轻松查看。
  • @SzymonMaszke 谢谢!我的版本是 1.15.0

标签: tensorflow machine-learning keras tensorflow2.0


【解决方案1】:

您使用的是colab's 默认tensorflow,即1.15.0

您可以通过从 colab 的单元格发出来下载最新版本:

!pip install --upgrade pip && pip install tensorflow.

您可能需要事先通过此命令卸载tensorflow 1.15.0 版:

!pip uninstall tensorflow (or tensorflow-gpu).

在那之后你应该可以正常运行tf2.x 代码了。

注意:很快 TF2.0 将成为默认版本,此解决方法是临时的。

【讨论】:

    【解决方案2】:

    您使用的是 Tensorflow 1.x,在这种情况下,它有一点 different API。你应该使用from_keras_model_file,即:

    converter = tf.lite.TFLiteConverter.from_keras_model_file('Final_model.h5')
    

    【讨论】:

    • 这是我得到的错误:TypeError: expected str, bytes or os.PathLike object, not Sequential
    • @ShlokKhemani 那是因为您应该提供模型的路径而不是模型本身。我编辑了答案以确保它清楚。
    猜你喜欢
    • 2020-10-10
    • 2020-09-10
    • 2019-07-21
    • 2019-04-14
    • 2020-12-15
    • 2022-12-22
    • 2021-12-09
    • 2019-06-10
    • 2021-02-13
    相关资源
    最近更新 更多