My question:
A straightforward experiment that I conducted showed that using padding=\'SAME\'
in a conv2d layer in Keras/TF is differen
padding='Same'
in Keras means padding is added as required to make up for overlaps when the input size and kernel size do not perfectly fit.
Example of padding='Same':
# Importing dependency
import keras
from keras.models import Sequential
from keras.layers import Conv2D
# Create a sequential model
model = Sequential()
# Convolutional Layer
model.add(Conv2D(filters=24, input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2) ,padding='Same'))
# Model Summary
model.summary()
Output of the code -
Model: "sequential_20"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_28 (Conv2D) (None, 3, 3, 24) 120
=================================================================
Total params: 120
Trainable params: 120
Non-trainable params: 0
_________________________________________________________________
Pictorial Representation: Below image shows how the padding for the input (input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2)) when padding='Same'.
padding='Valid'
in Keras means no padding is added.
Example of padding='Valid': Have used same input for Conv2D that we used above for padding = 'Same' .i.e. (input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2))
# Importing dependency
import keras
from keras.models import Sequential
from keras.layers import Conv2D
# Create a sequential model
model = Sequential()
# Convolutional Layer
model.add(Conv2D(filters=24, input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2) ,padding='Valid'))
# Model Summary
model.summary()
Output of the code -
Model: "sequential_21"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_29 (Conv2D) (None, 2, 2, 24) 120
=================================================================
Total params: 120
Trainable params: 120
Non-trainable params: 0
_________________________________________________________________
Pictorial Representation: Below image shows there is no padding added for the input (input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2)) when padding='Valid'.
Now lets try same code that we used for padding='Valid'
for the input (input_shape=(6,6,1), kernel_size=(2,2), strides =(2,2)). Here padding='Valid'
should behave same as padding='Same'
.
Code -
# Importing dependency
import keras
from keras.models import Sequential
from keras.layers import Conv2D
# Create a sequential model
model = Sequential()
# Convolutional Layer
model.add(Conv2D(filters=24, input_shape=(6,6,1), kernel_size=(2,2), strides =(2,2) ,padding='Valid'))
# Model Summary
model.summary()
Output of the code -
Model: "sequential_22"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_30 (Conv2D) (None, 3, 3, 24) 120
=================================================================
Total params: 120
Trainable params: 120
Non-trainable params: 0
_________________________________________________________________