padding卷积的两种方式“SAME”和“VALID”
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”相关推荐
- TensorFlow中padding卷积的两种方式“SAME”和“VALID”
最近在用tensorflow搭建卷积神经网络,遇到了一个比较棘手的问题,我一直理解的padding有两个值,一个是SAME,一个是VALID,如果padding设置为SAME,则说明输入图片大小和输出 ...
- padding和卷积的区别_TensorFlow笔记1——20.CNN卷积神经网络padding两种模式SAME和VALID...
第1种解说:(核心最后一张图,两种填充方式输出的形状尺寸计算公式) 在用tensorflow写CNN的时候,调用卷积核api的时候,会有填padding方式的参数,找到源码中的函数定义如下(max p ...
- 深度学习(6)之卷积的几种方式:1D、2D和3D卷积的不同卷积原理(全网最全!)
深度学习(6)之卷积的几种方式:1D.2D和3D卷积的不同卷积原理(全网最全!) 英文原文 :A Comprehensive Introduction to Different Types of Co ...
- jvm两种方式获取对象所占用的内存
在开发过程中,我们有时需要来获取某个对象的大小,以方便我们参考,来决定开发的技术方案.jvm中提供了两种方式来获取一个对象的大小. 通过Instrumentation来计算对象的大小 编写计算代码: ...
- 【REACT NATIVE 系列教程之十三】利用LISTVIEW与TEXTINPUT制作聊天/对话框获取组件实例常用的两种方式...
本站文章均为 李华明Himi 原创,转载务必在明显处注明: 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/react-native/2346.html ...
- 原生js更改html,原生js更改css样式的两种方式
原生js更改css样式的两种方式 发布时间:2020-08-30 01:46:17 来源:脚本之家 阅读:148 作者:外婆的彭湖湾 下面我给大家介绍的是原生js更改CSS样式的两种方式: 1. 通过 ...
- 在线直播源码,VUE 获奖名单滚动显示的两种方式
在线直播源码,VUE 获奖名单滚动显示的两种方式 第一种: 使用vue-seamless-scroll组件: 1.安装vue-seamless-scroll npm install vue-seaml ...
- ImGui添加背景图片的两种方式
给ImGui添加背景图片的两种方式 最近在使用ImGui做客户端程序,想给窗口添加背景图片,但是作者的文档里面好像并没有讲如何添加背景图片,研究了下找到了两种方式. 第一种 创建一个和窗口一样大的Im ...
- 微信小程序生成二维码的两种方式
微信小程序生成二维码的两种方式 2020/11/10 第一种,利用网络api自动生成 <image class="xin-erma" src="{{'https:/ ...
最新文章
- B-Tree与B+Tree的区别
- ML之LoR:LoR之二分类之线性决策算法实现根据两课成绩分数~预测期末通过率(合格还是不合格)
- 干货:排名前16的Java工具类
- mysql时间字段条件查询_mysql 查询 时间作为查询条件
- java必读书籍_必读:Java Java
- 13-Flutter移动电商实战-ADBanner组件的编写
- async js 返回值_获取JavaScript异步函数的返回值
- 分公司网络建设---Juniper 设备策略路由配置
- develop note 1
- 选项不属于HTML语言特点,Web前端开发初级模拟测试卷一(2)
- iOS 监听锁屏/解锁事件
- paip.提升用户体验---提高兼容性无JS支持总结
- 【语音识别】基于matlab GUI BP神经网络0到10数字语音识别【含Matlab源码 672期】
- 计算机输入设备和输出设备怎么区分,输入设备和输出设备的区别
- CentOS6.9 网络安装及配置
- HTML5系列代码:使用空格符号
- 找出直系亲属-cpp
- SSR门户项目爬坑之路(一)
- Global项目|浅析销售BOM实施方案及注意事项
- 批量重命名,文件夹重命名的五种方式分享
热门文章
- 百度长语音识别免费开放 大幅提升语音转写效率
- java oracle连接数据库_Java 连接 Oracle 数据库
- 【计算机网络笔记】编码与调制
- LIRe 源代码分析 4:建立索引(DocumentBuilder)[以颜色布局为例]
- mysql tcp优化_mysql 优化
- python读取idx_使用groupby和transform获取idxmaxp
- css将空的div撑开,如何使用css将空的浮动div伸展到可用的全高度?
- android 的a标签,Android开发技巧之在a标签或TextView控件中单击链接弹出Activity(自定义动作)...
- Jmeter压力测试快速使用
- idea项目(git)版本回退