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的计算

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中的卷积和池化计算方式详解

来源:https://blog.csdn.net/zz2230633069/article/details/83214308

标签:pytorch,卷积,池化
0
投稿

猜你喜欢

  • Python创建增量目录的代码实例

    2021-12-07 04:12:55
  • Python尾递归优化实现代码及原理详解

    2023-11-08 15:35:28
  • Django搭建项目实战与避坑细节详解

    2021-04-30 06:46:46
  • python中的lambda表达式用法详解

    2022-12-01 17:33:57
  • c#数据库与TXT导入导出的实例

    2024-01-24 06:34:16
  • MySQL事务处理与应用简析

    2024-01-21 09:01:24
  • python GUI库图形界面开发之PyQt5信号与槽多窗口数据传递详细使用方法与实例

    2022-06-05 14:11:58
  • 丢失的数据忘记备份的处理方法[图文]第1/2页

    2024-01-18 22:18:41
  • 最简洁的SQL多重查询的解决方案

    2005-09-12 16:31:00
  • 详解Python中Addict模块的使用方法

    2021-11-04 00:06:04
  • Python 的类、继承和多态详解

    2021-05-29 17:31:51
  • python画图时设置分辨率和画布大小的实现(plt.figure())

    2022-11-09 18:12:17
  • python中利用matplotlib读取灰度图的例子

    2022-08-08 08:25:36
  • 测试框架JUnit VS TestNG对比分析

    2023-02-11 02:44:02
  • Oracle数据库集复制方法浅议

    2010-07-21 12:50:00
  • sql 判断函数、存储过程是否存在的代码整理

    2024-01-28 20:09:53
  • pandas DataFrame 行列索引及值的获取的方法

    2022-03-24 06:14:11
  • 删除python pandas.DataFrame 的多重index实例

    2022-11-10 17:25:12
  • Python实现测试磁盘性能的方法

    2022-01-31 19:00:46
  • javascript 继承实现方法

    2024-04-19 11:04:12
  • asp之家 网络编程 m.aspxhome.com