• Same Convolution Padding

ni和no分别表示输入和输出的大小，k为kernel大小，s为stride步长。那么在same padding中，no由ni和s 二者 确定：no = ceil(ni / s)

1. mod(ni , s) = 0时

1. mod(ni , s) != 0时

# 先确定输出维度，记住是上取整
out_height = ceil(float(in_height) / float(strides[1]))
out_width  = ceil(float(in_width) / float(strides[2]))

# 上面的公式
if (in_height % strides[1] == 0):
pad_along_height = max(filter_height - strides[1], 0)
else:
pad_along_height = max(filter_height - (in_height % strides[1]), 0)
if (in_width % strides[2] == 0):
pad_along_width = max(filter_width - strides[2], 0)
else:
pad_along_width = max(filter_width - (in_width % strides[2]), 0)

# 这里是当pi为奇数时，下侧比上侧多一，右侧比左侧多一。
#  Note that this is different from existing libraries such as cuDNN and Caffe, which explicitly specify the number of padded pixels and always pad the same number of pixels on both sides.


• Valid Convolution Padding

no = ceil( (ni – k + 1) / s )

out_height = ceil(float(in_height - filter_height + 1) / float(strides[1]))
out_width  = ceil(float(in_width - filter_width + 1) / float(strides[2]))