Ultralytics:解读LightConv模块

Ultralytics:解读LightConv模块

前言

相关介绍

Ultralytics 简介

Ultralytics 基于多年的计算机视觉和人工智能基础研究,创建了最先进的 (SOTA) YOLO 模型。我们的模型不断更新性能和灵活性,快速、准确且易于使用。他们擅长对象检测、跟踪、实例分割、语义分割、图像分类和姿势估计任务。

前提条件

  • 熟悉Python、Pytorch

实验环境

bash 复制代码
Package                  Version
------------------------ ------------
Python                   3.11.8
absl-py                  2.4.0
accelerate               1.13.0
annotated-doc            0.0.4
anyio                    4.13.0
calflops                 0.3.2
certifi                  2026.4.22
charset-normalizer       3.4.7
click                    8.3.3
colorama                 0.4.6
contourpy                1.3.3
cycler                   0.12.1
filelock                 3.29.0
flatbuffers              25.12.19
fonttools                4.62.1
fsspec                   2026.4.0
grpcio                   1.80.0
h11                      0.16.0
hf-xet                   1.5.0
httpcore                 1.0.9
httpx                    0.28.1
huggingface_hub          1.14.0
idna                     3.15
Jinja2                   3.1.6
kiwisolver               1.5.0
Markdown                 3.10.2
markdown-it-py           4.2.0
MarkupSafe               3.0.3
matplotlib               3.10.9
mdurl                    0.1.2
ml_dtypes                0.5.0
mpmath                   1.3.0
networkx                 3.6.1
numpy                    1.26.4
nvidia-cublas-cu12       12.8.3.14
nvidia-cuda-cupti-cu12   12.8.57
nvidia-cuda-nvrtc-cu12   12.8.61
nvidia-cuda-runtime-cu12 12.8.57
nvidia-cudnn-cu12        9.7.1.26
nvidia-cufft-cu12        11.3.3.41
nvidia-cufile-cu12       1.13.0.11
nvidia-curand-cu12       10.3.9.55
nvidia-cusolver-cu12     11.7.2.55
nvidia-cusparse-cu12     12.5.7.53
nvidia-cusparselt-cu12   0.6.3
nvidia-nccl-cu12         2.26.2
nvidia-nvjitlink-cu12    12.8.61
nvidia-nvtx-cu12         12.8.55
onnx                     1.19.0
onnxruntime-gpu          1.26.0
onnxslim                 0.1.94
opencv-python            4.6.0.66
packaging                26.2
pillow                   12.2.0
pip                      24.0
polars                   1.40.1
polars-runtime-32        1.40.1
protobuf                 7.34.1
psutil                   7.2.2
pycocotools              2.0.11
Pygments                 2.20.0
pyparsing                3.3.2
python-dateutil          2.9.0.post0
PyYAML                   6.0.3
regex                    2026.5.9
requests                 2.34.1
rich                     15.0.0
safetensors              0.7.0
scipy                    1.16.0
setuptools               65.5.0
shellingham              1.5.4
six                      1.17.0
sympy                    1.14.0
tabulate                 0.10.0
tensorboard              2.20.0
tensorboard-data-server  0.7.2
tokenizers               0.22.2
torch                    2.7.1+cu128
torchaudio               2.7.1+cu128
torchvision              0.22.1+cu128
tqdm                     4.67.3
transformers             5.8.1
triton                   3.3.1
typer                    0.25.1
typing_extensions        4.15.0
ultralytics              8.4.58
ultralytics-thop         2.0.19
urllib3                  2.7.0
Werkzeug                 3.1.8

LightConv(轻量卷积模块)

LightConv 是一个轻量级卷积模块 ,它将标准卷积分解为两个步骤:1×1 点卷积(Pointwise Convolution) + 深度卷积(Depthwise Convolution) 。这种结构最早在 MobileNet 中提出,被称为 深度可分离卷积(Depthwise Separable Convolution),可大幅减少参数量和计算量,同时保持较高的特征提取能力。本实现参考了 PaddleDetection 的 HGNetV2 骨干网络,但核心思想与其他轻量级模块一致。


代码实现

python 复制代码
import cv2
import math
import torch
import numpy as np
import matplotlib.pyplot as plt
from torch import nn

def autopad(k, p=None, d=1):  # kernel, padding, dilation
    """Pad to 'same' shape outputs."""
    if d > 1:
        k = d * (k - 1) + 1 if isinstance(k, int) else [d * (x - 1) + 1 for x in k]  # actual kernel-size
    if p is None:
        p = k // 2 if isinstance(k, int) else [x // 2 for x in k]  # auto-pad
    return p


