12
$\begingroup$

I am using vgg16 to create a deep learning model. I want to know how to change the names of the layers of deep learning in Keras?

I tried this

for layer in vgg_model.layers:
    layer.name = layer.name + str("_")

But when I change the names of the layers, the model accuracy become low.

$\endgroup$
3
  • $\begingroup$ Have you seen here? $\endgroup$ Commented Nov 7, 2018 at 20:57
  • $\begingroup$ Changing the string name attribute of a layer should not affect the accuracy of a model. They are simply descriptors. $\endgroup$
    – n1k31t4
    Commented Nov 7, 2018 at 23:01
  • $\begingroup$ Thanks @Media, I saw the link, but the problem is little different. $\endgroup$
    – N.IT
    Commented Nov 8, 2018 at 8:11

4 Answers 4

10
$\begingroup$

I made a small example, which basically does the same as your code, and I show that the expected results are obtained:

Imports

In [1]: from keras.models import Sequential
In [2]: from keras.layers import Dense

Build a model

Note the name arguments:

In [3]: model = Sequential()
In [4]: model.add(Dense(50, input_shape=(20, 20), name='dense1'))
In [5]: model.add(Dense(30, name='dense2'))
In [6]: model.add(Dense(1, name='dense3'))
In [7]: model.compile('rmsprop', 'mse')
In [8]: model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense1 (Dense)               (None, 20, 50)            1050      
_________________________________________________________________
dense2 (Dense)               (None, 20, 30)            1530      
_________________________________________________________________
dense3 (Dense)               (None, 20, 1)             31        
=================================================================

Rename the layers

EDIT: it seems newer versions of Keras and the tf.keras API now do not allow renaming layers via layer.name = "new_name". Instead you must assign your new name to the private attribute, layer._name.

In [9]: for i, layer in enumerate(model.layers):
    ...:     # layer.name = 'layer_' + str(i)    <-- old way
    ...:     layer._name = 'layer_' + str(i)
In [10]: model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
layer_0 (Dense)              (None, 20, 50)            1050      
_________________________________________________________________
layer_1 (Dense)              (None, 20, 30)            1530      
_________________________________________________________________
layer_2 (Dense)              (None, 20, 1)             31        
=================================================================

So we see the layers now have the new names!

Further inspection

I can see that the internal identifiers of the layers themselves are not changed. I show the first layer's weights, as we see the original label is still present:

In [11]: a = model.layers[0]
In [12]: a.weights
Out[12]: 
[<tf.Variable 'dense1/kernel:0' shape=(20, 50) dtype=float32_ref>,
 <tf.Variable 'dense1/bias:0' shape=(50,) dtype=float32_ref>]

We see the dense1 name is still visible in the layer container, along with its unchanged shape and datatype.

I also initialised the model weights by running model.get_weights(), then renamed the layers as before to new names, compiled the model and saw that the layers' names were altered as before, and the weights themselves were not changed i.e. reinitialised. The exact same values were contained. This means that renaming your layers cannot be behind the drop in accuracy.

$\endgroup$
1
  • $\begingroup$ I am using vgg16..... In my case, the weights and the structures of the model before and after changing the names are same. But the performance is different. It is strange :/ $\endgroup$
    – N.IT
    Commented Nov 8, 2018 at 8:14
1
$\begingroup$

Maybe it is caused by not correctly load pre-trained weights. Check if you use model.load_weights with by_name=True

$\endgroup$
1
$\begingroup$

See this.

You need to use: layer._name = layer.name + str('asdf')

$\endgroup$
1
$\begingroup$

you can try this code.

def model1_VGG16():
    vgg16 = VGG16(include_top=False, input_shape=(32, 32, 3))
    vgg16._name = 'VGG16_1'
    model = Sequential()  
    model.add(vgg16)
    ...
    return model

def model2_VGG16():
    vgg16 = VGG16(include_top=False, input_shape=(32, 32, 3))
    vgg16._name = 'VGG16_2'
    model = Sequential() 
    model.add(vgg16)
    ...
    return model
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.