4

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.

Amir
  • 10,600
  • 9
  • 48
  • 75
dnth
  • 879
  • 2
  • 12
  • 22

1 Answers1

7

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.

0x1337
  • 1,074
  • 1
  • 14
  • 33
  • 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 Jan 12 '16 at 09:22
  • 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 – dnth Jan 12 '16 at 09:29
  • 1
    @dnth I have the same question, so your answer is `26` ? – Calvin Cheng Feb 09 '17 at 11:05