【问题标题】:Error when trying to rename a pretrained model on tf.keras尝试在 tf.keras 上重命名预训练模型时出错
【发布时间】:2019-11-15 02:34:22
【问题描述】:

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

  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.

【问题讨论】:

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


【解决方案1】:

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

modelTemp._name = 'inception'

【讨论】:

  • 这应该是公认的答案。显然他们没有包含名称设置器装饰器,必须手动更改内部属性
  • 这是正确的方法吗?弄乱“_name”似乎很危险。也许 tensorflow 团队这样做是有原因的。
【解决方案2】:

我在这里回答了同样的话题How to rename Pre-Trained model ? ValueError 'Trained Model' is not a valid scope name

解决办法是:

model = load_model(r"C:\Master\Learning\Agri_Intelligence\Models\Model.h5")
model._name = "New_Model_Name"
model.save(r"C:\Master\Learning\Agri_Intelligence\Models\New_Model.h5")

【讨论】:

  • 你自己的问题(你链接到这里)和这个问题一样吗?如果是这样,那么在这里添加您的详细答案而不是发布类似的问题会更合适。
  • @FluffyKitten 是的,没错!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
  • 2020-10-02
  • 1970-01-01
  • 2022-12-04
相关资源
最近更新 更多