当前位置: 首页 > 编程笔记 >

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

夏俊人
2023-03-14
本文向大家介绍pytorch中的卷积和池化计算方式详解,包括了pytorch中的卷积和池化计算方式详解的使用技巧和注意事项,需要的朋友参考一下

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

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

 类似资料:
  • 本文向大家介绍卷积池化后大小计算公式?相关面试题,主要包含被问及卷积池化后大小计算公式?时的应答技巧和注意事项,需要的朋友参考一下 首先我们应该知道卷积或者池化后大小的计算公式: out_height=((input_height - filter_height + padding_top+padding_bottom)/stride_height )+1 out_width=((input_wi

  • 本文向大家介绍CNN中卷积和池化的作用?相关面试题,主要包含被问及CNN中卷积和池化的作用?时的应答技巧和注意事项,需要的朋友参考一下 卷积有一个重要概念是卷积核,用法是对上一层feature map进行逐块扫描进行卷积计算得到新的feature map,用于获得新的feature map,每个卷积核代表了一种特征,即从前一层提取新的特征,并且减少了参数 池化是为了防止图像特征提取中像素偏移对结果

  • 本文向大家介绍Pytorch 实现sobel算子的卷积操作详解,包括了Pytorch 实现sobel算子的卷积操作详解的使用技巧和注意事项,需要的朋友参考一下 卷积在pytorch中有两种实现,一种是torch.nn.Conv2d(),一种是torch.nn.functional.conv2d(),这两种方式本质都是执行卷积操作,对输入的要求也是一样的,首先需要输入的是一个torch.autogr

  • 请问以下对卷积神经网络中批量归一化的理解是否正确? 如下图所示,均值和方差是使用当前小批量中各个示例生成的相同特征图上的所有单元格计算的,即它们是跨h、w和m轴计算的。

  • 本文向大家介绍卷积层和池化层有什么区别相关面试题,主要包含被问及卷积层和池化层有什么区别时的应答技巧和注意事项,需要的朋友参考一下 参考回答:   卷积层 池化层 功能 提取特征 压缩特征图,提取主要特征 操作 可惜是二维的,对于三维数据比如RGB图像(3通道),卷积核的深度必须同输入的通道数,输出的通道数等于卷积核的个数。卷积操作会改变输入特征图的通道数。 池化只是在二维数据上操作的,因此不改变

  • 我找不到正确的公式来计算CNN中一个卷积层中的MAC数量。我从Quora尝试了这个公式 式中:输入特征映射的HW大小;KL滤波器大小S跨距C通道输入M输出特征映射N输入特征映射数 我举了一个例子:1个输入图像5x5x1 1个过滤器3x3x1然后我做了一个天真的计算,我得到了81个MAC。但当我使用上述公式时,ai得到了9。 我想有些事情我不明白。 提前感谢

  • 主要内容:卷积神经网络深度学习是机器学习的一个分支,它是近几十年来研究人员突破的关键步骤。深度学习实现的示例包括图像识别和语音识别等应用。 下面给出了两种重要的深度神经网络 - 卷积神经网络 递归神经网络。 在本章中,我们将关注第一种类型,即卷积神经网络(CNN)。 卷积神经网络 卷积神经网络旨在通过多层阵列处理数据。这种类型的神经网络用于图像识别或面部识别等应用。 CNN与任何其他普通神经网络之间的主要区别在于CNN

  • 如果输入到神经网络卷积层的是128x128x3大小的图像,并对其应用40个5x5大小的滤波器,那么输出大小会是多少?