Ultralytics:解读C2fCIB模块

Ultralytics:解读C2fCIB模块

前言

相关介绍

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

C2fCIB(C2f 模块 + CIB 模块)

C2fCIB 是 YOLOv8 中 C2f 模块的一个变体,它将内部的 Bottleneck 块替换为 CIB(Compact Inverted Block) 模块。CIB 是一种轻量级倒置瓶颈结构,包含深度卷积、1×1 卷积和可选的 RepVGGDW 分支,在保持高效计算的同时提升了特征表达能力。C2fCIB 继承了 C2f 的 CSP 级联结构,因此拥有丰富的梯度流和出色的速度-精度权衡。


代码实现

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

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))

def fuse_conv_and_bn(conv, bn):
    """Fuse Conv2d and BatchNorm2d layers for inference optimization.

    Args:
        conv (nn.Conv2d): Convolutional layer to fuse.
        bn (nn.BatchNorm2d): Batch normalization layer to fuse.

    Returns:
        (nn.Conv2d): The fused convolutional layer with gradients disabled.

    Examples:
        >>> conv = nn.Conv2d(3, 16, 3)
        >>> bn = nn.BatchNorm2d(16)
        >>> fused_conv = fuse_conv_and_bn(conv, bn)
    """
    # Compute fused weights
    w_conv = conv.weight.view(conv.out_channels, -1)
    w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var)))
    conv.weight.data = torch.mm(w_bn, w_conv).view(conv.weight.shape)

    # Compute fused bias
    b_conv = (
        torch.zeros(conv.out_channels, device=conv.weight.device, dtype=conv.weight.dtype)
        if conv.bias is None
        else conv.bias
    )
    b_bn = bn.bias - bn.weight.mul(bn.running_mean).div(torch.sqrt(bn.running_var + bn.eps))
    fused_bias = torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn

    if conv.bias is None:
        conv.register_parameter("bias", nn.Parameter(fused_bias))
    else:
        conv.bias.data = fused_bias

    return conv.requires_grad_(False)

class RepVGGDW(torch.nn.Module):
    """RepVGGDW is a class that represents a depth-wise convolutional block in RepVGG architecture."""

    def __init__(self, ed: int) -> None:
        """Initialize RepVGGDW module.

        Args:
            ed (int): Input and output channels.
        """
        super().__init__()
        self.conv = Conv(ed, ed, 7, 1, 3, g=ed, act=False)
        self.conv1 = Conv(ed, ed, 3, 1, 1, g=ed, act=False)
        self.dim = ed
        self.act = nn.SiLU()

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """Perform a forward pass of the RepVGGDW block.

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

        Returns:
            (torch.Tensor): Output tensor after applying the depth-wise convolution.
        """
        return self.act(self.conv(x) + self.conv1(x))

    def forward_fuse(self, x: torch.Tensor) -> torch.Tensor:
        """Perform a forward pass of the fused RepVGGDW block.

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

        Returns:
            (torch.Tensor): Output tensor after applying the depth-wise convolution.
        """
        return self.act(self.conv(x))

    @torch.no_grad()
    def fuse(self):
        """Fuse the convolutional layers in the RepVGGDW block.

        This method fuses the convolutional layers and updates the weights and biases accordingly.
        """
        if not hasattr(self, "conv1"):
            return  # already fused
        conv = fuse_conv_and_bn(self.conv.conv, self.conv.bn)
        conv1 = fuse_conv_and_bn(self.conv1.conv, self.conv1.bn)

        conv_w = conv.weight
        conv_b = conv.bias
        conv1_w = conv1.weight
        conv1_b = conv1.bias

        conv1_w = torch.nn.functional.pad(conv1_w, [2, 2, 2, 2])

        final_conv_w = conv_w + conv1_w
        final_conv_b = conv_b + conv1_b

        conv.weight.data.copy_(final_conv_w)
        conv.bias.data.copy_(final_conv_b)

        self.conv = conv
        del self.conv1

class CIB(nn.Module):
    """Compact Inverted Block (CIB) module.

    Args:
        c1 (int): Number of input channels.
        c2 (int): Number of output channels.
        shortcut (bool, optional): Whether to add a shortcut connection. Defaults to True.
        e (float, optional): Scaling factor for the hidden channels. Defaults to 0.5.
        lk (bool, optional): Whether to use RepVGGDW for the third convolutional layer. Defaults to False.
    """

    def __init__(self, c1: int, c2: int, shortcut: bool = True, e: float = 0.5, lk: bool = False):
        """Initialize the CIB module.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            shortcut (bool): Whether to use shortcut connection.
            e (float): Expansion ratio.
            lk (bool): Whether to use RepVGGDW.
        """
        super().__init__()
        c_ = int(c2 * e)  # hidden channels
        self.cv1 = nn.Sequential(
            Conv(c1, c1, 3, g=c1),
            Conv(c1, 2 * c_, 1),
            RepVGGDW(2 * c_) if lk else Conv(2 * c_, 2 * c_, 3, g=2 * c_),
            Conv(2 * c_, c2, 1),
            Conv(c2, c2, 3, g=c2),
        )

        self.add = shortcut and c1 == c2

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """Forward pass of the CIB module.

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

        Returns:
            (torch.Tensor): Output tensor.
        """
        return x + self.cv1(x) if self.add else self.cv1(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))

