conv2d是常用的实现卷积的,Tensorflow调用Conv的api时,常用代码如下:

查看:https://tensorflow.google.cn/api_docs/python/tf/nn/conv2d


tf.nn.conv2d(input,filter,strides,padding,use_cudnn_on_gpu=True,data_format='NHWC',dilations=[1, 1, 1, 1],name=None
)

对于其中一个参数padding的理解为:

padding: A `string` from: `"SAME", "VALID"`.The type of padding algorithm to use.

padding有两个方式可以选择:“SAME” and “VALID”

举两个栗子:

One:padding='SAME'

import tensorflow as tfinput = tf.Variable(tf.random_normal([1,5,5,3]))
filter = tf.Variable(tf.random_normal([3,3,3,7]))result = tf.nn.conv2d(input, filter, strides=[1,2,2,1],padding='SAME')
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
print(sess.run(result))
print(result.shape)
sess.close()

结果为:

[[[[  6.88815355e-01  -1.58929396e+00  -8.13352680e+00   3.47248018e-01-2.10637522e+00  -2.47548366e+00  -3.29180861e+00][ -1.50164223e+00  -2.82424307e+00  -2.40781856e+00  -2.55665493e+00-3.89841533e+00  -6.71445191e-01   3.10867667e+00][ -3.39479542e+00  -1.40321875e+00   2.29996824e+00  -3.98842275e-017.90905952e-03  -1.71421432e+00  -5.47636747e-01]][[ -1.07995415e+00  -2.21969414e+00  -1.43076777e-01   2.65041399e+00-4.38491011e+00  -4.83550358e+00   8.30997753e+00][  1.35791779e+00  -1.38357902e+00  -4.50581169e+00   1.22106361e+00-1.36877072e+00  -1.19497585e+00  -3.64005876e+00][ -3.07881045e+00   1.33630781e+01  -4.33032846e+00   1.98507690e+00-1.34837186e+00  -3.44964921e-01  -5.76371312e-01]][[ -4.02724743e-01  -3.08082283e-01   1.51205099e+00  -2.11967897e+008.77675891e-01  -3.89271736e-01   1.28933489e+00][  1.05681574e+00   3.83993292e+00   1.46158600e+00   5.12251711e+00-4.37659168e+00  -5.88564873e-02   8.72927666e-01][  3.13625002e+00  -2.52725768e+00  -1.89247894e+00  -2.89734745e+002.49475980e+00  -7.85117006e+00   4.73596001e+00]]]]
(1, 3, 3, 7)

Two:padding='VALID'

import tensorflow as tfinput = tf.Variable(tf.random_normal([1,5,5,3]))
filter = tf.Variable(tf.random_normal([3,3,3,7]))result = tf.nn.conv2d(input, filter, strides=[1,2,2,1],padding='VALID')
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
print(sess.run(result))
print(result.shape)
sess.close()

结果为:

[[[[  3.30246162e+00   1.00174313e+01   1.02988682e+01  -3.38870287e+003.57620907e+00   9.25950432e+00   1.40226996e+00][  2.39865661e+00   4.90117121e+00   6.27546692e+00  -7.14295626e+00-1.87810266e+00   4.73461962e+00  -8.87438393e+00]][[  5.66498578e-01   1.21167574e+01  -2.98488545e+00   2.54433393e+00-4.40911025e-01  -4.96578693e-01   2.93070102e+00][ -5.70178509e+00   1.09887476e+01   2.36247849e+00   2.91668701e+00-1.77950829e-01   1.17763281e-02  -9.67830420e-01]]]]
(1, 2, 2, 7)

再比如,我们再举个栗子,如果x是一个2×3的矩阵, max pooling窗口为2×2,步长为strides=2

第一次由于窗口可以覆盖,橙色区域做max pooling:

由于步长为2,当向右滑动两步之后,VALID方式发现余下的窗口不到,所以将第3列直接舍弃掉了,所以得到的比原有的小;

而SAME方式与原有尺寸一致,所以需要填充,做补零填充;

