深度学习与图像处理
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 卷积
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
一、Pytorch中函数torch.nn.Conv2d
二、参数解释
torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
参数 | 参数类型 | ||
---|---|---|---|
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 | 卷积核尺寸,可以设为1个int型数或者一个(int, int)型的元组。例如(2,3)是高2宽3卷积核 |
stride | (int or tuple, optional) | Stride of the convolution. Default: 1 | 卷积步长,默认为1。可以设为1个int型数或者一个(int, int)型的元组。 |
padding | (int or tuple, optional) | Zero-padding added to both sides of the input. Default: 0 | 填充操作,控制padding_mode的数目。 |
padding_mode | (string, optional) | ‘zeros’, ‘reflect’, ‘replicate’ or ‘circular’. Default: ‘zeros’ | padding模式,默认为Zero-padding 。 |
dilation | (int or tuple, optional) | Spacing between kernel elements. Default: 1 | 扩张操作:控制kernel点(卷积核点)的间距,默认值:1。 |
groups | (int, optional) | Number of blocked connections from input channels to output channels. Default: 1 | group参数的作用是控制分组卷积,默认不分组,为1组。 |
bias | (bool, optional) | If True, adds a learnable bias to the output. Default: True | 为真,则在输出中添加一个可学习的偏差。默认:True。 |
三、常见使用方式
1.特征图尺寸减半
代码如下(示例):
#1.
Conv2d(kernel_size=(1, 1), stride=(2, 2), bias=False) # k = 1, s = 2, p = 0
#2.
Conv2d(kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
#3.
Conv2d(kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
#4.
MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
2.特征图尺寸不变
代码如下(示例):
#1.
Conv2d(kernel_size=(1, 1), stride=(1, 1), bias=False) # k = 1, s = 1, p = 0
#2.
Conv2d(kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) # k = 3, s = 1, p = 1