Tensorflow - Same Padding Calculation

Viewed 4309

I have the following parameters:

in_height = 28
in_width = 28
stride (s) = 2
padding (p) = 'SAME'

The idea of 'SAME' padding is when s = 1 then input map and output map dimensions (height, width) should remain same

So if I should be able to get the padding size using the following:

(28 + 2*p - 5) + 1 = 28
Solving gives p = 2

which means there should be a padding on each side of 2

Using p=2 the output map size would be:

(28 + 4 -5)/2 + 1 = 14

From Tensorflow documentation, Same Padding:

out_height = ceil(float(in_height) / float(strides[1]))
out_width  = ceil(float(in_width) / float(strides[2]))

pad_along_height = max((out_height - 1) * strides[1] +
                    filter_height - in_height, 0)
pad_along_width = max((out_width - 1) * strides[2] +
                   filter_width - in_width, 0)
pad_top = pad_along_height // 2
pad_bottom = pad_along_height - pad_top
pad_left = pad_along_width // 2
pad_right = pad_along_width - pad_left

To follow the above:

out_height = ceil(28.0/2.0) = 14.0
out_width = ceil(28.0/2.0) = 14.0

Hence

pad_along_height = max((14.0 -1)*2 + 5 - 28,0) = 3
pad_along_width = max((14.0 -1)*2 + 5 - 28,0) = 3

pad_top = 3 // 2 = 1
pad_bottom = 3//2 - pad_top = 2
pad_left = pad_along_width // 2 = 1
pad_right = pad_along_width - pad_left = 2

So does it mean that the image should be padded 1 on top and 2 on bottom similarly on the left and right?

1 Answers
Related