class Conv(nn.Module):
    """Standard convolution module with batch normalization and activation.

    Attributes:
        conv (nn.Conv2d): Convolutional layer.
        bn (nn.BatchNorm2d): Batch normalization layer.
        act (nn.Module): Activation function layer.
        default_act (nn.Module): Default activation function (SiLU).
    """

    default_act = nn.SiLU()  # default activation

    def __init__(self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True):
        """Initialize Conv layer with given parameters.

        Args:
            c1 (int): Number of input channels.
            c2 (int): Number of output channels.
            k (int): Kernel size.
            s (int): Stride.
            p (int, optional): Padding.
            g (int): Groups.
            d (int): Dilation.
            act (bool | nn.Module): Activation function.
        """
        super().__init__()
        self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p, d), groups=g, dilation=d, bias=False)
        self.bn = nn.BatchNorm2d(c2)
        self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()

    def forward(self, x):
        """Apply convolution, batch normalization and activation to input tensor.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor.
        """
        return self.act(self.bn(self.conv(x)))

    def forward_fuse(self, x):
        """Apply convolution and activation without batch normalization.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor.
        """
        return self.act(self.conv(x))

class LightConv(nn.Module):
    """Light convolution module with 1x1 and depthwise convolutions.

    This implementation is based on the PaddleDetection HGNetV2 backbone.

    Attributes:
        conv1 (Conv): 1x1 convolution layer.
        conv2 (DWConv): Depthwise convolution layer.
    """

    def __init__(self, c1, c2, k=1, act=nn.ReLU()):
        """Initialize LightConv layer with given parameters.

        Args:
            c1 (int): Number of input channels.
            c2 (int): Number of output channels.
            k (int): Kernel size for depthwise convolution.
            act (nn.Module): Activation function.
        """
        super().__init__()
        self.conv1 = Conv(c1, c2, 1, act=False)
        self.conv2 = DWConv(c2, c2, k, act=act)

    def forward(self, x):
        """Apply 2 convolutions to input tensor.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor.
        """
        return self.conv2(self.conv1(x))


class DWConv(Conv):
    """Depth-wise convolution module."""

    def __init__(self, c1, c2, k=1, s=1, d=1, act=True):
        """Initialize depth-wise convolution with given parameters.

        Args:
            c1 (int): Number of input channels.
            c2 (int): Number of output channels.
            k (int): Kernel size.
            s (int): Stride.
            d (int): Dilation.
            act (bool | nn.Module): Activation function.
        """
        super().__init__(c1, c2, k, s, g=math.gcd(c1, c2), d=d, act=act)

功能

  • 深度可分离卷积 (分离版本):
    • 第 1 步 :1×1 卷积将输入通道数 c1 映射到 c2,进行跨通道特征融合。
    • 第 2 步 :深度卷积(DWConv)对每个通道独立进行空间卷积(核大小 k),提取空间特征。
  • 与标准卷积相比 :参数量从 k²·c1·c2 降为 c1·c2 + k²·c2(当 DWConv 的分组数等于 c2 时),计算量也大幅降低。
  • 激活函数处理
    • 1×1 卷积后不激活act=False),而深度卷积后使用传入的激活函数(默认为 nn.ReLU())。
    • 这种设计符合 MobileNetV2 等网络的经验:通道变换后应进行非线性激活,而 1×1 卷积本身仅做线性变换,可省略激活以提升效率。

初始化参数

