pythontensorflowkerascomputer-visionconv-neural-network

How to specify padding with keras in Conv2D layer?


I am trying to implement AlexNet with Keras and was inspecting the network design in MATLAB which is given as follows

enter image description here

As could be seen, the second convolution layer has 256 filters of size 5x5, 48 channels and a padding of [ 2 2 2 2 ]. How could I specify padding of [ 2 2 2 2] with Keras? I went through the documentation of Conv2D. It accepts only 2 values for padding namely valid and same. I could not understand this. For what I know, valid would mean zero padding. How could I specify [2 2 2 2] padding with the second convolution layer? I created the first layer as:

model.add(keras.layers.Conv2D(filters = 96, kernel_size = (11,11), 
 strides = (4,4), padding = "valid", input_shape=(227,227,3)))

Also, since in the second layer there are 48 channels, do I need to be explicit about it?


Solution

  • A specific padding isn't specified in Conv2D but instead a ZeroPadding2D layer.

    valid and same are really just shorthands for common paddings - valid means that you don't pad the input and same means you add padding such that the output length is the same as the input length.

    In your case if you wanted to add a specific padding of size 2:

    model.add(keras.layers.ZeroPadding2D(padding=(2, 2)))
    model.add(keras.layers.Conv2D(filters = 96, kernel_size = (11,11), strides = (4,4), padding = "valid"))
    

    I would also strongly suggest checking out this keras implementation of alexnet. Note that you can also find docs for padding layers in the keras convolutional docs (it's all the way at the bottom).