padding的规则

·          padding=‘VALID’时,输出的宽度和高度的计算公式(下图gif为例)

    

    输出宽度:output_width = (in_width-filter_width+1)/strides_width  =(5-3+1)/2=1.5【向上取整=2】

    输出高度:output_height = (in_height-filter_height+1)/strides_height  =(5-3+1)/2=1.5【向上取整=2】

    输出的形状[1,2,2,1]

    

import tensorflow as tf
image = [0,1.0,1,2,2,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1,0,2,0,1,0]
input = tf.Variable(tf.constant(image,shape=[1,5,5,1])) ##1通道输入
fil1 = [-1.0,0,1,-2,0,2,-1,0,1]
filter = tf.Variable(tf.constant(fil1,shape=[3,3,1,1])) ##1个卷积核对应1个featuremap输出 op = tf.nn.conv2d(input,filter,strides=[1,2,2,1],padding='VALID') ##步长2,VALID不补0操作 init = tf.global_variables_initializer() with tf.Session() as sess:
sess.run(init)
# print('input:\n', sess.run(input))
# print('filter:\n', sess.run(filter))
print('op:\n',sess.run(op)) ##输出结果
'''
[[[[ 2.]
[-1.]] [[-1.]
[ 0.]]]]
'''

VALID步长2

    如果strides=[1,3,3,1]的情况又是如何呢?   

    输出宽度:output_width  = (in_width-filter_width+1)/strides_width  =(5-3+1)/3=1

    输出高度:output_height = (in_height-filter_height+1)/strides_height  =(5-3+1)/3=1

    输出的形状[1,1,1,1],因此输出的结果只有一个

    

import tensorflow as tf
image = [0,1.0,1,2,2,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1,0,2,0,1,0]
input = tf.Variable(tf.constant(image,shape=[1,5,5,1])) ##1通道输入
fil1 = [-1.0,0,1,-2,0,2,-1,0,1]
filter = tf.Variable(tf.constant(fil1,shape=[3,3,1,1])) ##1个卷积核对应1个featuremap输出 op = tf.nn.conv2d(input,filter,strides=[1,3,3,1],padding='VALID') ##步长2,VALID不补0操作 init = tf.global_variables_initializer() with tf.Session() as sess:
sess.run(init)
# print('input:\n', sess.run(input))
# print('filter:\n', sess.run(filter))
print('op:\n',sess.run(op)) ##输出结果
'''
op:
[[[[ 2.]]]]
'''

VALID步长3

padding=‘SAME’时,输出的宽度和高度的计算公式

    输出宽度:output_width  = in_width/strides_width=5/2=2.5【向上取整3】

    输出高度:output_height = in_height/strides_height=5/2=2.5【向上取整3】

    则输出的形状:[1,3,3,1]

    那么padding补0的规则又是如何的呢?【先确定输出形状,再计算补多少0】

    pad_width = max((out_width-1)*strides_width+filter_width-in_width,0)=max((3-1)*2+3-5,0)=2

    pad_height = max((out_height-1)*strides_height+filter_height-in_height,0)=max((3-1)*2+3-5,0)=2

    pad_top = pad_height/2=1

    pad_bottom = pad_height-pad_top=1

    pad_left = pad_width/2=1

    pad_right = pad_width-pad_left=1

        

import tensorflow as tf
image = [0,1.0,1,2,2,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1,0,2,0,1,0]
input = tf.Variable(tf.constant(image,shape=[1,5,5,1])) ##1通道输入
fil1 = [-1.0,0,1,-2,0,2,-1,0,1]
filter = tf.Variable(tf.constant(fil1,shape=[3,3,1,1])) ##1个卷积核对应1个featuremap输出 op = tf.nn.conv2d(input,filter,strides=[1,2,2,1],padding='SAME') ##步长2,VALID不补0操作 init = tf.global_variables_initializer() with tf.Session() as sess:
sess.run(init)
# print('input:\n', sess.run(input))
# print('filter:\n', sess.run(filter))
print('op:\n',sess.run(op)) ##输出结果
'''
op:
[[[[ 3.]
[ 1.]
[-4.]] [[ 3.]
[ 0.]
[-3.]] [[ 4.]
[-1.]
[-3.]]]]
'''

SAME步长2

    如果步长为3呢?补0的规则又如何?

    输出宽度:output_width  = in_width/strides_width=5/3=2

    输出高度:output_height = in_height/strides_height=5/3=2

    则输出的形状:[1,2,2,1]

    那么padding补0的规则又是如何的呢?【先确定输出形状,再计算补多少0】

    pad_width = max((out_width-1)*strides_width+filter_width-in_width,0)=max((2-1)*3+3-5,0)=1

    pad_height = max((out_height-1)*strides_height+filter_height-in_height,0)=max((2-1)*3+3-5,0)=1

    pad_top = pad_height/2=0【向下取整】

    pad_bottom = pad_height-pad_top=1

    pad_left = pad_width/2=0【向下取整】

    pad_right = pad_width-pad_left=1

    

