尝试在 tf.keras 上重命名预训练模型时出错
Posted
技术标签:
【中文标题】尝试在 tf.keras 上重命名预训练模型时出错【英文标题】:Error when trying to rename a pretrained model on 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.
【问题讨论】:
你试过其他名字吗? 是的,我试过了,但没有出现同样的错误 Keras rename model and layers的可能重复 【参考方案1】:根据this question here on *** 你需要使用:
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 是的,没错!以上是关于尝试在 tf.keras 上重命名预训练模型时出错的主要内容,如果未能解决你的问题,请参考以下文章
JSONDecodeError:加载 tf.Keras 模型时的期望值