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

Gui*_*ine 15 python deep-learning conv-neural-network keras tensorflow

当我尝试使用以下代码加载它们时,我训练了两个模型以将它们组合起来:

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

发生错误:

  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.
Run Code Online (Sandbox Code Playgroud)

完整的错误信息:

---------------------------------------------------------------------------
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.
Run Code Online (Sandbox Code Playgroud)

gsa*_*dhu 21

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

modelTemp._name = 'inception'
Run Code Online (Sandbox Code Playgroud)