pytorch中的卷积和池化计算方式详解


Posted in Python onJanuary 03, 2020

TensorFlow里面的padding只有两个选项也就是valid和same

pytorch里面的padding么有这两个选项,它是数字0,1,2,3等等,默认是0

所以输出的h和w的计算方式也是稍微有一点点不同的:tf中的输出大小是和原来的大小成倍数关系,不能任意的输出大小;而nn输出大小可以通过padding进行改变

nn里面的卷积操作或者是池化操作的H和W部分都是一样的计算公式:H和W的计算

pytorch中的卷积和池化计算方式详解

class torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False):
"""
Parameters: 
  kernel_size ? the size of the window to take a max over
  stride ? the stride of the window. 默认值是kernel_size
  padding ? implicit zero padding to be added on both side,默认值是0
  dilation ? a parameter that controls the stride of elements in the window,默认值是1
  return_indices ? if True, will return the max indices along with the outputs. Useful when Unpooling later
  ceil_mode ? when True, will use ceil instead of floor to compute the output shape,向上取整和向下取整,默认是向下取整
"""

不一样的地方在于:第一点,步长stride默认值,上面默认和设定的kernel_size一样,下面默认是1;第二点,输出通道的不一样,上面的输出通道和输入通道是一样的也就是没有改变特征图的数目,下面改变特征图的数目为out_channels

class torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True):
    pass
"""
Parameters: 
  in_channels (int) ? Number of channels in the input image
  out_channels (int) ? Number of channels produced by the convolution
  kernel_size (int or tuple) ? Size of the convolving kernel
  stride (int or tuple, optional) ? Stride of the convolution. Default: 1,默认是1
  padding (int or tuple, optional) ? Zero-padding added to both sides of the input. Default: 0
  dilation (int or tuple, optional) ? Spacing between kernel elements. Default: 1
  groups (int, optional) ? Number of blocked connections from input channels to output channels. Default: 1
  bias (bool, optional) ? If True, adds a learnable bias to the output. Default: True
"""

第三点不一样是卷积有一个参数groups,将特征图分开给不同的卷积进行操作然后再整合到一起,xception就是利用这一个。

"""
At groups=1, all inputs are convolved to all outputs.
At groups=2, the operation becomes equivalent to having two conv layers side by side, each seeing half the input channels, and producing half the output channels, and both subsequently concatenated.
At groups= in_channels, each input channel is convolved with its own set of filters (of size ⌊out_channelsin_channels⌋
).
"""

pytorch AvgPool2d函数

class torch.nn.AvgPool2d(kernel_size, stride=None, padding=0, 
             ceil_mode=False, count_include_pad=True):
  pass
"""
kernel_size: the size of the window
stride: the stride of the window. Default value is :attr:`kernel_size`
padding: implicit zero padding to be added on both sides
ceil_mode: when True, will use `ceil` instead of `floor` to compute the output shape
count_include_pad: when True, will include the zero-padding in the averaging calculation
"""

shape的计算公式,在(h,w)位置处的输出值的计算。

pytorch中的卷积和池化计算方式详解

pytorch中的F.avg_pool1d()平均池化操作作用于一维,input 的维度是三维比如[2,2,7]。F.avg_pool1d()中核size是3,步长是2表示每三个数取平均,每隔两个数取一次.比如[1,3,3,4,5,6,7]安照3个数取均值,两步取一次,那么结果就是[ 2.3333 ,4 ,6 ],也就是核是一维的,也只作用于一个维度。按照池化操作计算公式input size为[2,2,7],kernel size为3,步长为2,则输出维度计算(7-3)/2+1=3所以输出维度是[2,2,3],这与输出结果是一致的。

pytorch中的F.avg_pool2d(),input 是维度是4维如[2,2,4,4],表示这里批量数是2也就是两张图像,这里通道数量是2,图像是size 是4*4的.核size是(2,2),步长是(2,2)表示被核覆盖的数取平均,横向纵向的步长都是2.那么核是二维的,所以取均值时也是覆盖二维取的。输出中第一个1.5的计算是:(1+2+1+2)/4=1.5.表示第一张图像左上角的四个像素点的均值。按照池化操作计算公式input size为[2,2,4,4],kernel size为2*2,步长为2,则输出维度计算(4-2)/2+1=2所以输出维度是[2,2,2,2],这与输出结果是一致的。

Conv3d函数

class torch.nn.Conv3d(in_channels, out_channels, kernel_size, stride=1,
           padding=0, dilation=1, groups=1, bias=True):
  pass
"""
in_channels (int): Number of channels in the input image
out_channels (int): Number of channels produced by the convolution
kernel_size (int or tuple): Size of the convolving kernel
stride (int or tuple, optional): Stride of the convolution. Default: 1
padding (int or tuple, optional): Zero-padding added to all three sides of the input. Default: 0
dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``True``
Shape:
    - Input: :math:`(N, C_{in}, D_{in}, H_{in}, W_{in})`
    - Output: :math:`(N, C_{out}, D_{out}, H_{out}, W_{out})`
"""
  C_out = out_channels

pytorch中的卷积和池化计算方式详解

以上这篇pytorch中的卷积和池化计算方式详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
介绍Python的Django框架中的QuerySets
Apr 20 Python
Python的Django框架下管理站点的基本方法
Jul 17 Python
python读取oracle函数返回值
Jul 18 Python
详解Python函数可变参数定义及其参数传递方式
Aug 02 Python
浅谈Python实现Apriori算法介绍
Dec 20 Python
python 搭建简单的http server,可直接post文件的实例
Jan 03 Python
python图像处理入门(一)
Apr 04 Python
Python向excel中写入数据的方法
May 05 Python
在windows下使用python进行串口通讯的方法
Jul 02 Python
python爬虫解决验证码的思路及示例
Aug 01 Python
Django跨域资源共享问题(推荐)
Mar 09 Python
解决Keras自带数据集与预训练model下载太慢问题
Jun 12 Python
Python While循环语句实例演示及原理解析
Jan 03 #Python
在Pytorch中计算卷积方法的区别详解(conv2d的区别)
Jan 03 #Python
Python综合应用名片管理系统案例详解
Jan 03 #Python
Python tkinter常用操作代码实例
Jan 03 #Python
PyTorch中的padding(边缘填充)操作方式
Jan 03 #Python
nginx搭建基于python的web环境的实现步骤
Jan 03 #Python
Python如何使用字符打印照片
Jan 03 #Python
You might like
php入门学习知识点六 PHP文件的读写操作代码
2011/07/14 PHP
smarty中英文多编码字符截取乱码问题解决方法
2014/10/28 PHP
phpnow php探针环境检测代码
2014/11/04 PHP
PHP创建文件及写入数据(覆盖写入,追加写入)的方法详解
2019/02/15 PHP
javascript-简单的日历实现及Date对象语法介绍(附图)
2013/05/30 Javascript
jQuery封装的tab选项卡插件分享
2015/06/16 Javascript
jQuery插件实现多级联动菜单效果
2015/12/01 Javascript
JS工作中的小贴士之”闭包“与事件委托的”阻止冒泡“
2016/06/16 Javascript
简单实现js间歇或无缝滚动效果
2016/06/29 Javascript
javascript实现的全国省市县无刷新多级关联菜单效果代码
2016/08/01 Javascript
HTML5canvas 绘制一个圆环形的进度表示实例
2016/12/16 Javascript
详解webpack 多入口配置
2017/06/16 Javascript
浅谈Express异步进化史
2017/09/09 Javascript
vue2组件之select2调用的示例代码
2017/10/12 Javascript
layer弹出层全屏及关闭方法
2018/08/17 Javascript
详解mpvue scroll-view自动回弹bug解决方案
2018/10/01 Javascript
利用Node.js如何实现文件循环覆写
2019/04/05 Javascript
python定时采集摄像头图像上传ftp服务器功能实现
2013/12/23 Python
Python注释详解
2016/06/01 Python
python爬虫入门教程--HTML文本的解析库BeautifulSoup(四)
2017/05/25 Python
在Python中增加和插入元素的示例
2018/11/01 Python
Python实现程序判断季节的代码示例
2019/01/28 Python
django url到views参数传递的实例
2019/07/19 Python
Python 实现大整数乘法算法的示例代码
2019/09/17 Python
Django中自定义模型管理器(Manager)及方法
2019/09/23 Python
django 解决自定义序列化返回处理数据为null的问题
2020/05/20 Python
Python+OpenCV图像处理—— 色彩空间转换
2020/10/22 Python
在HTML5 canvas里用卷积核进行图像处理的方法
2018/05/02 HTML / CSS
Hello Molly美国:女性时尚在线
2019/08/26 全球购物
碧欧泉法国官网:Biotherm法国
2019/10/23 全球购物
法国亚马逊官方网站:Amazon.fr
2020/12/19 全球购物
毕业生实习证明
2014/09/19 职场文书
2014年质检员工作总结
2014/11/18 职场文书
2014年工程部工作总结
2014/11/25 职场文书
简单了解 MySQL 中相关的锁
2021/05/25 MySQL
利用Python读取微信朋友圈的多种方法总结
2021/08/23 Python