【PyTorch】torch.nn.Conv2d 类:二维卷积层(2D Convolutional Layer)

torch.nn.Conv2d

torch.nn.Conv2d 是 PyTorch 二维卷积层(2D Convolutional Layer) 的实现,主要用于 计算机视觉任务(如图像分类、目标检测等),可以提取 空间特征增强模型的表示能力


1. torch.nn.Conv2d 语法

torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros')
参数说明
in_channels输入通道数(灰度图像=1,RGB图像=3)
out_channels卷积核的数量(输出通道数)
kernel_size卷积核大小(如 3(3,3)
stride步长(默认 1,控制卷积核滑动的步幅)
padding填充(如 1,保持输入输出尺寸一致)
dilation空洞卷积(控制卷积核的扩张间隔)
groups组卷积(groups=1 为标准卷积)
bias是否使用偏置项(默认 True
padding_mode填充模式(zeros, reflect, replicate, circular

2. 示例:定义基本 Conv2d

import torch
import torch.nn as nn

# 定义 2D 卷积层
conv = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1)

# 假设输入为 (batch_size=1, channels=3, height=32, width=32)
input_tensor = torch.randn(1, 3, 32, 32)

# 计算输出
output = conv(input_tensor)
print(output.shape)  # 输出: torch.Size([1, 16, 32, 32])

解析

  • in_channels=3(输入通道数,RGB 图像)。
  • out_channels=16(16 个卷积核,输出通道数)。
  • kernel_size=3(使用 3x3 卷积核)。
  • padding=1保持尺寸不变,即 H_out = H_in)。
  • stride=1(步长为 1,逐像素滑动)。
  • 输出形状 (1, 16, 32, 32),表示:
    • 1 是 batch_size。
    • 16 是输出通道数(16 个卷积核)。
    • 32 × 32 是特征图大小。

3. 计算卷积输出尺寸

卷积输出尺寸计算公式:
H out = H in + 2 P − K S + 1 H_{\text{out}} = \frac{H_{\text{in}} + 2P - K}{S} + 1 Hout=SHin+2PK+1
W out = W in + 2 P − K S + 1 W_{\text{out}} = \frac{W_{\text{in}} + 2P - K}{S} + 1 Wout=SWin+2PK+1
其中:

  • H in , W in H_{\text{in}}, W_{\text{in}} Hin,Win 是输入特征图的高宽。
  • P P P 是填充(padding)。
  • K K K 是卷积核大小(kernel_size)。
  • S S S 是步长(stride)。

4. paddingstride 的作用

示例 1:默认 padding=0

conv = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=0)
input_tensor = torch.randn(1, 3, 32, 32)
output = conv(input_tensor)
print(output.shape)  # torch.Size([1, 16, 30, 30])
  • 由于 padding=0,输出尺寸 变小

示例 2:padding=1(保持尺寸不变)

conv = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1)
input_tensor = torch.randn(1, 3, 32, 32)
output = conv(input_tensor)
print(output.shape)  # torch.Size([1, 16, 32, 32])
  • padding=1 确保输入输出 尺寸相同

示例 3:stride=2(步长 2,降采样)

conv = nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1)
input_tensor = torch.randn(1, 3, 32, 32)
output = conv(input_tensor)
print(output.shape)  # torch.Size([1, 16, 16, 16])
  • stride=2 使输出尺寸 缩小为 1/2

5. dilation(空洞卷积)

dilation 控制卷积核的 扩张间隔,适用于 感受野增大 的任务(如目标检测)。

conv = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=2, dilation=2)
input_tensor = torch.randn(1, 3, 32, 32)
output = conv(input_tensor)
print(output.shape)  # torch.Size([1, 16, 32, 32])
  • dilation=2 扩大了感受野,但 保持输出尺寸不变

6. groups(组卷积)

groups > 1 用于 分组卷积(Grouped Convolution),如 MobileNet

conv = nn.Conv2d(in_channels=8, out_channels=16, kernel_size=3, groups=2)
input_tensor = torch.randn(1, 8, 32, 32)
output = conv(input_tensor)
print(output.shape)  # torch.Size([1, 16, 30, 30])
  • groups=2 将输入通道分为 2 组,每组独立执行卷积。

7. 适用场景

  • CNN 图像分类(如 ResNet, VGG, EfficientNet)
  • 目标检测(如 YOLO, Faster R-CNN)
  • 语义分割(如 U-Net, DeepLabV3)
  • 风格迁移、超分辨率(如 SRGAN)

8. 结论

  • torch.nn.Conv2d 是 PyTorch 处理图像的核心组件
  • padding=1 保持尺寸,stride=2 进行降采样
  • dilation 增大感受野,groups 实现分组卷积
  • 适用于 CNN、目标检测、语义分割等任务

正确理解 Conv2d 参数对于 优化 CNN 结构 至关重要。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彬彬侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值