CNN中的padding

在使用TF搭建CNN的过程中,卷积的操作如下

convolution = tf.nn.conv2d(X, filters, strides=[1,2,2,1], padding="SAME") 

这个函数中各个参数的含义是什么呢?

  • X:输入数据的mini-batch,为一个4D tensor;分别表示的含义为[n_batch,height,width,channel]
  • filters:为卷积核,为一个4D tensor,分别表示的含义为 [filter_height, filter_width, in_channels, out_channels]
  • stride:为步长,使用方法为[1,stride,stride,1]
    该方法先将filter展开为一个2D的矩阵,形状为[filter_heightfilter_width in_channels, out_channels],再在图片上面选择一块大小进行卷积计算的到一个大小为[batch, out_height, out_width, filter_height * filter_width * in_channels]的虚拟张量。
    再将上面两部相乘(右乘filter矩阵)
  • padding:string类型的量,只能是"SAME","VALID"其中之一,这个值决定了不同的卷积方式。下面使用图表示两种的计算形式

当使用VALID的时候,如果卷积计算过程中,剩下的不够一步,则剩下的像素会被抛弃,SAME则会补0.

filter_primes = np.array([2., 3., 5., 7., 11., 13.], dtype=np.float32)
x = tf.constant(np.arange(1, 13+1, dtype=np.float32).reshape([1, 1, 13, 1]))
filters = tf.constant(filter_primes.reshape(1, 6, 1, 1))

valid_conv = tf.nn.conv2d(x, filters, strides=[1, 1, 5, 1], padding='VALID')
same_conv = tf.nn.conv2d(x, filters, strides=[1, 1, 5, 1], padding='SAME')

with tf.Session() as sess:
    print("VALID:
", valid_conv.eval())
    print("SAME:
", same_conv.eval())

输出内容为

VALID:
 [[[[ 184.]
   [ 389.]]]]
SAME:
 [[[[ 143.]
   [ 348.]
   [ 204.]]]]

实际计算向量如下所示:

print("VALID:")
print(np.array([1,2,3,4,5,6]).T.dot(filter_primes))
print(np.array([6,7,8,9,10,11]).T.dot(filter_primes))
print("SAME:")
print(np.array([0,1,2,3,4,5]).T.dot(filter_primes))
print(np.array([5,6,7,8,9,10]).T.dot(filter_primes))
print(np.array([10,11,12,13,0,0]).T.dot(filter_primes))
>>

VALID:
184.0
389.0
SAME:
143.0
348.0
204.0

再来做一个小实验,使用VALID的时候:

input = tf.Variable(tf.random_normal([1,5,5,5]))
filter = tf.Variable(tf.random_normal([3,3,5,1]))
op = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='VALID')
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print(op)
# print(sess.run(op))  
>>Tensor("Conv2D:0", shape=(1, 2, 2, 1), dtype=float32)

使用SAME的时候

input = tf.Variable(tf.random_normal([1,5,5,5]))
filter = tf.Variable(tf.random_normal([3,3,5,1]))
op = tf.nn.conv2d(input, filter, strides=[1, 2, 2, 1], padding='SAME')
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print(op)
# print(sess.run(op))  
>>Tensor("Conv2D:0", shape=(1, 3, 3, 1), dtype=float32)
原文地址:https://www.cnblogs.com/wxshi/p/8706499.html