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

相关推荐
YOLO数据集集合18 小时前
Deepseek融合yolo的行人车辆多目标检测系统 |行人检测 车辆检测 多目标检测 YOLO DeepSeek9165期
人工智能·yolo·目标检测·计算机视觉·车辆检测·行人检测
YOLO数据集集合1 天前
YOLO 目标检测模型与 DeepSeek/Qwen 大语言模型融合的跌倒检测系统 | 跌倒检测 YOLO DeepSeek Qwen 行为识别 目标检测
yolo·目标检测·语言模型
YOLO数据集集合1 天前
道路与桥梁灾后损伤检测数据集 | 道路损伤 桥梁损坏 灾后评估 无人机巡检 目标检测 YOLO格式 深度学习数据集 计算机视觉10551期
yolo·目标检测·计算机视觉·无人机·桥梁损害·灾后评估
答案是你2 天前
YOLOE 开放词汇检测 + ONNX / TensorRT 推理,开源了!
python·深度学习·yolo·目标检测·计算机视觉·视觉检测
Tingmanyi2 天前
YOLOv13改进策略【卷积层篇】| RFAConv 感受野注意力卷积,每个位置都值得被单独加权
深度学习·算法·yolo·目标检测·计算机视觉
AOI小白新手上路2 天前
AOI 缺陷检测复现实操指南:Anomalib + MVTec AD(glass)与 YOLOv8 + NEU-DET 两条路线
人工智能·深度学习·yolo
YOLO数据集集合3 天前
江西九江地区洪涝无人机高分辨率影像数据集 | 洪涝监测 语义分割 无人机遥感 淹没识别 灾害评估9157期
人工智能·yolo·目标检测·无人机·洪涝·洪水灾害
听风吹等浪起3 天前
第22章:YOLOv5船舶目标检测+注意力模块改进对比,卫星遥感船舶识别实战
人工智能·yolo·目标检测
hhzz4 天前
【YOLO 入门到精通 04】理解任务与模型家族:7 大 CV 任务与 YOLO26 选型指南
人工智能·python·深度学习·yolo·计算机视觉
YOLO数据集集合4 天前
无人机视角工程机械目标检测数据集 | 工程机械检测 无人机航拍 智慧工地9152期
人工智能·yolo·目标检测·计算机视觉·语言模型·无人机·工程车