import tensorflow as tf
print(3/2)
image = [0,1.0,1,2,2,0,1,1,0,0,1,1,0,1,0,1,0,1,1,1,0,2,0,1,0]
input = tf.Variable(tf.constant(image,shape=[1,5,5,1])) ##1通道输入
fil1 = [-1.0,0,1,-2,0,2,-1,0,1]
filter = tf.Variable(tf.constant(fil1,shape=[3,3,1,1])) ##1个卷积核对应1个featuremap输出 op = tf.nn.conv2d(input,filter,strides=[1,3,3,1],padding='SAME') ##步长2,VALID不补0操作 init = tf.global_variables_initializer() with tf.Session() as sess:
sess.run(init)
# print('input:\n', sess.run(input))
# print('filter:\n', sess.run(filter))
print('op:\n',sess.run(op)) ##输出结果
'''
op:
[[[[ 2.]
[-3.]] [[ 0.]
[-3.]]]]
'''

SAME步长3

Tensorflow之CNN卷积层池化层padding规则的更多相关文章

  1. tensorflow中的卷积和池化层(一)

    在官方tutorial的帮助下,我们已经使用了最简单的CNN用于Mnist的问题,而其实在这个过程中,主要的问题在于如何设置CNN网络,这和Caffe等框架的原理是一样的,但是tf的设置似乎更加简洁. ...

  2. tensorflow的卷积和池化层(二):记实践之cifar10

    在tensorflow中的卷积和池化层(一)和各种卷积类型Convolution这两篇博客中,主要讲解了卷积神经网络的核心层,同时也结合当下流行的Caffe和tf框架做了介绍,本篇博客将接着tenso ...

  3. CNN中卷积层 池化层反向传播

    参考:https://blog.csdn.net/kyang624823/article/details/78633897 卷积层 池化层反向传播: 1,CNN的前向传播 a)对于卷积层,卷积核与输入 ...

  4. tensorflow 1.0 学习:池化层(pooling)和全连接层(dense)

    池化层定义在 tensorflow/python/layers/pooling.py. 有最大值池化和均值池化. 1.tf.layers.max_pooling2d max_pooling2d( in ...

  5. CNN中的池化层的理解和实例

    池化操作是利用一个矩阵窗口在输入张量上进行扫描,并且每个窗口中的值通过取最大.取平均或其它的一些操作来减少元素个数.池化窗口由ksize来指定,根据strides的长度来决定移动步长.如果stride ...

  6. 『TensorFlow』卷积层、池化层详解

    一.前向计算和反向传播数学过程讲解

  7. 第十三节,使用带有全局平均池化层的CNN对CIFAR10数据集分类

    这里使用的数据集仍然是CIFAR-10,由于之前写过一篇使用AlexNet对CIFAR数据集进行分类的文章,已经详细介绍了这个数据集,当时我们是直接把这些图片的数据文件下载下来,然后使用pickle进 ...

  8. tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图

    tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图 因为很多 demo 都比较复杂,专门抽出这两个函数,写的 demo. 更多教程:http://www.tensorflown ...

  9. CNN卷积神经网络的卷积层、池化层的输出维度计算公式

    卷积层Conv的输入:高为h.宽为w,卷积核的长宽均为kernel,填充为pad,步长为Stride(长宽可不同,分别计算即可),则卷积层的输出维度为: 其中上开下闭开中括号表示向下取整. MaxPo ...

随机推荐

  1. 035-OpenStack 关闭安全组

    OpenStack Neutron的安全组默认会对每个网口开启MAC/IP过滤功能(防arp欺骗),不是该网口的MAC/IP发出的包会被宿主机丢弃.这种限制会导致vNF的上行网口转发的数据包被丢弃,无 ...

  2. 021-制作OpenStack镜像官方文档

    可参考官方文档:https://docs.openstack.org/image-guide/ 制作centos7 :https://docs.openstack.org/image-guide/ce ...

  3. Qt 打包release发布问题

    除了使用depens查看exe依赖的dll,本文使用qt5.13自带的打包工具windeployqt.exe tips: demo.exe(x86) :C:\Qt\Qt5.12.3\5.12.3\ms ...

  4. 【leetcode】1110. Delete Nodes And Return Forest

    题目如下: Given the root of a binary tree, each node in the tree has a distinct value. After deleting al ...

  5. Java并行

    序言 Java 有四种并行方式: 1.thread  使用“原汁原味”的裸线程 2.Executor (java 5 以后出现的) 3.Forkjoin 框架 (java 8 出现的) 4.Actor ...

  6. luogu P1217 [USACO1.5]回文质数 Prime Palindromes x

    P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...

  7. HDU 1134 Game of Connections(卡特兰数+大数模板)

    题目代号:HDU 1134 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1134 Game of Connections Time Limit: 20 ...

  8. es之java索引操作

    1.7.1: 创建索引 /** * 创建索引 * */ @Test public void createIndex(){ // 创建索引 CreateIndexResponse blog2 = cli ...

  9. python中的实例属性和类属性

    在python中,类属性和实例属性的区别是什么? 我认为是作用域的不同,实例对象可以访问类属性,类对象不可以访问实例属性.(类的概念本身就是作用域的概念,你不能让一只猫会飞,猫属于猫类,这一类都不会飞 ...

  10. db2用户权限赋值

    <!------------创建db2用户和组-------------------------------------------> [root@localhost ~]# userad ...