YOLOv5改进:CA注意力机制【注意力系列篇】(附详细的修改步骤,以及代码)

如果实验环境尚未搭建成功,可以参考这篇文章 ->【YOLOv5超详细环境搭建以及模型训练(GPU版本)】

文章链接为:http://t.csdnimg.cn/Ke0bb

---------------------------------------------------------------------------​

1.基本原理简介

Abstract:Recent studies on mobile network design have demonstrated the remarkable effectiveness of channel attention (e.g., the Squeeze-and-Excitation attention) for lifting model performance, but they generally neglect the positional information, which is important for generating spatially selective attention maps. In this paper, we propose a novel attention mechanism for mobile networks by embedding positional information into channel attention, which we call “coordinate attention”. Unlike channel attention that transforms a feature tensor to a single feature vector via 2D global pooling, the coordinate attention factorizes channel attention into two 1D feature encoding processes that aggregate features along the two spatial directions, respectively. In this way, long-range dependencies can be captured along one spatial direction and meanwhile precise positional information can be preserved along the other spatial direction. The resulting feature maps are then encoded separately into a pair of direction-aware and position-sensitive attention maps that can be complementarily applied to the input feature map to augment the representations of the objects of interest. Our coordinate attention is simple and can be flexibly plugged into classic mobile networks, such as MobileNetV2, MobileNeXt, and EfficientNet with nearly no computational overhead. Extensive experiments demonstrate that our coordinate attention is not only beneficial to ImageNet classification but more interestingly, behaves better in down-stream tasks, such as object detection and semantic segmentation.

摘要:最近关于移动网络设计的研究表明了通道注意力(例如,挤压与激励注意力)对提升模型性能的显著效果,但它们通常忽略了位置信息,而位置信息对于生成空间选择性注意力图至关重要。在本文中,我们提出了一种新颖的用于移动网络的注意机制,通过将位置信息嵌入通道注意力中,我们称之为“坐标注意力”。与通道注意力通过2D全局池化将特征张量转换为单个特征向量不同,坐标注意力将通道注意力分解为两个沿着两个空间方向分别聚合特征的1D特征编码过程。通过这种方式,可以沿着一个空间方向捕捉长距离依赖关系,同时可以保留另一个空间方向的精确位置信息。然后,生成的特征图分别编码为一对方向感知和位置敏感的注意力图,可以互补地应用于输入特征图,以增强感兴趣对象的表示。我们的坐标注意力简单易用,可以灵活地插入经典移动网络,如MobileNetV2、MobileNeXt和EfficientNet,几乎没有计算开销。大量实验表明,我们的坐标注意力不仅有益于ImageNet分类,而且更有趣的是,在下游任务,如目标检测和语义分割中表现更好。

实验对比:

论文地址:https://arxiv.org/pdf/2103.02907.pdf

代码地址:GitHub - houqb/CoordAttention: Code for our CVPR2021 paper coordinate attention

2.将CA模块加入到YOLOv5中

注意:CA 模块是一种即插即用的模块,可以在许多位置添加。不同的数据集可能需要在不同的位置添加 CA 模块,其效果也会有所不同。建议在不同位置进行多次实验以便比较效果。以下是我选择添加CA模块的位置,供大家参考,但不一定要完全按照这种方式添加。

2.1 方法一:在YOLOv5的主干网络(Backbone)中添加CA模块

2.1.1 步骤一

在【modules】目录下新建一个ca.py的文件,添加CA模块代码。代码获取链接:https://mbd.pub/o/bread/mbd-ZpuUmJlx

最后在【moduels/_init_.py】文件里面导入CA模块。

2.1.2 步骤二

打开【moduels/yolo.py】文件,在文件的开头导入CA模块。

然后找到parse_model这个方法(可以通过搜索parse_model)。

最后在parse_model方法中找到for语句(for i, (f, n, m, args) in enumerate(d["backbone"] + d["head"])),在for语句中再添加一个elif语句,将CA模块添加进去。

elif m in {CA}:
    args = [ch[f],  ch[f]]

添加的位置截图如下:

2.1.3 步骤三

修改模型。在【models】目录下新建一个yolov5-ca.yaml文件网络结构配置文件。将CA模块添加到YOLOv5结构中(这里我将CA模块添加到主干网络的倒数第二层)。 代码获取链接:https://mbd.pub/o/bread/mbd-ZpuUmJlx

2.1.4 训练过程

修改train.py文件训练模型,这里只修改YOLO模型对象,改为自己刚刚创建的文件,其余这里选择默认。

成功运行的网络结构截图如下:

开始训练模型的部分截图如下:

训练结果保存在【runs/tain】目录下。

注:如果训练过程出现如下错误,需要打开train.py文件,在scaler.scale(loss).backward()前添加torch.use_deterministic_algorithms(False)即可解决问题

错误提示:

2.2 方法二:在C3模块的残差结构中添加CA模块

2.2.1 步骤一

在【modules】目录下新建一个c3_ca.py的文件.将CA模块添加到C3模块的残差结构中。 代码获取链接:https://mbd.pub/o/bread/mbd-ZpuUmJlx

然后在【moduels/_init_.py】文件里面导入Bottleneck_CA,C3_CA模块。

2.2.2 步骤二

打开【models/yolo.py】文件,在文件的开头导入C3_CA模块。

找到parse_model这个方法(可以通过搜索parse_model)。

然后在parse_model方法中找到for语句(for i, (f, n, m, args) in enumerate(d["backbone"] + d["head"])),在for语句中的两个位置添加C3_CA模块。

2.2.3 步骤三

修改模型。在【models】目录下新建一个yolov5-c3-ca.yaml文件网络结构配置文件。(这里我将主干中的C3模块替换为C3_CA模块,除此之外,也可以将Neck的C3模块替换为C3_CA模块)【代码在购买后可获取】

2.2.4 训练过程

在train.py文件中修改模型的配置文件路径,使用yolov5-c3-ca.yaml文件,然后运行train.py。

成功运行的网络结构截图如下:

开始训练模型的部分截图如下:

训练结果保存在【runs/train】"目录下。

3.完整的项目文件下载路径

我们提供完整的项目文件,你也可以直接下载到本地,然后打开项目,修改数据集配置文件【NEU-DET.yaml】的数据集路径即可训练模型。项目完整代码获取链接:https://mbd.pub/o/bread/mbd-ZpuUmJlx

  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值