class C2f(nn.Module):
    """Faster Implementation of CSP Bottleneck with 2 convolutions."""

    def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = False, g: int = 1, e: float = 0.5):
        """Initialize a CSP bottleneck with 2 convolutions.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of Bottleneck blocks.
            shortcut (bool): Whether to use shortcut connections.
            g (int): Groups for convolutions.
            e (float): Expansion ratio.
        """
        super().__init__()
        self.c = int(c2 * e)  # hidden channels
        self.cv1 = Conv(c1, 2 * self.c, 1, 1)
        self.cv2 = Conv((2 + n) * self.c, c2, 1)  # optional act=FReLU(c2)
        self.m = nn.ModuleList(Bottleneck(self.c, self.c, shortcut, g, k=((3, 3), (3, 3)), e=1.0) for _ in range(n))

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """Forward pass through C2f layer."""
        y = list(self.cv1(x).chunk(2, 1))
        y.extend(m(y[-1]) for m in self.m)
        return self.cv2(torch.cat(y, 1))

    def forward_split(self, x: torch.Tensor) -> torch.Tensor:
        """Forward pass using split() instead of chunk()."""
        y = self.cv1(x).split((self.c, self.c), 1)
        y = [y[0], y[1]]
        y.extend(m(y[-1]) for m in self.m)
        return self.cv2(torch.cat(y, 1))

class C2fCIB(C2f):
    """C2fCIB class represents a convolutional block with C2f and CIB modules.

    Args:
        c1 (int): Number of input channels.
        c2 (int): Number of output channels.
        n (int, optional): Number of CIB modules to stack. Defaults to 1.
        shortcut (bool, optional): Whether to use shortcut connection. Defaults to False.
        lk (bool, optional): Whether to use large kernel. Defaults to False.
        g (int, optional): Number of groups for grouped convolution. Defaults to 1.
        e (float, optional): Expansion ratio for CIB modules. Defaults to 0.5.
    """

    def __init__(
        self, c1: int, c2: int, n: int = 1, shortcut: bool = False, lk: bool = False, g: int = 1, e: float = 0.5
    ):
        """Initialize C2fCIB module.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of CIB modules.
            shortcut (bool): Whether to use shortcut connection.
            lk (bool): Whether to use large kernel.
            g (int): Groups for convolutions.
            e (float): Expansion ratio.
        """
        super().__init__(c1, c2, n, shortcut, g, e)
        self.m = nn.ModuleList(CIB(self.c, self.c, shortcut, e=1.0, lk=lk) for _ in range(n))

功能

  • CSP 级联结构 :与 C2f 相同,通过 cv1 将输入通道扩展为 2*self.c,拆分后一路直接保留,另一路依次通过 nCIB 模块,每次输出都追加到特征列表,最终拼接并压缩。
  • CIB 核心优势 :每个 CIB 模块基于倒置瓶颈设计,内部包含 1×1 卷积升维、深度卷积、1×1 卷积降维,并支持可选的 RepVGGDW 分支,在训练时提供多分支增益,推理时可融合加速。
  • 灵活配置 :通过 lk 参数控制是否启用 RepVGGDW,允许在精度和速度之间权衡。

初始化参数

参数 类型 说明
c1 int 输入通道数
c2 int 输出通道数
n int CIB 模块的数量(默认 1)
shortcut bool CIB 内部是否使用残差连接(默认 False)
lk bool 是否使用 RepVGGDW 替换 CIB 中的标准深度卷积(默认 False)
g int 分组卷积的组数(作用于 CIB 中的卷积,但 CIB 内部固定为深度卷积,此参数作用有限)
e float 扩展比,self.c = int(c2 * e)(默认 0.5)
  • 父类 C2f 会创建 self.cv1self.cv2 和初始的 self.mBottleneck 列表),但子类立即覆盖 self.mCIB 列表。
  • 每个 CIB 的输入/输出通道均为 self.c,且 e=1.0(不压缩通道),shortcutlk 参数传递。

前向方法

  • forward(x):继承自 C2f,完全复用其逻辑,仅 self.m 中的模块类型不同。
  • forward_split(x):同样继承,使用 split 拆分。