另外来讲,我们如何去计算这两种方式?

 If padding == "SAME":output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides[i])If padding == "VALID":output_spatial_shape[i] =ceil((input_spatial_shape[i] -(spatial_filter_shape[i]-1) * dilation_rate[i])/ strides[i])

dilation_rate为一个可选的参数,默认为1。

padding = “SAME”:

S是步长。

padding = “VALID”:

输出大小等于输入大小减去滤波器大小加上1,最后再除以步长(f为滤波器的大小,S是步长大小)。

最后,简单的举几个小栗子,说些padding=“SAME”的一些如何补零的情况,挺有趣的:

对于原始图像与卷积核不匹配的情况,就要对图像的边界做一些填充,具体的填充方式和所差的元素个数有关:

1. 28x28的全1图像,卷积核全1的5x5,每个方向的两端各补了一个0,形成30x30,四周都是0的图像

import tensorflow as tfinput = tf.Variable(tf.ones([1,28,28,1]))
filter = tf.Variable(tf.ones([5,5,1,1]))
op = tf.nn.conv2d(input,filter,strides=[1,5,5,1],padding='SAME')with tf.Session() as sess:sess.run(tf.global_variables_initializer())res = sess.run(op)print("res.shape")print(res.shape)output = tf.reshape(res,[6,6])print(sess.run(output))

结果为:

res.shape
(1, 6, 6, 1)
[[ 16.  20.  20.  20.  20.  16.][ 20.  25.  25.  25.  25.  20.][ 20.  25.  25.  25.  25.  20.][ 20.  25.  25.  25.  25.  20.][ 20.  25.  25.  25.  25.  20.][ 16.  20.  20.  20.  20.  16.]]

2. 29x29的全1图像

import tensorflow as tfinput = tf.Variable(tf.ones([1,29,29,1]))
filter = tf.Variable(tf.ones([5,5,1,1]))
op = tf.nn.conv2d(input,filter,strides=[1,5,5,1],padding='SAME')with tf.Session() as sess:sess.run(tf.global_variables_initializer())res = sess.run(op)print("res.shape")print(res.shape)output = tf.reshape(res,[6,6])print(sess.run(output))

每行每列的最后补了一个0,结果为:

res.shape
(1, 6, 6, 1)
[[ 25.  25.  25.  25.  25.  20.][ 25.  25.  25.  25.  25.  20.][ 25.  25.  25.  25.  25.  20.][ 25.  25.  25.  25.  25.  20.][ 25.  25.  25.  25.  25.  20.][ 20.  20.  20.  20.  20.  16.]]

3. 27x27的全1图像

import tensorflow as tfinput = tf.Variable(tf.ones([1,27,27,1]))
filter = tf.Variable(tf.ones([5,5,1,1]))
op = tf.nn.conv2d(input,filter,strides=[1,5,5,1],padding='SAME')with tf.Session() as sess:sess.run(tf.global_variables_initializer())res = sess.run(op)print("res.shape")print(res.shape)output = tf.reshape(res,[6,6])print(sess.run(output))

在每个方向的开头补了一个0,最后补了两个0,结果为:

res.shape
(1, 6, 6, 1)
[[ 16.  20.  20.  20.  20.  12.][ 20.  25.  25.  25.  25.  15.][ 20.  25.  25.  25.  25.  15.][ 20.  25.  25.  25.  25.  15.][ 20.  25.  25.  25.  25.  15.][ 12.  15.  15.  15.  15.   9.]]

4.  26×26的全1图像

import tensorflow as tfinput = tf.Variable(tf.ones([1,26,26,1]))
filter = tf.Variable(tf.ones([5,5,1,1]))
op = tf.nn.conv2d(input,filter,strides=[1,5,5,1],padding='SAME')with tf.Session() as sess:sess.run(tf.global_variables_initializer())res = sess.run(op)print("res.shape")print(res.shape)output = tf.reshape(res,[6,6])print(sess.run(output))

首尾各补两个0,结果为:

