【问题标题】:'TFLiteKerasModelConverterV2' object has no attribute 'predict''TFLiteKerasModelConverterV2' 对象没有属性 'predict'
【发布时间】:2022-01-26 03:02:27
【问题描述】:

我正在尝试通过加载我的模型的保存版本来预测值。 这是它的代码-

def classifier(img, weights_file):
    # Load the model

    model = tf.lite.TFLiteConverter.from_keras_model(weights_file)

    # Create the array of the right shape to feed into the keras model
    data = np.ndarray(shape=(1, 200, 200, 3), dtype=np.float32)
    image = img
    # image sizing
    size = (200, 200)
    image = ImageOps.fit(image, size, Image.ANTIALIAS)

    # turn the image into a numpy array
    image_array = np.asarray(image)
    # Normalize the image
    normalized_image_array = image_array.astype(np.float32) / 255

    # Load the image into the array
    data[0] = normalized_image_array

    # run the inference
    prediction_percentage = model.predict(data)
    prediction = prediction_percentage.round()

    return prediction, prediction_percentage

我的模型抛出错误“'TFLiteKerasModelConverterV2'对象没有属性'predict'” 谁能告诉我我可以在这里改变什么?

【问题讨论】:

    标签: python machine-learning keras data-science tensorflow-lite


    【解决方案1】:

    您正在从权重文件创建一个 TFLiteConverter 对象。加载模型权重的正确方法是使用load_weightslink。试试:

    tf.keras.model.load_weights(weights_file)
    

    但是,您首先还需要以与训练模型时相同的方式定义模型。如果您已将模型保存为 SavedModel 格式,请使用

    model = tf.keras.models.load_model(weights_file)
    

    【讨论】:

      猜你喜欢
      • 2020-02-21
      • 1970-01-01
      • 2021-07-24
      • 2022-07-05
      • 1970-01-01
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多