[Pytorch]卷积运算conv2d

文章目录

[Pytorch]卷积运算conv2d

一.F.Conv2d

torch.nn.functional.Conv2d()的详细参数:

python 复制代码
conv2d(input: Tensor, weight: Tensor, bias: Optional[Tensor]=None, stride: Union[_int, _size]=1, padding: str="valid", dilation: Union[_int, _size]=1, groups: _int=1)

即F.Conv2d的主要参数如下:

  • input:输入特征图
  • weight:卷积核
  • bias:偏置参数
  • stride:卷积步长
  • padding:填充
  • dilation:膨胀系数
  • groups:分组卷积

利用F.Conv2d对图像中的暗线进行识别demo:

python 复制代码
x = torch.tensor([
    [
        [1.0, 4, 1, 4, 5],
        [0, 5, 3, 2, 1],
        [21,25, 25, 23, 26],
        [5, 2, 5, 2, 5],
        [4, 9, 3, 0, 7]
    ],
    [
        [2, 2, 2, 7, 2],
        [0, 0, 6, 3, 0],
        [24, 25, 25, 23, 27],
        [0, 1, 1, 1, 5],
        [0, 2, 0, 2, 2]
    ],
    [
        [2, 2, 2, 1, 0],
        [7, 2, 4, 3, 1],
        [24, 23, 28, 23, 24],
        [0, 0, 2, 2, 5],
        [5, 2, 4, 5, 2]
    ]
])

weight = torch.tensor([
    [
        [0.0, 0, 0],
        [1, 1, 1],
        [0, 0, 0],
    ],
    [
        [0, 0, 0],
        [1, 1, 1],
        [0, 0, 0],
    ],
    [
        [0, 0, 0],
        [1, 1, 1],
        [0, 0, 0],
    ]
])
out = F.conv2d(x, weight=weight.unsqueeze(0), bias=None, stride=1, padding=0)


toPIL = transforms.ToPILImage()  # 这个函数可以将张量转为PIL图片,由小数转为0-255之间的像素值
img_PIL = toPIL(x)  # 张量tensor转换为图片
img_PIL.save('./original.png')  # 保存图片;img_PIL.show()可以直接显示图片
torchvision.transforms.ToPILImage()
img_PIL = toPIL(out)
img_PIL.save('./convoluted.png')
print(out)


二.nn.Conv2d

pytorch中的卷积运算接口可使用torch.nn中的Conv2d():

python 复制代码
torch.nn.Conv2d( in_channels, out_channels, kernel_size, stride,  padding)

pytorch官方的参数解释说明:

python 复制代码
 Args:
        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, tuple or str, optional): Padding added to all four sides of
            the input. Default: 0
        padding_mode (str, optional): ``'zeros'``, ``'reflect'``,
            ``'replicate'`` or ``'circular'``. Default: ``'zeros'``
        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``

其中:padding_mode, dilation, groups, bias为可选参数,不是必须给定。

python 复制代码
import torch
import torchvision.transforms
from torch import nn
from torchvision import transforms
from torch.nn import functional as F

convolutional_layer = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=0)
x = torch.tensor([
    [
        [231.0, 120, 111, 34, 45],
        [100, 85, 23, 200, 111],
        [31, 45, 100, 103, 220],
        [5, 5, 5, 5, 5],
        [54, 89, 103, 150, 67]
    ],
    [
        [12, 58, 52, 87, 100],
        [200, 140, 86, 23, 10],
        [60, 75, 45, 30, 7],
        [155, 155, 155, 155, 155],
        [0, 122, 0, 0, 12]
    ],
    [
        [12, 12, 12, 11, 10],
        [67, 12, 45, 23, 1],
        [56, 12, 5, 10, 8],
        [0, 0, 0, 0, 0],
        [5, 12, 34, 56, 12]
    ]
])
out = convolutional_layer(x)
print(out)
print(convolutional_layer.weight.shape)
print(convolutional_layer.bias)

通过将tensor转为图片模拟以下卷积运算的效果:

python 复制代码
toPIL = transforms.ToPILImage()  # 这个函数可以将张量转为PIL图片,由小数转为0-255之间的像素值
img_PIL = toPIL(x)  # 张量tensor转换为图片
img_PIL.save('./original.png')  # 保存图片;img_PIL.show()可以直接显示图
img_PIL = toPIL(out)
img_PIL.save('./convoluted.png')

原特征图:

卷积运算后的输出特征图:

三.nn.Conv2d的运算过程

假如nn.Conv2d的定义如下:

python 复制代码
convolutional_layer = torch.nn.Conv2d(2, 3, 3, 1, bias=None)

我们研究该函数是如何通过卷积运算将输入通道数2变成输入通道数3的:

原特征图的输入通道数为2,图示橙色和蓝色两个通道:

nn.Conv2d随机初始化3组卷积核,3为输出通道数,其中每组卷积核中卷积核的数量为2(输入通道数),分为橙色的卷积核与蓝色的卷积核,与对应的特征图做卷积运算,每组分别得到两个卷积运算后的特征图,将每组得到的特征图进行对应位置数值的相加操作即可得到最后的nn.Conv2d运算结果。该卷积层的卷积核个数总共为3x2个即输出通道数x输入通道数个。

下面为了验证上述过程正确,我们使用两个F.conv2d来分别模拟蓝色卷积核和橙色卷积核与对应特征图卷积运算的过程。并将最终的结果相加和直接采用nn.conv2d的结果比较:

python 复制代码
convolutional_layer = torch.nn.Conv2d(2, 3, 3, 1, bias=None)
input = torch.randn(1, 2, 4, 4)
output = convolutional_layer(input)
weight = convolutional_layer.weight
out_1 = F.conv2d(input[:, 0, :, :].unsqueeze(1), weight[:, 0, :, :].unsqueeze(1), bias=None, stride=1, padding=0)
out_2 = F.conv2d(input[:, 1, :, :].unsqueeze(1), weight[:, 1, :, :].unsqueeze(1), bias=None, stride=1, padding=0)
print(f'output:\n{output}')
print('-----------------------------------------------------------------')
print(f'out_1 + out_2:\n{out_1 + out_2}')

最终通过比较发现,卷积运算的结果一致,说明上述nn.conv2d的具体运算过程正确

相关推荐
我算是程序猿16 分钟前
用AI做电子萌宠,快速涨粉变现
人工智能·stable diffusion·aigc
萱仔学习自我记录18 分钟前
微调大语言模型——超详细步骤
人工智能·深度学习·机器学习
湘大小菜鸡29 分钟前
NLP进阶(一)
人工智能·自然语言处理
XiaoLiuLB36 分钟前
最佳语音识别 Whisper-large-v3-turbo 上线,速度更快(本地安装 )
人工智能·whisper·语音识别
哪 吒39 分钟前
吊打ChatGPT4o!大学生如何用上原版O1辅助论文写作(附论文教程)
人工智能·ai·自然语言处理·chatgpt·aigc
Eric.Lee202141 分钟前
音频文件重采样 - python 实现
人工智能·python·深度学习·算法·audio·音频重采样
大神薯条老师42 分钟前
Python从入门到高手5.1节-Python简单数据类型
爬虫·python·深度学习·机器学习·数据分析
爱喝白开水a44 分钟前
关于大模型在企业生产环境中的独立部署问题
人工智能·深度学习·llm·大语言模型·ai大模型·计算机技术·本地部署大模型
可惜已不在2 小时前
AI 搜索引擎工具集合
人工智能·ai
Landy_Jay2 小时前
深度学习:基于MindSpore实现CycleGAN壁画修复
人工智能·深度学习