Home > Net >  Keras Conv2D without MaxPool2D but the Output Shape is divided by 2
Keras Conv2D without MaxPool2D but the Output Shape is divided by 2

Time:08-09

I found two problems in the following Keras model.

Here is the full test code:

import tensorflow as tf

model=tf.keras.Sequential()

model.add(tf.keras.layers.Conv2D(15,(5,5), padding='same', input_shape=(28, 28, 1)))
model.add(tf.keras.layers.Conv2D(16,(5,5)))
model.add(tf.keras.layers.MaxPool2D(pool_size=(2,2)))

model.add(tf.keras.layers.Conv2D(32,(5,5),padding='same', input_shape=(28, 28, 3)))
model.add(tf.keras.layers.MaxPool2D(pool_size=(2,2)))

model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(10))

model.compile(
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    optimizer=tf.keras.optimizers.Adam(),
    metrics = [
        "accuracy"
    ]
)

model.summary()

Here is the output:

Model: "sequential_6"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d_18 (Conv2D)          (None, 28, 28, 15)        390       
                                                                 
 conv2d_19 (Conv2D)          (None, 24, 24, 16)        6016      
                                                                 
 max_pooling2d_12 (MaxPoolin  (None, 12, 12, 16)       0         
 g2D)                                                            
                                                                 
 conv2d_20 (Conv2D)          (None, 12, 12, 32)        12832     
                                                                 
 max_pooling2d_13 (MaxPoolin  (None, 6, 6, 32)         0         
 g2D)                                                            
                                                                 
 flatten_6 (Flatten)         (None, 1152)              0         
                                                                 
 dense_6 (Dense)             (None, 10)                11530     
                                                                 
=================================================================
Total params: 30,768
Trainable params: 30,768
Non-trainable params: 0

Question 1:

This layer will generate the Output Shape as "(None, 24, 24, 16)".

model.add(tf.keras.layers.Conv2D(16,(5,5)))

There is no any tf.keras.layers.MaxPool2D between the first layer and the second layer, why does the second layer still change the output shape to (None, 24, 24, 16)?

It should be (None, 28, 28, 16) because no any MaxPool2D before the second layer.

Question 2:

Why does the input_shape argument in this layer can't change the model to input_shape=(28, 28, 3):

model.add(tf.keras.layers.Conv2D(32,(5,5),padding='same', input_shape=(28, 28, 1)))

CodePudding user response:

Question 1

Your 2nd Conv2D layer is missing padding=same. It defaults to padding=valid therefore, the output size is 28-5 1=24.

Question 2

Not sure what you expected here. Sequential models can have only 1 input (which you've already defined the shape of, in the first layer). input_shape in the middle layers has no effect.

  • Related