Ultralytics:解读Bottleneck模块

Ultralytics:解读Bottleneck模块

前言

相关介绍

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

Bottleneck(标准瓶颈模块)

Bottleneck 是一种经典的残差模块,广泛用于 ResNet、YOLO 等网络结构中。它通过两个卷积层(先降维后升维)减少计算量,并支持残差连接(shortcut),从而在增加网络深度的同时保持训练稳定性。该实现参考了 YOLOv5/YOLOv8 的设计,提供了灵活的参数配置。


代码实现

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 Bottleneck(nn.Module):
    """Standard bottleneck."""

    def __init__(
        self, c1: int, c2: int, shortcut: bool = True, g: int = 1, k: tuple[int, int] = (3, 3), e: float = 0.5
    ):
        """Initialize a standard bottleneck module.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            shortcut (bool): Whether to use shortcut connection.
            g (int): Groups for convolutions.
            k (tuple): Kernel sizes for convolutions.
            e (float): Expansion ratio.
        """
        super().__init__()
        c_ = int(c2 * e)  # hidden channels
        self.cv1 = Conv(c1, c_, k[0], 1)
        self.cv2 = Conv(c_, c2, k[1], 1, g=g)
        self.add = shortcut and c1 == c2

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """Apply bottleneck with optional shortcut connection."""
        return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))

功能

  • 特征压缩与恢复 :通过 c_ = int(c2 * e) 将通道压缩(通常 e=0.5 即减半),然后通过第二个卷积恢复,减少计算量。
  • 残差连接 :当 shortcut=True 且输入输出通道数相同时,将输入直接与输出相加,促进梯度流动。
  • 灵活配置:支持分组卷积、自定义卷积核大小,适应不同网络设计。

初始化参数

参数 类型 说明
c1 int 输入通道数
c2 int 输出通道数
shortcut bool 是否启用残差连接(默认 True)
g int 分组卷积的组数(默认 1)
k tupleint, int 两个卷积的核大小,默认 (3, 3)
e float 通道扩展比,隐藏层通道数 = c2 * e(默认 0.5)

前向方法

  • forward(x):输入 x,输出形状 [B, c2, H, W](空间尺寸不变)。

计算流程

  1. y = self.cv1(x):第一个卷积,通道数变为 c_
  2. z = self.cv2(y):第二个卷积,通道数恢复为 c2
  3. self.add 为真(shortcut=Truec1 == c2),返回 x + z;否则返回 z

使用示例

python 复制代码
if __name__ == '__main__':
    # 1. 创建随机输入
    x = torch.randn(1, 32, 64, 64)

    # 2. 创建 Bottleneck 模块:输入32,输出64,shortcut=False(因为通道不等)
    bottleneck1 = Bottleneck(c1=32, c2=64, shortcut=True, e=0.5, k=(3,3))

    # 3. 前向传播
    with torch.no_grad():
        out1 = bottleneck1(x)
    print("输入形状:", x.shape)       # [1, 32, 64, 64]
    print("输出形状:", out1.shape)    # [1, 64, 64, 64]

    # 4. 创建通道相等且启用shortcut的模块
    bottleneck2 = Bottleneck(c1=64, c2=64, shortcut=True, e=0.5)
    with torch.no_grad():
        out2 = bottleneck2(out1)
    print("shortcut 输出形状:", out2.shape)  # [1, 64, 64, 64]

    # 5. 使用真实图像演示(单通道扩展为多通道)
    img_path = "cat_640x640.png"
    img_bgr = cv2.imread(img_path)
    if img_bgr is not None:
        # 缩放到 64x64,转为灰度图
        img_gray = cv2.cvtColor(cv2.resize(img_bgr, (64, 64)), cv2.COLOR_BGR2GRAY)
        img_tensor = torch.from_numpy(img_gray).float().unsqueeze(0).unsqueeze(0)  # [1,1,64,64]
        # 扩展通道数至 32(模拟特征图)
        x_img = img_tensor.repeat(1, 32, 1, 1)  # [1,32,64,64]
        
        # 创建 Bottleneck(输入32,输出32,shortcut启用)
        bottleneck_img = Bottleneck(c1=32, c2=32, shortcut=True, e=0.5)
        with torch.no_grad():
            out_img = bottleneck_img(x_img)
        
        # 可视化:输入通道0、输出通道0
        inp_ch0 = x_img[0, 0].cpu().numpy()
        out_ch0 = out_img[0, 0].cpu().numpy()
        
        def norm(arr):
            return (arr - arr.min()) / (arr.max() - arr.min() + 1e-8)
        
        plt.figure(figsize=(12, 5), constrained_layout=True)
        plt.subplot(1, 3, 1)
        plt.imshow(img_gray, cmap='gray')
        plt.title("Original Gray")
        plt.axis("off")
        plt.subplot(1, 3, 2)
        plt.imshow(norm(inp_ch0), cmap='gray')
        plt.title("Input Ch0")
        plt.axis("off")
        plt.subplot(1, 3, 3)
        plt.imshow(norm(out_ch0), cmap='gray')
        plt.title("Bottleneck Output Ch0")
        plt.axis("off")
        plt.savefig("bottleneck_demo.png", dpi=150)
        print("可视化已保存为 bottleneck_demo.png")

输出示例

复制代码
输入形状: torch.Size([1, 32, 64, 64])
输出形状: torch.Size([1, 64, 64, 64])
shortcut 输出形状: torch.Size([1, 64, 64, 64])
可视化已保存为 bottleneck_demo.png

流程示意图


代码解读

  • __init__
    • c_ = int(c2 * e):计算隐藏层通道数,通常小于 c2,实现降维。
    • self.cv1:第一个卷积,使用核大小 k[0](默认 3),步长 1,将通道从 c1 映射到 c_
    • self.cv2:第二个卷积,使用核大小 k[1](默认 3),步长 1,分组数 g,将通道从 c_ 映射到 c2
    • self.add:布尔值,决定是否启用残差连接(条件为 shortcut=Truec1 == c2)。
  • forward
    • self.add 为真,返回 x + self.cv2(self.cv1(x)),否则仅返回 self.cv2(self.cv1(x))

注意事项

  1. 空间尺寸不变:所有卷积步长均为 1,填充自动为 same,因此输入输出空间尺寸相同。
  2. 残差条件 :只有 shortcut=True c1 == c2 时才会添加残差,否则无残差。
  3. 扩展比 e:通常取 0.5,使隐藏层通道减半,降低参数量。也可根据需要调整(如 1.0 表示不压缩)。
  4. 分组卷积g 用于第二个卷积,可减少参数量(如深度可分离卷积)。
  5. 核大小灵活性:允许分别指定两个卷积的核大小,适应不同感受野需求。

优缺点

优点
  1. 参数高效:通过先降维后升维,减少计算量和参数量,提高模型效率。
  2. 残差机制:保持梯度传递,便于训练深层网络。
  3. 灵活配置:支持 shortcut 开关、分组卷积、自定义核大小,适配多种场景。
  4. 与残差网络兼容:可无缝替换标准 ResNet 瓶颈块。
缺点
  1. 表达能力受限 :降维比 e 较小时可能损失部分特征信息。
  2. 无跨层融合:与 CSP 结构相比,缺少特征复用机制,梯度多样性稍弱。
  3. 对通道数敏感 :若 c1c2 相差较大,残差失效,可能影响训练。

在 YOLOv5/YOLOv8 中,Bottleneck 是构建 C2fC3 模块的基础单元。实际使用时,可根据任务设置合适的 egk,并注意在深层网络中合理启用 shortcut。

参考文献

1 https://docs.ultralytics.com/

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