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.
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.
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.
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.
According to this question here on StackOverflow you need to use:
modelTemp._name = 'inception'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With