Keras: How to get layer shapes in a Sequential model
Asked Answered
M

3

44

I would like to access the layer size of all the layers in a Sequential Keras model. My code:

model = Sequential()
model.add(Conv2D(filters=32, 
               kernel_size=(3,3), 
               input_shape=(64,64,3)
        ))
model.add(MaxPooling2D(pool_size=(3,3), strides=(2,2)))

Then I would like some code like the following to work

for layer in model.layers:
    print(layer.get_shape())

.. but it doesn't. I get the error: AttributeError: 'Conv2D' object has no attribute 'get_shape'

Midshipman answered 2/5, 2017 at 17:8 Comment(0)
E
43

According to official doc for Keras Layer, one can access layer output/input shape via layer.output_shape or layer.input_shape.

from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D


model = Sequential(layers=[
    Conv2D(32, (3, 3), input_shape=(64, 64, 3)),
    MaxPool2D(pool_size=(3, 3), strides=(2, 2))
])

for layer in model.layers:
    print(layer.output_shape)

# Output
# (None, 62, 62, 32)
# (None, 30, 30, 32)
Especially answered 24/1, 2019 at 1:45 Comment(2)
"AttributeError: The layer has never been called and thus has no defined output shape"Barlow
@Barlow make sure to define the inpute_shape of the first layer correctly. You can check this by calling model.build() first.Avent
M
44

If you want the output printed in a fancy way:

model.summary()

If you want the sizes in an accessible form

for layer in model.layers:
    print(layer.get_output_at(0).get_shape().as_list())

There are probably better ways to access the shapes than this. Thanks to Daniel for the inspiration.

Midshipman answered 2/5, 2017 at 19:52 Comment(0)
E
43

According to official doc for Keras Layer, one can access layer output/input shape via layer.output_shape or layer.input_shape.

from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D


model = Sequential(layers=[
    Conv2D(32, (3, 3), input_shape=(64, 64, 3)),
    MaxPool2D(pool_size=(3, 3), strides=(2, 2))
])

for layer in model.layers:
    print(layer.output_shape)

# Output
# (None, 62, 62, 32)
# (None, 30, 30, 32)
Especially answered 24/1, 2019 at 1:45 Comment(2)
"AttributeError: The layer has never been called and thus has no defined output shape"Barlow
@Barlow make sure to define the inpute_shape of the first layer correctly. You can check this by calling model.build() first.Avent
U
18

Just use model.summary(), and it will print all layers with their output shapes.


If you need them as arrays, tuples or etc, you can try:

for l in model.layers:
    print (l.output_shape)

For layers that are used more than once, they contain "multiple inbound nodes", and you should get each output shape separately:

if isinstance(layer.outputs, list):
    for out in layer.outputs:
        print(K.int_shape(out))
            

It will come as a (None, 62, 62, 32) for the first layer. The None is related to the batch_size, and will be defined during training or predicting.

Unperforated answered 2/5, 2017 at 17:15 Comment(3)
model.summary() is a good bet in many cases, but ideally I would like to have the shape as an array or tensorMidshipman
I get the following error (with the update): AttributeError: The layer "max_pooling2d_1 has multiple inbound nodes, with different output shapes. Hence the notion of "output shape" is ill-defined for the layer. Use 'get_output_shape_at(node_index)' instead. I think you have to do the full thing, as in my answerMidshipman
You can K.int_shape(layer.get_output_at(node_index)), but you will need to get outputs at many indicesHandbarrow

© 2022 - 2024 — McMap. All rights reserved.