Search code examples
pythontensorflowconvolution

What is the padding strategy of TensorFlow conv2d?


I am following this StackOverflow question and answer.

However, I am still confused of the start index and padding strategy of tf.nn.conv2d after doing the following tests, hopefully, someone can give me a clue here, especially on odd and even strides.

array height(h),kernel size(f), stride number(s)

h,f,s = 4,3,2 

padding number on left column (pl) padding on right column (pr) of matrix x

pl = int((f-1)/2)                           
pr = int(np.ceil((f-1)/2))                  

tf.reset_default_graph()
x = np.arange(1*h*h*1).reshape(1,h,h,1)
w = np.ones((f,f,1,1))
xc = tf.constant(x,np.float32)
wc = tf.constant(w,np.float32)
xp = np.pad(x,((0,0),(pl,pr),(pl,pr),(0,0)),'constant',constant_values = 0)
xcp = tf.constant(xp,np.float32)
zs = tf.nn.conv2d(xc,wc,strides=[1,s,s,1],padding='SAME')
zv = tf.nn.conv2d(xc,wc,strides=[1,s,s,1],padding='VALID')
zp = tf.nn.conv2d(xcp,wc,strides=[1,s,s,1],padding='VALID')

with tf.Session() as sess:
    os = sess.run(zs)
    ov = sess.run(zv)
    op = sess.run(zp)

print('x shape: ', x.shape,' kernel: ',f,' stride: ',s,'\n',x[0,:,:,0])
print(' 'SAME' os shape: ', os.shape,'\n',os[0,:,:,0])
print(' 'VALID' ov shape: ', ov.shape,'\n',ov[0,:,:,0])
print(' 'VALID' op shape: ', op.shape,' pl: ',pl,' pr: ', pr,'\n',op[0,:,:,0])

In the case of pooling in convolution, the zero padding should pad around the array x like how I define xp, however, I cannot figure out the conv2d start index of it.

Original matrix x

x shape:  (1, 4, 4, 1)  kernel:  3  stride:  2 
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]
[12 13 14 15]]

In 'same' type convolution, why doesn't tf.nn.conv2d pad zero on the left in this case?

'SAME' os shape:  (1, 2, 2, 1) 
[[45. 39.]
[66. 50.]]

Valid convolution on matrix x:

'VALID' ov shape:  (1, 1, 1, 1) 
[[45.]]

'valid' type convolution after zero padding from xp (as my expected result).

'VALID' op shape:  (1, 2, 2, 1)  pl:  1  pr:  1 
[[10. 24.]
[51. 90.]]

Solution

  • The formula for the (total) padding is explained here:

    enter image description here

    In your case, n mod s = 4 mod 2 = 0 so

    p = max(3 - 2, 0) = 1
    

    so

    p_left = p // 2 = 0
    p_right = 1 - p_left = 1
    

    That explains why you don't see any padding on the left.