res.shape
(1, 6, 6, 1)
[[  9.  15.  15.  15.  15.   9.][ 15.  25.  25.  25.  25.  15.][ 15.  25.  25.  25.  25.  15.][ 15.  25.  25.  25.  25.  15.][ 15.  25.  25.  25.  25.  15.][  9.  15.  15.  15.  15.   9.]]

得到的结论为:

当差偶数个元素是首尾各补一半,差奇数个时前边补奇数个,后边补偶数个

具体差多少元素和选定的卷积核大小以及滑动步长密切相关,那么滑动步长又有哪些影响呢?如果改成滑动步长为1

import tensorflow as tfinput = tf.Variable(tf.ones([1,26,26,1]))
filter = tf.Variable(tf.ones([5,5,1,1]))
op = tf.nn.conv2d(input,filter,strides=[1,1,1,1],padding='SAME')with tf.Session() as sess:sess.run(tf.global_variables_initializer())res = sess.run(op)print("res.shape")print(res.shape)output = tf.reshape(res,[26,26])print(sess.run(output))

结果为:

res.shape
(1, 26, 26, 1)
[[  9.  12.  15.  15.  15.  15.  15.  15.  15.  15.  15.  15.  15.  15.15.  15.  15.  15.  15.  15.  15.  15.  15.  15.  12.   9.][ 12.  16.  20.  20.  20.  20.  20.  20.  20.  20.  20.  20.  20.  20.20.  20.  20.  20.  20.  20.  20.  20.  20.  20.  16.  12.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 15.  20.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  20.  15.][ 12.  16.  20.  20.  20.  20.  20.  20.  20.  20.  20.  20.  20.  20.20.  20.  20.  20.  20.  20.  20.  20.  20.  20.  16.  12.][  9.  12.  15.  15.  15.  15.  15.  15.  15.  15.  15.  15.  15.  15.15.  15.  15.  15.  15.  15.  15.  15.  15.  15.  12.   9.]]

将其换为padding=“VALID”

import tensorflow as tfinput = tf.Variable(tf.ones([1,26,26,1]))
filter = tf.Variable(tf.ones([5,5,1,1]))
op = tf.nn.conv2d(input,filter,strides=[1,1,1,1],padding='VALID')with tf.Session() as sess:sess.run(tf.global_variables_initializer())res = sess.run(op)print("res.shape")print(res.shape)output = tf.reshape(res,[22,22])print(sess.run(output))

结果为:

res.shape
(1, 22, 22, 1)
[[ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.][ 25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.  25.25.  25.  25.  25.  25.  25.  25.  25.]]

总结一下:

卷积核大小,滑动步长直接影响最后的卷积结果的大小,且padding为SAME模式时,先对原图像进行填充,再做卷积,填充值须根据卷积核大小及滑动步长决定;而padding为VALID模式时,很简单粗暴直接从原始图像的首段开始卷积,到最后不能匹配卷积核的部分直接舍去。

参考文章:https://blog.csdn.net/wuzqChom/article/details/74785643

参考文章:https://blog.csdn.net/syyyy712/article/details/80272071?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

参考文章:https://blog.csdn.net/zhaozx19950803/article/details/80409502?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

参考文章:https://blog.csdn.net/qq_17272679/article/details/79591540?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

