在pytorch中的官方文档(Conv2d — PyTorch 1.12 documentation),是这样描述Covn2d函数的。是在由多个输入平面组成的输入信号上应用 2D 卷积。其主要是对模型进行卷积,关于卷积就不多作解释,知乎有一篇介绍卷积的,介绍的很详细。如何通俗易懂地解释卷积? - 知乎

卷积的用法:

        Conv2d(in_channels, out_channels, kernel_size, stride=1,padding=0, dilation=1, groups=1,bias=True, padding_mode=‘zeros’)

卷积的几个主要参数

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 tupleoptional) – Stride of the convolution. Default: 1
  • padding (inttuple or stroptional) – Padding added to all four sides of the input. Default: 0
  • padding_mode (stringoptional) – 'zeros''reflect''replicate' or 'circular'. Default: 'zeros'
  • dilation (int or tupleoptional) – Spacing between kernel elements. Default: 1
  • groups (intoptional) – Number of blocked connections from input channels to output channels. Default: 1
  • bias (booloptional) – If True, adds a learnable bias to the output. Default: True

 这里面的参数比较多,但是如果不去设置,一般系统会给它莫认真

        in_channels (int)  --网络输入的通道数(必填)

        out_channels (int)  --经过卷积后,网络输出的通道数(必填)

        kernel_size (int or tuple) --卷积核的大小,可以是个数也可以是元祖(必填)

        stride (int or tuple, optional) --卷积的步幅(默认值是1),每次卷积移动的步长,一般卷积核在输入图像上的移动是自左至右,自上至下。如果参数是一个整数那么就默认在水平和垂直方向都是该整数。如果参数是stride=(2, 1),2代表着高(h)进行步长为2,1代表着宽(w)进行步长为1。(选填)

        padding (inttuple or str, optional)  -- 设置在所有边界增加 值为 0 的边距的大小(也就是在feature map 外围增加几圈 0 ),例如当 padding =1 的时候,如果原来大小为 3 × 3 ,那么之后的大小为 5 × 5 。即在外围加了一圈 0 。【选填】

        padding_mode (stringoptional) – 填充模式, padding_mode=‘zeros’表示的是0填充。默认是‘zero’。填充的几个选择 'zeros', 'reflect', 'replicate' 或 'circular'。(选调)

        dilation (int or tuple, optional) –控制卷积核之间的间距,默认值是1(可选)

        groups (intoptional) –控制输入和输出之间的连接。默认值是1(可选)当 groups 为1,那么所有的输入都会连接到所有输出。当 groups 为 2的时,相当于将输入分为两组,并排放置两层,每层看到一半的输入通道并产生一半的输出通道,并且两者都是串联在一起的。需要注意的是,in_channels 和 out_channels 必须都可以整除 groups,否则会报错

        bias (bool, optional) --偏置,该类型是个布尔值,如果是True,则偏置被应用,否则,该偏置不被使用。默认是True (可选)

相关形状

pytorch2对应python pytorch conv2d_python

 示例操作

Conv2d中常用的参数有in_channels, out_channels, kernel_size, stride, padding。

当in_channels =1 ,out_channels  =1 ,此时,输入的图像是1 ,输出的图像是1.,那么此时只需要一个卷积核

当in_channels =1 ,out_channels  =2,此时,输入的图像是1 ,输出的图像是2.,那么此时需要两个卷积核

import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
# 使用CIFAR10数据集
from torchvision import transforms

dataset_conv = torchvision.datasets.CIFAR10("./data", train=False, transform=transforms.ToTensor(), download=True)

# 将数据集放入dataloader中,让它进行加载
dataloder = DataLoader(dataset=dataset_conv, batch_size=64, shuffle=True, num_workers=0, drop_last=False)


class C2(nn.Module):  # 创建一个Test类,继承nn.Module
    def __init__(self):
        super(C2, self).__init__()  # 完成父类的初始化
        self.conc1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)  # 在Test这个网络写入个卷积层

    def forward(self, x):
        x = self.conc1(x)
        return (x)


# 初始化这个网络并输出
c2 = C2()
print(c2)

# 写入tensorboard中
writer = SummaryWriter("logs")
step = 0
for data in dataloder:
    imgs, target = data
    output = c2(imgs)  # 输出的数据是经过神经网络处理的数据
    print(output)
    # 查看数据类型代码
    print(output.shape)
    print(imgs.shape)

输出的结果是:

pytorch2对应python pytorch conv2d_pytorch2对应python_02