Ошибка атрибута модели Keras: объект 'str' не имеет атрибута 'call' - PullRequest
1 голос
/ 12 июля 2020

Я пытаюсь преобразовать свой файл 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.

1 Ответ

1 голос
/ 12 июля 2020

Я не уверен, как все работает на Colab, но, глядя на документацию для tf.lite.TFLiteConverter.from_keras_model , я вижу, что он ожидает экземпляр модели Keras в качестве аргумента, но вы даете это строка. Может быть, вам сначала нужно загрузить модель Keras ?

Что-то вроде:

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