数据流(与 C2f 一致):

  1. y = self.cv1(x)[B, 2*self.c, H, W]
  2. 拆分为 [y0, y1],各 [B, self.c, H, W]
  3. self.m 中的每个 CIB,取当前列表最后一个元素输入,输出追加到列表
  4. 拼接所有 (2+n) 个张量
  5. 通过 self.cv2 压缩到 c2

使用示例

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

    # 2. 创建 C2fCIB(默认不使用 RepVGGDW)
    model = C2fCIB(c1=32, c2=64, n=2, shortcut=False, lk=False, e=0.5)

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

    # 4. 使用真实图像演示(扩展为多通道)
    img_path = "cat_640x640.png"
    img_bgr = cv2.imread(img_path)
    if img_bgr is not None:
        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]
        # 扩展通道至 c1=32
        x_img = img_tensor.repeat(1, 32, 1, 1)

        model_img = C2fCIB(c1=32, c2=32, n=1, shortcut=False, lk=False, e=0.5)
        with torch.no_grad():
            out_img = model_img(x_img)

        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("C2fCIB Output Ch0")
        plt.axis("off")
        plt.savefig("c2fcib_demo.png", dpi=150)
        print("可视化已保存为 c2fcib_demo.png")

输出示例

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

流程示意图

#mermaid-svg-TitwZFLReyQyXwAv{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-TitwZFLReyQyXwAv .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-TitwZFLReyQyXwAv .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-TitwZFLReyQyXwAv .error-icon{fill:#552222;}#mermaid-svg-TitwZFLReyQyXwAv .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-TitwZFLReyQyXwAv .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-TitwZFLReyQyXwAv .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-TitwZFLReyQyXwAv .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-TitwZFLReyQyXwAv .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-TitwZFLReyQyXwAv .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-TitwZFLReyQyXwAv .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-TitwZFLReyQyXwAv .marker{fill:#333333;stroke:#333333;}#mermaid-svg-TitwZFLReyQyXwAv .marker.cross{stroke:#333333;}#mermaid-svg-TitwZFLReyQyXwAv svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-TitwZFLReyQyXwAv p{margin:0;}#mermaid-svg-TitwZFLReyQyXwAv .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-TitwZFLReyQyXwAv .cluster-label text{fill:#333;}#mermaid-svg-TitwZFLReyQyXwAv .cluster-label span{color:#333;}#mermaid-svg-TitwZFLReyQyXwAv .cluster-label span p{background-color:transparent;}#mermaid-svg-TitwZFLReyQyXwAv .label text,#mermaid-svg-TitwZFLReyQyXwAv span{fill:#333;color:#333;}#mermaid-svg-TitwZFLReyQyXwAv .node rect,#mermaid-svg-TitwZFLReyQyXwAv .node circle,#mermaid-svg-TitwZFLReyQyXwAv .node ellipse,#mermaid-svg-TitwZFLReyQyXwAv .node polygon,#mermaid-svg-TitwZFLReyQyXwAv .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-TitwZFLReyQyXwAv .rough-node .label text,#mermaid-svg-TitwZFLReyQyXwAv .node .label text,#mermaid-svg-TitwZFLReyQyXwAv .image-shape .label,#mermaid-svg-TitwZFLReyQyXwAv .icon-shape .label{text-anchor:middle;}#mermaid-svg-TitwZFLReyQyXwAv .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-TitwZFLReyQyXwAv .rough-node .label,#mermaid-svg-TitwZFLReyQyXwAv .node .label,#mermaid-svg-TitwZFLReyQyXwAv .image-shape .label,#mermaid-svg-TitwZFLReyQyXwAv .icon-shape .label{text-align:center;}#mermaid-svg-TitwZFLReyQyXwAv .node.clickable{cursor:pointer;}#mermaid-svg-TitwZFLReyQyXwAv .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-TitwZFLReyQyXwAv .arrowheadPath{fill:#333333;}#mermaid-svg-TitwZFLReyQyXwAv .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-TitwZFLReyQyXwAv .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-TitwZFLReyQyXwAv .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-TitwZFLReyQyXwAv .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-TitwZFLReyQyXwAv .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-TitwZFLReyQyXwAv .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-TitwZFLReyQyXwAv .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-TitwZFLReyQyXwAv .cluster text{fill:#333;}#mermaid-svg-TitwZFLReyQyXwAv .cluster span{color:#333;}#mermaid-svg-TitwZFLReyQyXwAv div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-TitwZFLReyQyXwAv .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-TitwZFLReyQyXwAv rect.text{fill:none;stroke-width:0;}#mermaid-svg-TitwZFLReyQyXwAv .icon-shape,#mermaid-svg-TitwZFLReyQyXwAv .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-TitwZFLReyQyXwAv .icon-shape p,#mermaid-svg-TitwZFLReyQyXwAv .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-TitwZFLReyQyXwAv .icon-shape .label rect,#mermaid-svg-TitwZFLReyQyXwAv .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-TitwZFLReyQyXwAv .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-TitwZFLReyQyXwAv .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-TitwZFLReyQyXwAv :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 输入 x (B, c1, H, W)
Conv 1x1: c1 → 2*c (cv1)
chunk(2,1): y0, y1
y0 直接保存
y1
CIB 1
输出 y2
CIB 2
...
CIB n
输出 y_{n+1}
拼接: y0, y1, y2, ..., y_{n+1}
Conv 1x1: (2+n)*c → c2 (cv2)
输出 (B, c2, H, W)


