Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 225
Guissous Allaeddine Avatar asked Jul 04 '19 10:07

Guissous Allaeddine


People also ask

What is input layer in Keras?

However, there are two options to define the input layer. We can use the InputLayer() class to explicitly define the input layer of a Keras sequential model or we can use the Dense() class with the input_shape argument that will add the input layer behind the scene.

How do I change my model name in Keras?

In order to change the layer name of a pre-trained model on Tensorflow Keras, the solution is a bit more complex. A simple layer.name = "new_name" or layer.

How do I get the layer name in Keras?

Every layer of the Keras model has a unique name. e.g. "dense_1", "dense_2" etc. Keras has a function for getting a layer with this unique name. So you need just to call that function and pass a name for the layer.


1 Answers

According to this question here on StackOverflow you need to use:

modelTemp._name = 'inception'
like image 128
gsandhu Avatar answered Sep 23 '22 17:09

gsandhu