Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the output shape of a layer in Keras?

Tags:

I have the following code in Keras (Basically I am modifying this code for my use) and I get this error:

'ValueError: Error when checking target: expected conv3d_3 to have 5 dimensions, but got array with shape (10, 4096)'

Code:

from keras.models import Sequential from keras.layers.convolutional import Conv3D from keras.layers.convolutional_recurrent import ConvLSTM2D from keras.layers.normalization import BatchNormalization import numpy as np import pylab as plt from keras import layers  # We create a layer which take as input movies of shape # (n_frames, width, height, channels) and returns a movie # of identical shape.  model = Sequential() model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),                    input_shape=(None, 64, 64, 1),                    padding='same', return_sequences=True)) model.add(BatchNormalization())  model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),                    padding='same', return_sequences=True)) model.add(BatchNormalization())  model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),                    padding='same', return_sequences=True)) model.add(BatchNormalization())  model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),                    padding='same', return_sequences=True)) model.add(BatchNormalization())  model.add(Conv3D(filters=1, kernel_size=(3, 3, 3),                activation='sigmoid',                padding='same', data_format='channels_last')) model.compile(loss='binary_crossentropy', optimizer='adadelta') 

the data I feed is in the following format: [1, 10, 64, 64, 1]. So I would like to know where I am wrong and also how to see the output_shape of each layer.

like image 827
MRM Avatar asked Mar 28 '18 06:03

MRM


People also ask

How do you specify output shape of a layer in Keras?

You can get the output shape with 'output. shape[1:]' command. It will get the shape of output layer and can be used for other purposes.

What is output shape of dense layer?

The output shape of the Dense layer will be affected by the number of neuron / units specified in the Dense layer. For example, if the input shape is (8,) and number of unit is 16, then the output shape is (16,).

How do you find the input shape of a Keras model?

We can use the “. shape” function to find the shape of the image. It returns a tuple of integers that represent the shape and color mode of the image. In the above output, the first two integer values represent the shape of the image.

What is the shape of the output of the first convolutional layer?

So the output shape of the first Conv layer is (28,28,8). Followed by a max-pooling layer, the method of calculating pooling layer is as same as the Conv layer. The kernel size of max-pooling layer is (2,2) and stride is 2, so output size is (28–2)/2 +1 = 14. After pooling, the output shape is (14,14,8).


Video Answer


1 Answers

You can get the output shape of a layer by layer.output_shape.

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

Gives you:

(None, None, 64, 64, 40) (None, None, 64, 64, 40) (None, None, 64, 64, 40) (None, None, 64, 64, 40) (None, None, 64, 64, 40) (None, None, 64, 64, 40) (None, None, 64, 64, 40) (None, None, 64, 64, 40) (None, None, 64, 64, 1) 

Alternatively you can pretty print the model using model.summary:

model.summary() 

Gives you the details about the number of parameters and output shapes of each layer and an overall model structure in a pretty format:

_________________________________________________________________ Layer (type)                 Output Shape              Param #    ================================================================= conv_lst_m2d_1 (ConvLSTM2D)  (None, None, 64, 64, 40)  59200      _________________________________________________________________ batch_normalization_1 (Batch (None, None, 64, 64, 40)  160        _________________________________________________________________ conv_lst_m2d_2 (ConvLSTM2D)  (None, None, 64, 64, 40)  115360     _________________________________________________________________ batch_normalization_2 (Batch (None, None, 64, 64, 40)  160        _________________________________________________________________ conv_lst_m2d_3 (ConvLSTM2D)  (None, None, 64, 64, 40)  115360     _________________________________________________________________ batch_normalization_3 (Batch (None, None, 64, 64, 40)  160        _________________________________________________________________ conv_lst_m2d_4 (ConvLSTM2D)  (None, None, 64, 64, 40)  115360     _________________________________________________________________ batch_normalization_4 (Batch (None, None, 64, 64, 40)  160        _________________________________________________________________ conv3d_1 (Conv3D)            (None, None, 64, 64, 1)   1081       ================================================================= Total params: 407,001 Trainable params: 406,681 Non-trainable params: 320 _________________________________________________________________ 

If you want to access information about a specific layer only, you can use name argument when constructing that layer and then call like this:

... model.add(ConvLSTM2D(..., name='conv3d_0')) ...  model.get_layer('conv3d_0') 

EDIT: For reference sake it will always be same as layer.output_shape and please don't actually use Lambda or custom layers for this. But you can use Lambda layer to echo the shape of a passing tensor.

... def print_tensor_shape(x):     print(x.shape)     return x model.add(Lambda(print_tensor_shape)) ... 

Or write a custom layer and print the shape of the tensor on call().

class echo_layer(Layer): ...     def call(self, x):         print(x.shape)         return x ...  model.add(echo_layer()) 
like image 139
umutto Avatar answered Sep 23 '22 17:09

umutto