马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如果实验环境尚未搭建成功,可以参考这篇文章 ->【YOLOv8超详细环境搭建以及模型训练(GPU版本)】
文章链接为:http://t.csdnimg.cn/8ZmAm
---------------------------------------------------------------------------------------------------------------------------------
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
代码地点:https://github.com/houqb/CoordAttention
2.将CA模块加入到YOLOv8中
注意:CA 模块是一种即插即用的模块,可以在许多位置添加。差别的数据集大概需要在差别的位置添加 CA 模块,其效果也会有所差别。建议在差别位置进行多次实验以便比较效果。以下是我选择添加CA模块的位置,供各人参考,但不肯定要完全按照这种方式添加。
2.1 方法一:在YOLOv8的主干网络(Backbone)中添加CA模块
2.1.1 步骤一
在【ultralytics/nn/modules】目录下新建一个ca.py的文件,添加CA模块代码。代码获取链接:https://mbd.pub/o/bread/mbd-ZpqamZpy
最后在【ultralytics/nn/moduels/_init_.py】文件里面导入CA模块。
2.1.2 步骤二
打开【ultralytics/nn/tasks.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 步骤三
修改模型。在【ultralytics/cfg/models/v8】目录下新建一个yolov8-ca.yaml网络结构配置文件。将CA模块添加到YOLOv8结构中(这里我将CA模块添加到主干网络的倒数第二层)。 代码获取链接:https://mbd.pub/o/bread/mbd-ZpqamZpy
2.1.4 训练过程
为了方便训练,可以在项目目录下新建一个名为train.py的文件,这样以后只需执行train.py就可以开始训练模型了。【代码在购买后可获取】
(如果train.py文件已经存在,只需要修改其中模型和数据集的配置文件路径即可。我这里给出的train.py文件中只是列举了常用的超参数,如果需要修改其他的超参数,可以进入【ultralytics/cfg/default.yaml】文件修改即可。)
成功运行的网络结构截图如下:
开始训练模型的部门截图如下:
训练效果保存在【runs/detect】目录下。
2.2 方法二:在C2f模块的残差结构中添加CA模块
2.2.1 步骤一
在【ultralytics/nn/modules】目录下新建一个c2f_ca.py的文件。将CA模块添加到C2f模块的残差结构中。 代码获取链接:https://mbd.pub/o/bread/mbd-ZpqamZpy
然后在【ultralytics/nn/moduels/_init_.py】文件里面导入Bottleneck_CA,C2f_CA模块。
2.2.2 步骤二
打开【ultralytics/nn/tasks.py】文件,在文件的开头导入C2f_CA模块。
找到parse_model这个方法(可以通过搜索parse_model)。
然后在parse_model方法中找到for语句(for i, (f, n, m, args) in enumerate(d["backbone"] + d["head"])),在for语句中的两个位置添加C2f_CA模块。
2.2.3 步骤三
修改模型。在【ultralytics/cfg/models/v8】目录下新建一个yolov8-c2f-ca.yaml网络结构配置文件。(这里我将主干中的C2f模块替换为C2f_CA模块,除此之外,也可以将Neck的C2f模块替换为C2f_CA模块) 代码获取链接:https://mbd.pub/o/bread/mbd-ZpqamZpy
2.2.4 训练过程
在train.py文件中修改模型的配置文件路径,利用yolov8-c2f-ca.yaml文件,然后运行train.py。
成功运行的网络结构截图如下:
开始训练模型的部门截图如下:
训练效果保存在"runs/detect"目录下。
3.完整的项目文件下载路径
我们提供完整的项目文件,你也可以直接下载到当地,然后打开项目,修改数据集配置文件【NEU-DET.yaml】的数据集路径即可训练模型。完整项目代码获取链接:https://mbd.pub/o/bread/mbd-ZpqamZpy
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |