How to interpret model.summary() output in CNN?

Viewed 6312

I am new to deep learning and CNNs. If a CNN has been created as shown in the screenshot, then how can one explain the outputs as described by model.summary(). I am not able to understand the output shapes of different layers.

Model summary:

Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_14 (Conv2D)           (None, 29, 29, 32)        1568      
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 14, 14, 32)        0         
_________________________________________________________________
conv2d_15 (Conv2D)           (None, 11, 11, 32)        16416     
_________________________________________________________________
max_pooling2d_7 (MaxPooling2 (None, 5, 5, 32)          0         
_________________________________________________________________
flatten_3 (Flatten)          (None, 800)               0         
_________________________________________________________________
dense_6 (Dense)              (None, 32)                25632     
_________________________________________________________________
dense_7 (Dense)              (None, 10)                330       
=================================================================
Total params: 43,946
Trainable params: 43,946
Non-trainable params: 0

neural network design

3 Answers

Assume that the size of each image is (32, 32, 3), as per the question.

Keras then appends an extra dimension for processing multiple batches, i.e., to train multiple images in every step of a single epoch. Since batch size can vary, its size is represented by None. Hence, the input shape becomes (None, 32, 32, 3).

Convolving a (32, 32) image with a (4, 4) filter, with strides and dilation rate of 1, and 'valid' padding, results in an output of size (32 - 4 + 1, 32 - 4 + 1) = (29, 29). Since you have 32 such filters, the output shape becomes (29, 29, 32).

The default MaxPooling kernel has a shape of (2, 2) and strides of (2, 2). Applying that to a (29, 29) image results in an image of shape (((29 - 2)//2) + 1, ((29 - 2)//2) + 1)) = (14, 14).

This pattern can be extended to all Conv2D and MaxPooling layers.

The Flatten layer takes all pixels along all channels and creates a 1D vector (not considering batch size). Therefore, an input of (5, 5, 32) is flattened to (5 * 5 * 32) = 800 values.

Parameter count

The number of parameters for a Conv2D layer is given by:

(kernel_height * kernel_width * input_channels * output_channels) + (output_channels if bias is used).

So, for the first Conv2D layer with 3 input channels, 32 output channels and a kernel size of (4, 4), the number of parameters is (4 * 4 * 3 * 32) + 32 = 1568.

Your first layer is a convolution, which takes an unknown input shape (it's known by you, you defined it somewhere with input_shape=....

The first convolution has an output with shape (None, 29, 29, 32), where:

  • None is the batch size
  • 29 and 29 are the size of the resulting image
  • 32 are the number of filters of this convolution and also the number of channels in its output

Then you have a maxpooling layer that takes the output of the convolution as input. The output of the pooling has shape (None, 14, 14, 32), so it divided the size of your image by two, leaving the rest as it was.

Then you have another convolution, taking the output of the pooling as input, the output shape of this new convolution is (None, 11, 11, 32). You used 32 filters here too.

.... .....

....

Then you have a Flatten layer, which takes the images and transform them into a single vector, output shape is (None, 800), where None is still the batch size untouched, the 800 are all elements you had in the input tensor, now in a single vector, one vector per sample in the batch.

Then Dense layers, the first with 32 units, the second with 10 units.

The final output shape of your model is (None, 10). It outputs 10 values per sample in the batch.


Each layer has a number of parameters (which are generally the weights). The parameters that are trainable will be updated with backpropagation. The parameters that are not trainable will remain static or will be updated with a different method (only a few layers such as BatchNormalization has parameters that are updated with different methods)

Your model has a total of 43946 weights, all trainable.

For Conv2d, As you can notice the output shape is (None, 10, 10, 64). The first dimension represents the batch size, which is None at the moment. Because the network does not know the batch size in advance. Once you fit the data, None would be replaced by the batch size you give while fitting the data.

Input shape

4D tensor with shape: (batch, channels, rows, cols) if data_format is "channels_first" 
or 4D tensor with shape: (batch, rows, cols, channels) if data_format is 
"channels_last".

Output shape

4D tensor with shape: (batch, filters, new_rows, new_cols) if data_format is 
"channels_first" or 4D tensor with shape: (batch, new_rows, new_cols, filters) if 
data_format is "channels_last". rows and cols values might have changed due to 
padding.

For the maxpool2d layers, the values are the same as the above with a slight change as follows.

**Input**

If data_format='channels_last': 4D tensor with shape: (batch_size, rows, cols, channels)
If data_format='channels_first': 4D tensor with shape: (batch_size, channels, rows, cols)

**Output**

If data_format='channels_last': 4D tensor with shape: (batch_size, pooled_rows, pooled_cols, channels)
If data_format='channels_first': 4D tensor with shape: (batch_size, channels, pooled_rows, pooled_cols)

Now, We can simply add a convolution layer at the top of another convolution layer since the output dimension of convolution is the same as it’s input dimension. We usually add the Dense layers at the top of the Convolution layer to classify the images. However input data to the dense layer 2D array of shape (batch_size, units). And the output of the convolution layer is a 4D array. Thus we have to change the dimension of output received from the convolution layer to a 2D array.

We can do it by inserting a Flatten layer on top of the Convolution layer. Flatten layer squash the 3 dimensions of an image to a single dimension. Now we only have a 2D array of shape (batch_size, squashed_size), which is acceptable for dense layers.

Hope this will help you.

Related