4
$\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$
  • $\begingroup$ Have you seen here? $\endgroup$ – Media Nov 7 '18 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 Nov 7 '18 at 23:01
  • $\begingroup$ Thanks @Media, I saw the link, but the problem is little different. $\endgroup$ – N.IT Nov 8 '18 at 8:11
3
$\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

In [9]: for i, layer in enumerate(model.layers):
    ...:     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.

| improve this answer | |
$\endgroup$
  • $\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 Nov 8 '18 at 8:14
0
$\begingroup$

See https://stackoverflow.com/questions/49550182/keras-rename-model-and-layers

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

| improve this answer | |
$\endgroup$
0
$\begingroup$

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

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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