How to check or print padding done in python (keras)?
While making a model, we use padding in Conv2D function and also Maxpooling function like :
Conv2D(filters=52,kernel_size=(3,3),padding='same')
Is there a means of check the padding done or printing its size or the padding itself so as to facilitate debugging ?
The exact way how the padding is computed can be found in the github code here. First the output width and height values are computed and then the padding:
For padding="SAME"
:
out_height = ceil(in_height / stride_height)
out_width = ceil(in_width / stride_width)
if (in_height % strides[1] == 0):
pad_along_height = max(filter_height - stride_height, 0)
else:
pad_along_height = max(filter_height - (in_height % stride_height), 0)
if (in_width % strides[2] == 0):
pad_along_width = max(filter_width - stride_width, 0)
else:
pad_along_width = max(filter_width - (in_width % stride_width), 0)
You can also get an idea about the padding by using a convolution with a kernel that consists of ones:
import tensorflow as tf
dummy_in = tf.ones([1, 6, 6, 1])
kernel_size = 3
strides = 1
padding = "SAME"
conv = tf.keras.layers.Conv2D(1,
kernel_size,
strides=strides,
padding=padding,
use_bias=False,
kernel_initializer=tf.ones)
# build layer
conv(dummy_in)
# subtracting the number of pixels in kernel gives
# the number of pixels that were padded
kernel_size**2 - conv(dummy_in)[...,0]
This outputs
[[[5., 3., 3., 3., 3., 5.],
[3., 0., 0., 0., 0., 3.],
[3., 0., 0., 0., 0., 3.],
[3., 0., 0., 0., 0., 3.],
[3., 0., 0., 0., 0., 3.],
[5., 3., 3., 3., 3., 5.]]]
where each entry corresponds to the number of pixels that were padded in a single convolutional window. As you can see, 5 pixels where added around the corners, and 3 pixels at the sides. In this case, the image was padded by one row/column of zeros at each side.