简体   繁体   English

张量流conv2d的填充策略是什么?

[英]What is the padding strategy of tensorflow conv2d?

i follow the question and answer from stackoverflow 我遵循stackoverflow的问题和答案

however i am still confused of the start index and padding strategy of tf.nn.conv2d after i have the following tests, hopefully someone can give me a clue here, especially on odd and even strides 但我仍然困惑tf.nn.conv2d开始索引填充策略后,我有以下的测试,希望有人能在这里给我一个线索,特别是奇数偶数的进步

array height(h),kernel size(f), stride number(s) 数组高度(h),内核大小(f),步幅数

h,f,s = 4,3,2 

padding number on left column (pl) padding on right column (pr) of matrix x 矩阵x左列(pl)的填充数右列(pr)的填充数

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 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 how conv2d the start index of it 在卷积池的情况下,零填充应该像我定义xp一样在数组x周围填充,但是,我无法弄清楚conv2d的起始索引如何

origin matrix x 原始矩阵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 tf.nn.conv2d does not pad zero on the left in this case ? 在“相同”类型的卷积中,为什么在这种情况下tf.nn.conv2d不在左侧填充零?

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

valid convolution on matrix x 在矩阵x上的有效卷积

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

valid type convolution after zero padding from xp (as my expected result) 从xp填充零后的有效类型卷积(作为我的预期结果)

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

The formula for the (total) padding is explained here : (总)填充的公式在这里说明:

在此处输入图片说明

In your case, n mod s = 4 mod 2 = 0 so 在您的情况下, n mod s = 4 mod 2 = 0因此

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. 这就解释了为什么您在左侧看不到任何填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM