pytorch中的卷积和池化计算方式详解
作者:一只tobey 发布时间:2021-03-31 19:26:32
TensorFlow里面的padding只有两个选项也就是valid和same
pytorch里面的padding么有这两个选项,它是数字0,1,2,3等等,默认是0
所以输出的h和w的计算方式也是稍微有一点点不同的:tf中的输出大小是和原来的大小成倍数关系,不能任意的输出大小;而nn输出大小可以通过padding进行改变
nn里面的卷积操作或者是池化操作的H和W部分都是一样的计算公式:H和W的计算
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中的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
来源:https://blog.csdn.net/zz2230633069/article/details/83214308


猜你喜欢
- 问题描述在电脑中重新安装Anaconda3&PyCharm后,运行原来的程序画图时出现了下图界面。不能弹出如下图所示的“figure”窗口。
- Node.js Domain(域) 简化异步代码的异常处理,可以捕捉处理try catch无法捕捉的异常。Domain 模块可分为
- 检测剪刀石头布三种手势,通过摄像头输入,方法如下:选用合适颜色空间及阈值提取皮肤部分使用滤波腐蚀膨胀等方法去噪边缘检测寻用合适方法分类Ope
- 这篇文章主要介绍了Python assert关键字原理及实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- 今天突然想起这个问题, 就好好搜索整理一下,不过在开始归纳之前,请先来一起做做这个小实验:忽略一切实际的外在情况, 你看了下面的按钮,第一本
- 简介短链接,通俗来说,就是将长的URL网址,通过程序计算等方式,转换为简短的网址字符串。早期短链接广泛应用于图片上传网站,通过缩短网址URL
- 前言由于一直用Linux系统,对于词典的支持特别不好,对于我这英语渣渣的人来说,当看英文文档就一直卡壳,之前用惯了有道词典,感觉很不错,虽然
- Silverlight也算一个比较开放的技术。Button控件其实也是一些标准的Grid、Canvas、Rectangle、TextBloc
- 通过文件夹导入包要求每个目录下都有一个__init__.py文件,此文件可空白。也可不空。a@ubuntu:~/Desktop$ tree
- 插入数据MySQL 表中使用 INSERT INTO SQL语句来插入数据。你可以通过 mysql> 命令提示窗口中向数据表中插入数据
- 一、创建多对多1.学生表create table students ( id int not null primary
- 程序开发一定要有开发工具,网上找了很多关于Python的开发工具,大神们在用记事本和VIM,小白都用PyCharm,我是属于小白一类的当然也
- 首先,我当时出现的问题是newproject创建的时候没有django的选项,查了半天发现我安装的pycharm是社区版本。所以需要用终端命
- JavaScriptSerializer 类由异步通信层内部使用,用于序列化和反序列化在浏览器和 Web 服务器之间传递的数据。说白了就是能
- 前言因为之前一直忽略的就是所有语言中关于位操作,觉得用处并不多,可能用到也非常简单的用法,但是其实一直忽略的是它们的用处还是非常大的,下面先
- 查看表空间的名称及大小代码如下:SQL>select t.tablespace_name, round(sum(bytes/(1024
- 引言膨胀与腐蚀是图像处理中两种最基本的形态学操作,膨胀将目标点融合到背景中,向外部扩展,腐蚀与膨胀意义相反,消除连通的边界,使边界向内收缩。
- uni-app自定义导航栏右侧做增加按钮并跳转链接uni-app 在页面上的导航栏右侧做一个增加的图标 并实现跳转1. 先看效果图2. 实现
- 列表对象pop()方法的使用pop() 方法用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。语法:verse.pop(in
- 声明定位元素:position属性值设置除默认值static以外的元素,包括relative,absolute,fixed。平台:win/I