In Keras, how to get the layer name associated with a "Model" object contained in my model?
Asked Answered
I

5

47

I built a Sequential model with the VGG16 network at the initial base, for example:

from keras.applications import VGG16
conv_base = VGG16(weights='imagenet',
                  # do not include the top, fully-connected Dense layers 
                  include_top=False,
                  input_shape=(150, 150, 3))

from keras import models
from keras import layers

model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
# the 3 corresponds to the three output classes
model.add(layers.Dense(3, activation='sigmoid'))

My model looks like this:

model.summary()

Layer (type)                 Output Shape              Param #   
=================================================================
vgg16 (Model)                (None, 4, 4, 512)         14714688  
_________________________________________________________________
flatten_1 (Flatten)          (None, 8192)              0         
_________________________________________________________________
dense_7 (Dense)              (None, 256)               2097408   
_________________________________________________________________
dense_8 (Dense)              (None, 3)                 771       
=================================================================
Total params: 16,812,867
Trainable params: 16,812,867
Non-trainable params: 0
_________________________________________________________________

Now, I want to get the layer names associated with the vgg16 Model portion of my network. I.e. something like:

layer_name = 'block3_conv1'
filter_index = 0

layer_output = model.get_layer(layer_name).output
loss = K.mean(layer_output[:, :, :, filter_index])

However, since the vgg16 convolutional is shown as a Model and it's layers are not being exposed, I get the error:

ValueError: No such layer: block3_conv1

How do I do this?

Innermost answered 11/5, 2018 at 1:48 Comment(0)
I
54

The key is to first do .get_layer on the Model object, then do another .get_layer on that specifying the specific vgg16 layer, THEN do .output:

layer_output = model.get_layer('vgg16').get_layer('block3_conv1').output

Innermost answered 11/5, 2018 at 4:5 Comment(1)
You can use the layer's summary: model.get_layer('vgg16').summary()Antifreeze
M
32

To get the name of the layer from the VGG16 instance use the following code.

for layer in conv_base.layers:
    print(layer.name)

the name should be the same inside your model. to show this you could do the following.

print([layer.name for layer in model.get_layer('vgg16').layers])

like Ryan showed us. to call the vgg16 layer you must call it from the model first using the get_layer method.

Morley answered 8/11, 2018 at 23:30 Comment(0)
H
16

One can simply store the name of layers in the list for further usage

layer_names=[layer.name for layer in base_model.layers]
Hydria answered 25/7, 2019 at 6:39 Comment(0)
H
6

This worked for me :)

for idx in range(len(model.layers)):
  print(model.get_layer(index = idx).name)
Harken answered 15/5, 2020 at 16:30 Comment(0)
A
2

Use the layer's summary:

model.get_layer('vgg16').summary()
Antifreeze answered 10/5, 2022 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.