YOLOv11中C3k2以及常见改进

目录

一、YOLOv11n框架

二、C3k2模块

三、C3k2改进思路

一、YOLOv11n框架

图1 YOLOv11结构图标题

二、C3k2模块

图2 C3k2结构图

C3k2模块是YOLOv11n中的一个核心组件,它是C2f模块的变体,专门设计用于高效的特征提取。该模块的主要特点包括:

  • 双路径结构 :继承自C2f的CSP(Cross Stage Partial)架构,通过两条路径处理特征,一条直接传递另一条经过多个瓶颈块处理,最后合并。
  • 可配置的瓶颈块 :通过c3k参数可以选择使用标准的Bottleneck块或更灵活的C3k块,后者支持自定义卷积核大小。如图2中蓝色的c3k,如果bool=0,时,会换成bottleneck,详情见代码。
  • 高效计算:通过分组卷积和扩张率控制计算复杂度,在保持性能的同时减少参数量和计算量。
  • 灵活扩展 :模块支持通过n参数控制瓶颈块的数量,e参数控制隐藏通道的扩展比例。

C3k2模块在YOLOv11n的骨干网络和颈部网络中广泛使用,能够有效提取多尺度特征,同时保持较高的推理速度。

python 复制代码
class C3k(C3):
    """C3k is a CSP bottleneck module with customizable kernel sizes for feature extraction in neural networks."""

    def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = True, g: int = 1, e: float = 0.5, k: int = 3):
        """
        Initialize C3k module.

        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.
            k (int): Kernel size.
        """
        super().__init__(c1, c2, n, shortcut, g, e)
        c_ = int(c2 * e)  # hidden channels
        # self.m = nn.Sequential(*(RepBottleneck(c_, c_, shortcut, g, k=(k, k), e=1.0) for _ in range(n)))
        self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, k=(k, k), e=1.0) for _ in range(n)))
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 C3k2(C2f):
    """Faster Implementation of CSP Bottleneck with 2 convolutions."""

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

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of blocks.
            c3k (bool): Whether to use C3k blocks.
            e (float): Expansion ratio.
            g (int): Groups for convolutions.
            shortcut (bool): Whether to use shortcut connections.
        """
        super().__init__(c1, c2, n, shortcut, g, e)
        self.m = nn.ModuleList(
            C3k(self.c, self.c, 2, shortcut, g) if c3k else Bottleneck(self.c, self.c, shortcut, g) for _ in range(n)
        )

三、C3k2改进思路以及案例

直接替换bottleneck或者改bottleneck

例子1

例子2

相关推荐
jay神1 天前
一文讲清楚YOLOv26模型
人工智能·深度学习·yolo·机器学习·计算机视觉
qq_25294131681 天前
无人机目标检测数据集(UAV Detection Dataset)| 无人机检测 反无人机 低空安防5001期
人工智能·yolo·目标检测·计算机视觉·视觉检测·无人机·反无人机
YOLO数据集集合1 天前
蚊子视觉验证数据集 | 蚊虫识别 疟疾防控 病媒生物 细粒度分类 YOLO格式 深度学习数据集
深度学习·yolo·分类·数据集·蚊子分类·蚊虫分类·蚊虫识别
java1234_小锋1 天前
YOLO26 计算机视觉 - YOLO26图片与视频推理 & 摄像头与实时检测
人工智能·yolo·计算机视觉·机器视觉·yolo26
YOLO数据集集合2 天前
蚊子目标检测数据集 | 蚊虫检测 病媒生物 智能硬件 轻量化模型 目标检测 YOLO格式 深度学习数据集9015期
深度学习·yolo·目标检测·计算机视觉·分类·智能硬件·蚊子识别
YOLO数据集集合2 天前
煤炭质量目标检测数据集 、| 煤炭检测 工业视觉 质量分级 异物检测 煤炭异物8005期
人工智能·yolo·目标检测·计算机视觉·分类·煤炭检测数据集·异物数据集
qq_25294131682 天前
窗户清洁检测数据集 | 窗户清洁检测 建筑运维 二分类任务 YOLO格式 深度学习数据集9019期
运维·人工智能·深度学习·yolo·目标检测·分类·窗户清晰度
YOLO数据集集合2 天前
尼帕果实目标检测数据集 | 尼帕果实检测 农业视觉 热带作物 目标检测 YOLO格式9013期
人工智能·yolo·目标检测·机器学习·计算机视觉·农业果实
qq_25294131682 天前
航拍垃圾堆检测数据集 | 航拍垃圾检测 固废管理 无人机巡检 环保监测9025期
yolo·目标检测·计算机视觉·分类·无人机·垃圾检测·垃圾
qq_25294131682 天前
建筑物缺陷目标检测数据集 | 建筑裂缝检测 房屋缺陷 结构损伤 目标检测9022期
人工智能·yolo·目标检测·计算机视觉·视觉检测·建筑裂缝·建筑外立面