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

相关推荐
精英的英2 小时前
记一次 i.MX6 SL YOLO 模型编译和性能调优
yolo
一碗白开水一16 小时前
入门实践工程二:基于 YOLOv8 的日常物品目标检测|附:环境依赖环境及工程源码
yolo·目标检测·目标跟踪
探物 AI21 小时前
yolov5里面的骨干组件14:C3模块 (CSP Bottleneck with 3 Convolutions)
yolo
雪的季节1 天前
不安装YOLO只安装 PyTorch,加载已有yolo数据,从无到有创建模型训练数据并加载使用(重要)
人工智能·pytorch·yolo
guo_xiao_xiao_1 天前
YOLO[室内与室外多场景混凝土块目标检测]目标检测数据集
yolo·目标检测·目标跟踪
江畔柳前堤1 天前
YOLO 目标检测全流程深度剖析
人工智能·yolo·目标检测·计算机视觉·unity·面试·vllm
guo_xiao_xiao_1 天前
YOLO[野外溪流与人工水域][鱼]目标检测数据集
yolo·目标检测·目标跟踪
智购科技智能售货柜1 天前
自动售货机商品识别YOLO模型训练实战:从6万张图片到98%识别率的完整复盘~YH
运维·服务器·数据库·人工智能·redis·物联网·yolo
CoderIsArt1 天前
SAHI with YOLOv5 for Sliced Inference
人工智能·yolo