参数 类型 说明
c1 int 输入通道数
c2 int 输出通道数
k int 深度卷积的卷积核大小(默认 1)
act nn.Module 深度卷积后的激活函数(默认 nn.ReLU()

Conv 不同,LightConvact 直接传入 nn.Module 实例,而非 bool。这是因为 Conv 可以接受 True/False 来使用默认 SiLU 或 Identity,而 LightConv 固定 1×1 卷积无激活,深度卷积使用指定激活,更加明确。


与标准深度可分离卷积的区别

组件 标准深度可分离(MobileNet) LightConv(本实现)
第1步 Depthwise(g=c1 1×1 点卷积(先升/降维)
第2步 Pointwise(1×1) Depthwise(后空间卷积)
顺序 空间 → 通道 通道 → 空间
典型用途 标准轻量级网络 PaddleDetection HGNetV2 等

两者在数学上等价(线性变换可交换),但 LightConv 的顺序更有利于在 1×1 卷积后立即进行通道压缩,减少后续深度卷积的输入通道数,从而进一步降低计算量(但需要注意通道数 c2 可能很大)。


使用示例

python 复制代码
if __name__ == '__main__':
    # 1. 读取图像
    img_path = "cat_640x640.png"
    img_bgr = cv2.imread(img_path)
    if img_bgr is None:
        raise FileNotFoundError(f"图片 {img_path} 不存在!")

    # 2. 转为张量 (1,3,640,640)
    img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
    img_tensor = torch.from_numpy(img_rgb).float().permute(2, 0, 1).unsqueeze(0)

    # 3. 创建 LightConv 层:输入3通道,输出16通道,深度卷积核3×3,步长1(未显式指定,DWConv默认s=1)
    light_layer = LightConv(c1=3, c2=16, k=3, act=nn.ReLU())

    # 4. 前向传播
    with torch.no_grad():
        out = light_layer(img_tensor)
    print("输出形状:", out.shape)  # torch.Size([1, 16, 640, 640]) 注意步长=1,尺寸不变

    # 5. 可视化第一个通道的特征图
    feat_map = out[0, 0, :, :].cpu().numpy()
    feat_map = (feat_map - feat_map.min()) / (feat_map.max() - feat_map.min() + 1e-8)
    feat_map = (feat_map * 255).astype(np.uint8)

    plt.figure(figsize=(10, 5))
    plt.subplot(1, 2, 1)
    plt.imshow(cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB))
    plt.title("Original")
    plt.axis("off")

    plt.subplot(1, 2, 2)
    plt.imshow(feat_map, cmap='gray')
    plt.title("LightConv Output (Ch0)")
    plt.axis("off")

    plt.tight_layout()
    plt.savefig("lightconv_output.png", dpi=150)
    # plt.show()  # 远程服务器建议注释
    print("可视化已保存为 lightconv_output.png")

输出示例

复制代码
输出形状: torch.Size([1, 16, 640, 640])
可视化已保存为 lightconv_output.png

由于深度卷积的步长默认为 1,且 autopad 确保 s=1 时尺寸不变,因此输出空间尺寸与输入相同。


流程示意图


代码解读

__init__ 方法
  • self.conv1 = Conv(c1, c2, 1, act=False):1×1 标准卷积,无 BN 激活(因为 act=False 会使用 Identity,但 Conv 默认依然包含 BN,即 BN 后接 Identity,相当于仅做归一化)。
  • self.conv2 = DWConv(c2, c2, k, act=act):深度卷积,输入输出通道数均为 c2,分组数为 gcd(c2, c2) = c2,因此每个通道独立卷积,实现真正的深度卷积。
forward 方法
  • conv1,再 conv2
  • 1×1 卷积负责通道混合,深度卷积负责空间特征提取。
DWConv 的协作
  • LightConvDWConv 与点卷积组合,形成完整的轻量级卷积模块。
  • 如果单独使用 DWConv,通道间缺乏信息融合,表达能力不足;LightConv 则补足了这一点。

注意事项

  1. 激活函数的选择 :本实现中 conv1 不包含激活(act=False),而 conv2 使用传入的 act(默认 ReLU)。这符合 MobileNetV2 的设计:在 1×1 卷积后不立即激活,而是先进行 BatchNorm 归一化,避免信息损失。
  2. 步长控制LightConv 本身不支持设置步长(固定为 1),因为深度卷积的步长 s 默认 1。若需下采样,可在 DWConv 中显式传入 s=2,但本接口未暴露。如需下采样,可修改代码增加 s 参数或使用 Conv 替代。
  3. 与标准深度可分离卷积的差异 :本实现是 先点卷积后深度卷积 ,而 MobileNet 是 先深度卷积后点卷积。两者在理论计算量上稍有不同,但实际效果相近,选择取决于网络设计习惯。
  4. 显存与速度:由于分为两次卷积,训练时显存占用略高于标准卷积(因为有中间特征图),但推理时可通过算子融合优化(如 TensorRT 可自动合并 Conv+BN+Act)。
  5. 适用任务:适合轻量级分类、检测、分割等任务,尤其适合移动端和嵌入式平台。

优缺点

优点
  1. 参数和计算量显著下降

    c2 较大时,相比标准卷积,参数量降至约 c1·c2 + k²·c2 vs k²·c1·c2,可节省几十倍。

  2. 结构清晰,易于扩展

    分离的 1×1 和深度卷积独立,可根据需求分别调整(如用更大的核、分组数等)。

  3. 隐式特征重用

    1×1 卷积先进行通道融合,再通过深度卷积提取空间特征,有助于提升信息流。

  4. 与主流轻量级网络兼容

    类似结构在 MobileNet、EfficientNet、HGNet 中被验证有效,可直接嵌入现有模型。

缺点
  1. 表达能力仍受限

    由于深度卷积不跨通道融合,整体容量低于同通道数的标准卷积,精度可能下降。

  2. 内存访问开销

    虽计算量减少,但深度卷积的非连续内存访问可能导致在 GPU 上实测加速比不如理论值。

  3. 灵活性不足

    当前接口未暴露深度卷积的步长、膨胀等参数,若需调整需修改源代码或继承重写。

  4. 激活函数固定模式

    1×1 卷积无激活,若想加入非线性需自定义,限制了设计空间。


在 YOLOv8 中,LightConv 可作为 C2f 内部瓶颈的替代,用于构建轻量级版本(如 YOLOv8‑n/s)。实际使用时,需根据任务调整 k 和激活函数,并在训练时注意收敛速度(可适当增加学习率)。

参考文献

1 https://docs.ultralytics.com/

2 https://github.com/ultralytics/ultralytics.git