YOLOv10改进教程|C2f-CIB加入注意力机制


一、 导读

论文链接:https://arxiv.org/abs/2311.11587

代码链接:GitHub - CV-ZhangXin/AKConv

YOLOv10训练、验证及推理教程


二、 C2f-CIB加入注意力机制

2.1 复制代码

打开ultralytics->nn->modules->block.py文件,复制SE注意力机制(也可以自行换成别的)代码,并创建C2fCIBAttention代码,如下图所示:

class SE(nn.Module):
    def __init__(self, channel, reduction=16):
        super().__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Sequential(
            nn.Linear(channel, channel // reduction, bias=False),
            nn.ReLU(inplace=True),
            nn.Linear(channel // reduction, channel, bias=False),
            nn.Sigmoid()
        )

    def forward(self, x):
        b, c, _, _ = x.size()
        y = self.avg_pool(x).view(b, c)
        y = self.fc(y).view(b, c, 1, 1)
        return x * y.expand_as(x)


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

    def __init__(self, c1, c2, n=1, shortcut=False, lk=False, g=1, e=0.5):
        """Initialize CSP bottleneck layer with two convolutions with arguments ch_in, ch_out, number, shortcut, groups,
        expansion.
        """
        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(CIB(self.c, self.c, shortcut, e=1.0, lk=lk) for _ in range(n))
        self.atten = SE(C2)

    def forward(self, x):
        """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.atten(self.cv2(torch.cat(y, 1)))

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

并在上方声明C2fCIBAttention类。

在nn.models.init.py中声明 C2fCIBAttention。

2.2 修改tasks.py

打开ultralytics->nn->tasks.py,如图所示操作。

​2.3 修改yolov10n.yaml

将yolov10n.yaml文件中的C2fCIB替换为C2fCIBAttention。

# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLOv10 object detection model. For Usage examples see https://docs.ultralytics.com/tasks/detect

# Parameters
nc: 80 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
  # [depth, width, max_channels]
  n: [0.33, 0.25, 1024]

backbone:
  # [from, repeats, module, args]
  - [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
  - [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
  - [-1, 3, C2f, [128, True]]
  - [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
  - [-1, 6, C2f, [256, True]]
  - [-1, 1, SCDown, [512, 3, 2]] # 5-P4/16
  - [-1, 6, C2f, [512, True]]
  - [-1, 1, SCDown, [1024, 3, 2]] # 7-P5/32
  - [-1, 3, C2f, [1024, True]]
  - [-1, 1, SPPF, [1024, 5]] # 9
  - [-1, 1, PSA, [1024]] # 10

# YOLOv8.0n head
head:
  - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  - [[-1, 6], 1, Concat, [1]] # cat backbone P4
  - [-1, 3, C2f, [512]] # 13

  - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  - [[-1, 4], 1, Concat, [1]] # cat backbone P3
  - [-1, 3, C2f, [256]] # 16 (P3/8-small)

  - [-1, 1, Conv, [256, 3, 2]]
  - [[-1, 13], 1, Concat, [1]] # cat head P4
  - [-1, 3, C2f, [512]] # 19 (P4/16-medium)

  - [-1, 1, SCDown, [512, 3, 2]]
  - [[-1, 10], 1, Concat, [1]] # cat head P5
  - [-1, 3, C2fCIBAttention, [1024, True, True]] # 22 (P5/32-large)

  - [[16, 19, 22], 1, v10Detect, [nc]] # Detect(P3, P4, P5)

2.5 修改train.py文件

在train.py脚本中填入yolov10n.yaml路径,运行即可训练。


相关推荐
大象机器人4 小时前
交互式乘法 myCobot 让学习充满乐趣!
人工智能·python·学习·yolo·机械臂·具身智能
阿_旭20 小时前
基于YOLOv10深度学习的CT扫描图像肾结石智能检测系统【python源码+Pyqt5界面+数据集+训练代码】深度学习实战、目标检测
人工智能·python·深度学习·yolo·yolov10
FL16238631291 天前
[数据集][目标检测]桥梁检测数据集VOC+YOLO格式1116张1类别
深度学习·yolo·目标检测
唐豆豆*2 天前
将深度相机的实时三维坐标数据保存为excel文档
数码相机·yolo
FL16238631292 天前
[数据集][目标检测]水面垃圾水面漂浮物检测数据集VOC+YOLO格式3749张1类别
人工智能·yolo·目标检测
大霸王龙2 天前
YOLO在目标检测与视频轨迹追踪中的应用
大数据·人工智能·python·yolo·目标检测·目标跟踪
Papicatch2 天前
人工智能--目标检测
人工智能·python·yolo·目标检测·r-cnn
FL16238631293 天前
[数据集][目标检测]游泳者溺水检测数据集VOC+YOLO格式4599张2类别
人工智能·yolo·目标检测
1037号森林里一段干木头3 天前
yolov8对新的数据集自动标注
开发语言·python·yolo