gpt4 book ai didi

python-3.x - Keras 模型属性错误 : 'str' object has no attribute 'call'

转载 作者:行者123 更新时间:2023-12-03 23:05:44 25 4
gpt4 key购买 nike

我正在尝试使用以下代码将我的 Keras hdf5 文件转换为 TensorFlow Lite 文件:

import tensorflow as tf

# Convert the model.
converter = tf.lite.TFLiteConverter.from_keras_model("/content/best_model_11class.hdf5")
tflite_model = converter.convert()

# Save the TF Lite model.
with tf.io.gfile.GFile('model.tflite', 'wb') as f:
f.write(tflite_model)
我在 from_keras_model 上收到此错误线:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-14-26467c686751> in <module>()
2
3 # Convert the model.
----> 4 converter = tf.lite.TFLiteConverter.from_keras_model("/content/best_model_11class.hdf5")
5 tflite_model = converter.convert()
6

/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/lite.py in from_keras_model(cls, model)
426 # to None.
427 # Once we have better support for dynamic shapes, we can remove this.
--> 428 if not isinstance(model.call, _def_function.Function):
429 # Pass `keep_original_batch_size=True` will ensure that we get an input
430 # signature including the batch dimension specified by the user.

AttributeError: 'str' object has no attribute 'call'
我该如何解决?顺便说一下,我正在使用 Google Colab。

最佳答案

我不确定 Colab 上的工作原理,但查看 tf.lite.TFLiteConverter.from_keras_model 的文档我可以看到它需要一个 Keras 模型实例作为参数,但你给它一个字符串。也许您需要load the Keras model第一的?
就像是:

keras_model = tf.keras.models.load_model("/content/best_model_11class.hdf5")
converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)

关于python-3.x - Keras 模型属性错误 : 'str' object has no attribute 'call' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62855617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com