【问题标题】:How do I load a keras model for conversion to tf lite? Official API returns an error如何加载 keras 模型以转换为 tf lite?官方API返回错误
【发布时间】:2020-11-03 04:36:29
【问题描述】:
!pip install tensorflow==2.2
import tensorflow as tf
converter = tf.compat.v1.lite.TFLiteConverter.from_keras_model( 'short-train.h5' ) # Your model's name
model = converter.convert()
file = open( 'model.tflite' , 'wb' ) 
file.write( model )

是我正在运行的代码。我收到以下错误:


AttributeError                            Traceback (most recent call last)
<ipython-input-8-38aae5855f2c> in <module>
      1 get_ipython().system('pip install tensorflow==2.2')
      2 import tensorflow as tf
----> 3 converter = tf.compat.v1.lite.TFLiteConverter.from_keras_model( 'short-train.h5' ) # Your model's name
      4 model = converter.convert()
      5 file = open( 'model.tflite' , 'wb' )

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

这是https://www.tensorflow.org/api_docs/python/tf/lite/TFLiteConverter给我的代码

这是 tensorflow 给出的确切语法。这里有什么问题?


编辑:我现在按照建议运行此代码:

import tensorflow as tf

# WHOLE MODEL
model = r'C:\Users\Owner\Anaconda3\face-mask-detector\mask_detector.model'

tflite_model = tf.keras.models.load_model(model)
converter = tf.lite.TFLiteConverter.from_keras_model(tflite_model)
tflite_save = converter.convert()
open("short-train.tflite", "wb").write(tflite_save)

但是我收到了这个错误:

ValueError: Unknown layer: Functional #after 尝试加载模型

我找不到有关此错误的任何有用信息。感谢您的宝贵时间

【问题讨论】:

    标签: python tensorflow keras tensorflow-lite h5py


    【解决方案1】:

    简而言之,改from_keras_model => from_keras_model_file

    详情

    如果您使用 tensorflow v2,转换器 from_keras_model 可以在 tf.lite.TFLiteConverter.from_keras_model 中找到,但它是用于加载模型而不是您显示的路径。 但是,要与 tf1.x 合并,您可以使用 tf.compat.v1 激活旧版本,就像您所做的那样。但在 tf1.x 中,您必须将 model_path_file 包含为 from_keras_model_file 而不是加载的模型

    我看到您的问题,因为您自己创建了一个功能层,这不是 Keras 内置模型。你必须解决这个问题:

    model = create_empty_model() #your model definition from keras that you create when you train
    model.load_weights(".../model.h5",compile=False)
    converter = tf.lite.TFLiteConverter.from_keras_model(tflite_model)
    tflite_save = converter.convert()
    open("short-train.tflite", "wb").write(tflite_save)
    

    【讨论】:

    • 感谢您的回复。我尝试使用 from_keras_model_file 但我再次遇到错误。我确定我使用的是 tf==2.2。错误:ValueError:未知层:功能
    • Keras 的功能层是表示模型、损失、...的层,如果您在 2.2 上训练它并在 1.x 上激活转换器为compat.v1,则不适合。因此,尝试使用tf.lite.TFLiteConverter.from_keras_model 加载。正如我所说,您必须在此方法中输入一个模型/不是目录的路径字符串。
    • 你必须 model = load_model(path_to_model),然后是 tf.lite.TFLiteConverter.from_keras_model(model)
    • 我刚刚使用这个对原始问题进行了编辑
    【解决方案2】:

    确保创建模型的 tensorflow 版本与加载 tensorflow 模型的版本相同。愚蠢的错误需要很多时间来纠正,但有时解决方法很简单。

    【讨论】:

      【解决方案3】:

      您使用的是 tensorflow v2,因此您应该尝试 tf.lite.TFLiteConverter.from_keras_model 而不是 tf.compat.v1.lite.TFLiteConverter.from_keras_model 我没有足够的声誉来评论抱歉

      【讨论】:

      • 感谢您的回复。我尝试了你的方法并得到了这个错误:AttributeError:'str' object has no attribute 'call'
      • 正如@dtlam26 在对他的回答的评论中所说,参数必须是模型的路径,而不是.h5
      猜你喜欢
      • 1970-01-01
      • 2019-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      • 2021-08-05
      • 1970-01-01
      相关资源
      最近更新 更多