代码解读

  • __init__
    • 调用父类 C2f.__init__,会创建 self.cself.cv1self.cv2 和初始的 self.m
    • 然后立即用 nn.ModuleList 覆盖 self.m,其中每个元素为 CIB(self.c, self.c, shortcut=shortcut, e=1.0, lk=lk)
    • g 参数在父类中用于 Bottleneck,但在 CIB 中未直接使用(因为 CIB 内部固定使用深度卷积),因此 g 参数在这里实际影响不大(仅传递给父类,但父类并未在 self.m 中使用 g,因为覆盖后 CIB 不使用 g)。
  • forward / forward_split :直接继承自 C2f,逻辑相同,无需重写。

注意事项

  1. lk 参数 :若设置为 TrueCIB 中的深度卷积将被替换为 RepVGGDW(包含 3×3 和 7×7 双分支),训练时提升精度,但推理前需要调用所有 RepVGGDWfuse() 方法进行融合,否则前向会使用双分支,降低速度。
  2. shortcut 默认 False :与 C2f 一致,默认无残差,可减少计算量。
  3. 通道约束self.c = int(c2 * e) 必须为正整数,且 CIB 要求输入/输出通道相等(此处均为 self.c),因此满足。
  4. 继承关系 :由于 C2fCIB 重写了 self.m,父类中用于生成 Bottleneckng 参数实际未影响 self.m,因此若需要分组卷积,需在 CIB 中自行支持(但 CIB 当前未提供 g 参数)。
  5. C2f 的兼容性C2fCIBC2f 接口完全一致,可无缝替换,只需额外传递 lk 参数。

优缺点

优点
  1. 效率更高CIB 采用倒置瓶颈和深度卷积,相比 Bottleneck 参数量和 FLOPs 更低。
  2. 可重参数化 :通过 lk=True 启用 RepVGGDW,训练时多分支提升性能,推理时融合保持速度。
  3. 级联梯度流 :继承 C2f 的级联结构,多个分支提供丰富梯度,训练收敛快。
  4. 灵活配置 :通过 lk 控制是否使用 RepVGGDW,方便在不同资源场景下切换。
缺点
  1. 依赖外部模块 :需要 CIBRepVGGDW 配合,模块间耦合度较高。
  2. lk=True 时需手动融合 :推理前必须对所有 RepVGGDW 调用 fuse(),否则速度优势消失。
  3. g 参数无效 :由于 CIB 内部固定使用深度卷积,g 参数在 C2fCIB 中不起作用,可能造成误解。
  4. shortcut 仅在 CIB 内部 :CSP 级联本身没有 shortcut,仅在每个 CIB 内部可选残差。

在 YOLOv8 的轻量级版本中,C2fCIB 可用作骨干网络的基本构建块,替换 C2f 以提升速度。若需要更高精度,可设置 lk=True,并在训练后执行融合。建议根据实际硬件和任务需求调整 ne 参数。

参考文献

1 https://docs.ultralytics.com/

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

相关推荐
c_lb72881 小时前
2026年AI量化学习流程,从表达开发走到分层验证
人工智能·python
阿虎儿1 小时前
Dify Failed to invoke tool: Text size is too large, max size is 1.00 M
人工智能
半个落月1 小时前
一文讲清 AI Agent:它和普通 AI 对话到底有什么不同?
人工智能
联盟分享专家1 小时前
品牌如何零代码搭建专属联盟营销项目,实现被动增长?
大数据·人工智能
s180579163621 小时前
低成本GEO优化技巧
人工智能·python
阿黎梨梨1 小时前
为什么你的 AI 知识库总是“断章取义”?
人工智能
东风破_1 小时前
注册了 MCP Resource,模型为什么仍然看不到?
人工智能
正律有为1 小时前
跨境手机壳、印花服饰避坑:游戏手柄无缝涂鸦插画版权自查实操步骤
人工智能
无己心1 小时前
中国医美行业GEO白皮书(2026)
人工智能