【问题标题】:When trying to convert Keras .h5 file to .tflite file: Converter gives AttributeError: 'str' object has no attribute 'call'尝试将 Keras .h5 文件转换为 .tflite 文件时:转换器给出 AttributeError: 'str' object has no attribute 'call'
【发布时间】:2020-10-10 18:57:11
【问题描述】:

“tflite_model = converter.convert()”行给出了 AttributeError: 'str' object has no attribute 'call'。

查看代码截图->1

代码:

import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model('///Users/theunskuhn/Desktop/Savedfile/basic_malaria_pos_neg_v3.h5')
converter.experimental_new_converter = True
tflite_model = converter.convert()
open("basic_malaria_pos_neg_v3.tflite", "wb").write(tflite_model)  

错误: AttributeError: 'str' 对象没有属性 'call'

错误指向第 4 行:“tflite_model = converter.convert()”

Screenshot of new code from answer below

【问题讨论】:

    标签: tensorflow keras converters tensorflow-lite


    【解决方案1】:

    如果您在 TensorFlow 2.0 或更高版本中使用 TFLiteConverter API,TFLiteConverter.from_keras_model 会接受 Keras Model 对象,而不是模型的路径 str

    首先,使用tf.keras.models.load_model() 加载模型,然后将此模型传递给TFLiteConverter API。

    import tensorflow as tf
    
    model = tf.keras.models.load_model( '///Users/theunskuhn/Desktop/Savedfile/basic_malaria_pos_neg_v3.h5' )
    
    converter = tf.lite.TFLiteConverter.from_keras_model( model )
    tflite_model = converter.convert()
    open("basic_malaria_pos_neg_v3.tflite", "wb").write(tflite_model)  
    

    方法 TFLiteConverter.from_keras_model_file() 在 TF 2.0 中被 TFLiteConverter.from_keras_model() 取代。请参阅docs

    【讨论】:

    • 我试过了,但 TFLiteConverter.from_keras_model_file() 需要 .pbtxt 或 .pb 文件而不是 .h5 文件:{saved_model.pbtxt|saved_model.pb}
    • 你试过上面的代码了吗?上面的代码是否给出了这个信息?
    • 是的,我尝试了上面的代码,这就是消息。我将在原始问题中添加屏幕截图。
    • 好像你的路径不正确。试试这个:import os , my_model_path = os.path.join(os.path.sep, "Users", "theunskuhn", "Desktop", "SavedFile", "basic_malaria_pos_neg_v3.h5") ant 然后用model = tf.keras.models.load_model(my_model_path) 加载你的模型并仔细检查路径是否存在。
    猜你喜欢
    • 2020-11-28
    • 2019-04-14
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 2021-06-24
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多