Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Keras模型属性错误:';str';对象没有属性';呼叫';_Python 3.x_Keras_Tensorflow2.0_Tf.keras_Tensorflow Lite - Fatal编程技术网

Python 3.x Keras模型属性错误:';str';对象没有属性';呼叫';

Python 3.x Keras模型属性错误:';str';对象没有属性';呼叫';,python-3.x,keras,tensorflow2.0,tf.keras,tensorflow-lite,Python 3.x,Keras,Tensorflow2.0,Tf.keras,Tensorflow Lite,我正在尝试使用以下代码将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 mode

我正在尝试使用以下代码将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)
我在“keras”模型的
行中发现此错误:

---------------------------------------------------------------------------
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'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
2.
3#转换模型。
---->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来自于_-keras_模型(cls,model)
426比零。
427#一旦我们对动态形状有了更好的支持,我们就可以将其删除。
-->428如果不存在(model.call、_def_function.function):
429#Pass`keep_original_batch_size=True`将确保我们获得输入
430#签名,包括用户指定的批次维度。
AttributeError:“str”对象没有属性“call”

我该如何解决这个问题?顺便说一句,我使用的是Google Colab。

我不确定东西在Colab上是如何工作的,但是查看文档,我可以看到它需要一个Keras模型实例作为参数,但您给它一个字符串。也许你需要先去

比如:

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

当我现在运行它时,它是根据

工作的,我得到了
ValueError:None仅在第一维中受支持。张量“input_1”的形状“[None,None,None,3]”无效。
对于
tfmodel=converter.convert()
import tensorflow as tf
model=tf.keras.models.load_model(""/content/best_model_11class.hdf5"")
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.experimental_new_converter = True
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)