简体   繁体   English

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

[英]Error when trying to rename a pretrained model on tf.keras

I trained two models in order to ensemble them, when I try to load them with this code:当我尝试使用以下代码加载它们时,我训练了两个模型以将它们组合起来:

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

error occur :发生错误:

  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.

full error message:完整的错误信息:

---------------------------------------------------------------------------
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.

根据StackOverflow上的这个问题,您需要使用:

modelTemp._name = 'inception'

I have answered the same topic here How to rename Pre-Trained model ?我在这里回答了相同的主题如何重命名预训练模型? ValueError 'Trained Model' is not a valid scope name ValueError 'Trained Model' 不是有效的范围名称

Solution is:解决办法是:

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM