Calculate dimension of feature maps in convolutional neural network

16,835

Check this article.

Formula for spatial size of the output volume: K*((W−F+2P)/S+1), where W - input volume size, F the receptive field size of the Conv Layer neurons, S - the stride with which they are applied, P - the amount of zero padding used on the border, K - the depth of conv layer.

Share:
16,835
dnth
Author by

dnth

Updated on June 04, 2022

Comments

  • dnth
    dnth almost 2 years

    I have convolutional neural network in Keras. I need to know the dimensions of the feature maps in each layer. My input is 28 by 28 pixel image. I know theres a way to calculate this I not sure how. Below is my code snippet using Keras.

    img_rows, img_cols = 28, 28
    nb_filters = 32
    nb_pool = 2
    nb_conv = 3
    
    model = Sequential()
    
    model.add(Convolution2D(nb_filters, nb_conv, nb_conv, border_mode='valid', input_shape=(1, img_rows, img_cols)))
    model.add(Activation('relu'))
    model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
    model.add(Dropout(0.25))
    
    model.add(Convolution2D(64, nb_conv, nb_conv, border_mode='valid'))
    model.add(Activation('relu'))
    model.add(Convolution2D(64, nb_conv, nb_conv))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
    model.add(Dropout(0.25))
    
    model.add(Flatten())
    model.add(Dense(512))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    
    
    model.add(Dense(nb_classes))
    model.add(Activation('softmax'))
    

    What i want to draw

    At the end of the day, this is what i want to draw. Thank you.

  • dnth
    dnth over 8 years
    So in my case above applying this formula (W−F+2P)/S+1, were W=28, F=3, I'm not sure what is the stride value in the above code, I assume it to be S=1. I'd get (28-3+0)/1 + 1 = 26. Can anyone verify this?
  • dnth
    dnth over 8 years
    I think I found the stride value. The default stride is S=1 which is called subsample in the current Keras version. The default value can be found in the Convolution2D class
  • Calvin Cheng
    Calvin Cheng about 7 years
    @dnth I have the same question, so your answer is 26 ?