python - 尝试重命名 tf.keras 上的预训练模型时出错

标签 python tensorflow keras deep-learning conv-neural-network

我训练了两个模型来集成它们, 当我尝试使用此代码加载它们时:

  from tensorflow.keras.models import load_model
  models=[]
  modelTemp=load_model('models/full.h5')
  modelTemp.name = "inception1"
  models.append(modelTemp)

发生错误:

  AttributeError: Can't set the attribute "name", likely because it conflicts with an existing read-only @property of the object. Please choose a different name.

完整错误信息:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __setattr__(self, name, value)
   1968       try:
-> 1969         super(tracking.AutoTrackable, self).__setattr__(name, value)
   1970       except AttributeError:

AttributeError: can't set attribute

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __setattr__(self, name, value)
   1972             ('Can\'t set the attribute "{}", likely because it conflicts with '
   1973              'an existing read-only @property of the object. Please choose a '
-> 1974              'different name.').format(name))
   1975       return
   1976 

AttributeError: Can't set the attribute "name", likely because it conflicts with an existing read-only @property of the object. Please choose a different name.

最佳答案

根据 this question here on StackOverflow 你需要使用:

modelTemp._name = 'inception'

关于python - 尝试重命名 tf.keras 上的预训练模型时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56886442/

相关文章:

python - python中的密码求解器循环计数

python - 将组中项目的长度/数量分配给新列

python - 获取 "Message: h is null"

python - 如何以 python 字符串格式传递整个列表?

python - 为什么 plt.imshow() 不显示图像?

python - 不同 GPU 上的 Tensorflow 执行和内存

python - 在训练期间固定神经网络中的一部分权重

tensorflow - 如何在for循环中创建张量列表并在tensorflow2中使用tf.stack

python - 有什么方法可以优化 keras 中大输入的内存使用情况?

tensorflow - 在 TensorFlow/Keras 中获取中间层的输出