padding卷积的两种方式“SAME”和“VALID”相关推荐

  1. TensorFlow中padding卷积的两种方式“SAME”和“VALID”

    最近在用tensorflow搭建卷积神经网络,遇到了一个比较棘手的问题,我一直理解的padding有两个值,一个是SAME,一个是VALID,如果padding设置为SAME,则说明输入图片大小和输出 ...

  2. padding和卷积的区别_TensorFlow笔记1——20.CNN卷积神经网络padding两种模式SAME和VALID...

    第1种解说:(核心最后一张图,两种填充方式输出的形状尺寸计算公式) 在用tensorflow写CNN的时候,调用卷积核api的时候,会有填padding方式的参数,找到源码中的函数定义如下(max p ...

  3. 深度学习(6)之卷积的几种方式:1D、2D和3D卷积的不同卷积原理(全网最全!)

    深度学习(6)之卷积的几种方式:1D.2D和3D卷积的不同卷积原理(全网最全!) 英文原文 :A Comprehensive Introduction to Different Types of Co ...

  4. jvm两种方式获取对象所占用的内存

    在开发过程中,我们有时需要来获取某个对象的大小,以方便我们参考,来决定开发的技术方案.jvm中提供了两种方式来获取一个对象的大小. 通过Instrumentation来计算对象的大小 编写计算代码: ...

  5. 【REACT NATIVE 系列教程之十三】利用LISTVIEW与TEXTINPUT制作聊天/对话框获取组件实例常用的两种方式...

    本站文章均为 李华明Himi 原创,转载务必在明显处注明:  转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/react-native/2346.html ...

  6. 原生js更改html,原生js更改css样式的两种方式

    原生js更改css样式的两种方式 发布时间:2020-08-30 01:46:17 来源:脚本之家 阅读:148 作者:外婆的彭湖湾 下面我给大家介绍的是原生js更改CSS样式的两种方式: 1. 通过 ...

  7. 在线直播源码,VUE 获奖名单滚动显示的两种方式

    在线直播源码,VUE 获奖名单滚动显示的两种方式 第一种: 使用vue-seamless-scroll组件: 1.安装vue-seamless-scroll npm install vue-seaml ...

  8. ImGui添加背景图片的两种方式

    给ImGui添加背景图片的两种方式 最近在使用ImGui做客户端程序,想给窗口添加背景图片,但是作者的文档里面好像并没有讲如何添加背景图片,研究了下找到了两种方式. 第一种 创建一个和窗口一样大的Im ...

  9. 微信小程序生成二维码的两种方式

    微信小程序生成二维码的两种方式 2020/11/10 第一种,利用网络api自动生成 <image class="xin-erma" src="{{'https:/ ...

最新文章

  1. sparkCore源码解析之思维脑图
  2. 中国学霸本科生提出AI新算法:速度比肩Adam,性能媲美SGD,ICLR领域主席赞不绝口
  3. 行业谈实践,客户送祝福
  4. 为啥mysql的load这么快_mysql – 为什么’LOAD DATA INFILE’比普通的INSERT语句更快?...
  5. git revert
  6. C++笔记-函数参数使用void *的野路子
  7. c python boost.python_如何利用Boost.Python实现Python C/C++混合编程详解
  8. java string == 比较,Java 基础 之 String 的比较
  9. 打开SharePoint 2013 web application显示iis 欢迎页面
  10. 【汇编优化】之X86架构优化公用头讲解
  11. VMWARE 之 vSphere vCenter 安装基本配置
  12. 想听懂用户的声音,至少得先学会数据分析吧
  13. 查看opencv版本
  14. oppo手机解锁_oppo手机密码解锁大全【图文】
  15. 用二维码分享WiFi密码(转)
  16. Vue-amap 实现获取定位功能
  17. IE被劫持的手动解除
  18. arduino编码器计数_ARDUINO旋转编码器
  19. vue读取文件夹下面的文件名称
  20. ceph-mimic版本的安装使用1

热门文章

  1. 独家:中国电信提出建产业统一开发平台 避免个人与政企业务脱节
  2. 四叶草启动linux黑屏,四叶草剧场黑屏进不去解决方法一览
  3. bag of words matlab,Bag of words(matlab实现)
  4. 如何进行linux内核开发,2. 开发流程如何工作 — The Linux Kernel documentation
  5. LIRe 源代码分析 4:建立索引(DocumentBuilder)[以颜色布局为例]
  6. C++查找一个目录下所有特定扩展名的文件
  7. 视频工作者应该知道的几个网站
  8. opencv物品定位_使用OpenCV获取零件位置的学习笔记
  9. 手机能上wifi电脑不行_电脑如何当Wifi供手机使用
  10. osg 镜面_浙江天梭手表镜面抛光