
一、前言为提升水下珊瑚礁白化/死亡/健康检测精度本实验在YOLOv8n网络中引入CA注意力机制。通过将CACoordinate Attention模块嵌入不同位置骨干网浅、中、深层及颈部对比分析其对模型性能的影响旨在寻找最佳嵌入策略为水下目标检测的注意力优化提供依据。二、实验准备1、数据集来源——Roboflow本实验采用Roboflow平台上的Marjan balance Dataset作为数据来源。Roboflow是全球领先的计算机视觉平台提供数据集管理、标注、增强和模型部署的一站式服务。该数据集包含珊瑚礁白化状态的标注图像类别涵盖健康珊瑚Healthy、死亡珊瑚dead和白化珊瑚Bleached三种状态。2、YOLOv8模型(结合SPPELAN和PSA模块)本实验所采用的改进型YOLOv8模型融合了源自SPMamba‑YOLO架构的两大核心组件——SPPELAN模块S与PSA注意力模块P。其中SPPELANSpatial Pyramid Pooling with Efficient Layer Aggregation Network通过多分支池化与跨阶段特征融合显著扩展了模型的感受野增强了对水下珊瑚图像中多尺度目标的特征提取能力而PSAPyramid Split Attention则通过金字塔式的通道分割与注意力权重重标定使模型能够更敏感地聚焦于珊瑚颜色差异与局部纹理变化。该YOLOv8模型识别效果图本实验的YOLOv8模型资料来源从零开始SPMamba-yolo全流程部署文档模型训练脚本——train.pyfrom ultralytics import YOLO def train_yolov8(): # 初始化模型 # model YOLO(yolov8n.pt) # 这里使用预训练的模型权重 # 修改为自己的配置文件地址 model YOLO(E:/Graduation_project/ultralytics_yolov8/datasets/yolov8.yaml,verboseTrue) # 开始训练 model.train( datadata.yaml, # 数据集配置文件 epochs100, # 训练的轮数 imgsz640, # 输入图像尺寸 device0, # 使用的设备0表示使用第一个GPU workers1, # 数据加载的工作进程数 batch32 # 每批次的样本数量 ) if __name__ __main__: train_yolov8()注训练轮次epochs推荐为300轮3、Coordinate Attention注意力机制为了使模型在解码器部分能够更好地结合深浅层特征利用水平和垂直方向上的两个分支使模型能够动态地调整对不同方向上特征的关注程度以捕获更全面的空间信息然后利用自适应平均池化、卷积和激活函数等操作提高模型精确处理感兴趣特征效率的同时提升检测与定位的精确程度同时引入坐标注意力通过坐标信息嵌入模型将位置信息与特征图中的每个通道相关联然后利用注意力生成模型应用注意力机制来动态调整不同通道之间的关系以及捕获长期依赖关系并提供精确的位置信息。引用相关论文吴琛,唐贝贝,邵叱风.基于注意力机制和DeepLabv3的图像篡改检测方法[J].哈尔滨商业大学学报(自然科学版),2025,41(03):283-289.DOI:10.19492/j.cnki.1672-0946.2025.03.011.CA注意力机制嵌入方法含相关资料芒果YOLO11改进05免费最新结合即插即用CA(Coordinate attention) 注意力机制CVPR 顶会助力分类检测涨点问题与解决方法1发生NameError: name nn is not defined时需要导包在ca.py文件开头加入import torch import torch.nn as nn2发生NameError: nameno is not defined时需要将task.py文件中添加的if c2 ! no:改成if c2 ! nc:三、对比实验在模型中加入CACoordinate Attention注意力机制时其嵌入位置的选择会显著影响模型的性能表现。以下是几种常见的嵌入方案及其具体实现细节实验1、原始基线无CAyolov8.yaml# Ultralytics YOLO , AGPL-3.0 license # YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect # Parameters nc: 3 # number of classes scales: # model compound scaling constants, i.e. modelyolov8n.yaml will call yolov8.yaml with scale n # [depth, width, max_channels] n: [0.33, 0.25, 1024] # YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPs s: [0.33, 0.50, 1024] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPs m: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPs l: [1.00, 1.00, 512] # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPs x: [1.00, 1.25, 512] # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPs # YOLOv8.0n backbone 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, Conv, [512, 3, 2]] # 5-P4/16 - [-1, 6, C2f, [512, True]] - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32 - [-1, 3, C2f, [1024, True]] - [-1, 1, SPPELAN, [1024, 256]] # 9 - [-1, 1, PSA, [1024]] # YOLOv8.0n head head: - [-1, 1, nn.Upsample, [None, 2, nearest]] - [[-1, 6], 1, Concat, [1]] # cat backbone P4 - [-1, 3, C2f, [512]] # 12 - [-1, 1, nn.Upsample, [None, 2, nearest]] - [[-1, 4], 1, Concat, [1]] # cat backbone P3 - [-1, 3, C2f, [256]] # 15 (P3/8-small) - [-1, 1, Conv, [256, 3, 2]] - [[-1, 13], 1, Concat, [1]] # cat head P4 - [-1, 3, C2f, [512]] # 18 (P4/16-medium) - [-1, 1, Conv, [512, 3, 2]] - [[-1, 9], 1, Concat, [1]] # cat head P5 - [-1, 3, C2f, [1024]] # 21 (P5/32-large) - [[16, 19, 22], 1, Detect, [nc]] # Detect(P3, P4, P5)实验效果实验2、替换PSA注意力机制# YOLOv8.0n backbone 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, Conv, [512, 3, 2]] # 5-P4/16 - [-1, 6, C2f, [512, True]] - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32 - [-1, 3, C2f, [1024, True]] - [-1, 1, SPPELAN, [1024, 256]] # 9 - [-1, 1, CA, [1024]] # YOLOv8.0n head head: - [-1, 1, nn.Upsample, [None, 2, nearest]] - [[-1, 6], 1, Concat, [1]] # cat backbone P4 - [-1, 3, C2f, [512]] # 12 - [-1, 1, nn.Upsample, [None, 2, nearest]] - [[-1, 4], 1, Concat, [1]] # cat backbone P3 - [-1, 3, C2f, [256]] # 15 (P3/8-small) - [-1, 1, Conv, [256, 3, 2]] - [[-1, 13], 1, Concat, [1]] # cat head P4 - [-1, 3, C2f, [512]] # 18 (P4/16-medium) - [-1, 1, Conv, [512, 3, 2]] - [[-1, 9], 1, Concat, [1]] # cat head P5 - [-1, 3, C2f, [1024]] # 21 (P5/32-large) - [[16, 19, 22], 1, Detect, [nc]] # Detect(P3, P4, P5)实验效果对比实验1结果显示mAP0.5指标下降11.6个百分点性能出现显著退化NG实验3、在SPPELAN与PSA之间# YOLOv8.0n backbone 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, Conv, [512, 3, 2]] # 5-P4/16 - [-1, 6, C2f, [512, True]] - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32 - [-1, 3, C2f, [1024, True]] - [-1, 1, SPPELAN, [1024, 256]] # 9 - [-1, 1, CA, [1024]] - [-1, 1, PSA, [1024]] # YOLOv8.0n head head: - [-1, 1, nn.Upsample, [None, 2, nearest]] - [[-1, 6], 1, Concat, [1]] # cat backbone P4 - [-1, 3, C2f, [512]] # 12 - [-1, 1, nn.Upsample, [None, 2, nearest]] - [[-1, 4], 1, Concat, [1]] # cat backbone P3 - [-1, 3, C2f, [256]] # 15 (P3/8-small) - [-1, 1, Conv, [256, 3, 2]] - [[-1, 14], 1, Concat, [1]] # cat head P4 - [-1, 3, C2f, [512]] # 18 (P4/16-medium) - [-1, 1, Conv, [512, 3, 2]] - [[-1, 9], 1, Concat, [1]] # cat head P5 - [-1, 3, C2f, [1024]] # 21 (P5/32-large) - [[17, 20, 23], 1, Detect, [nc]] # Detect(P3, P4, P5)实验效果对比实验1结果显示mAP0.5指标下降7个百分点性能出现显著退化NG对比实验2结果显示mAP0.5指标上升4.6个百分点性能取得明显改进OK实验4、在Neck的上采样融合之后P5级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, Conv, [512, 3, 2]] # 5-P4/16 - [-1, 6, C2f, [512, True]] - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32 - [-1, 3, C2f, [1024, True]] - [-1, 1, SPPELAN, [1024, 256]] # 9 - [-1, 2, PSA, [1024]] # YOLOv8.0n head head: - [-1, 1, nn.Upsample, [None, 2, nearest]] - [[-1, 6], 1, Concat, [1]] # cat backbone P4 - [-1, 3, C2f, [512]] # 12 - [-1, 1, nn.Upsample, [None, 2, nearest]] - [[-1, 4], 1, Concat, [1]] # cat backbone P3 - [-1, 3, C2f, [256]] # 15 (P3/8-small) - [-1, 1, Conv, [256, 3, 2]] - [[-1, 13], 1, Concat, [1]] # cat head P4 - [-1, 3, C2f, [512]] # 18 (P4/16-medium) - [-1, 1, Conv, [512, 3, 2]] - [[-1, 9], 1, Concat, [1]] # cat head P5 - [-1, 3, C2f, [1024]] # 21 (P5/32-large) - [-1, 3, CA, [1024]] - [[16, 19, 22], 1, Detect, [nc]] # Detect(P3, P4, P5)实验效果对比实验1结果显示mAP0.5指标上升1.2个百分点性能出现略微提升OK实验5、在Backbone每个阶段输出后# YOLOv8.0n backbone 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, CA, [256]] - [-1, 1, Conv, [512, 3, 2]] # 5-P4/16 - [-1, 6, C2f, [512, True]] - [-1, 1, CA, [512]] - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32 - [-1, 3, C2f, [1024, True]] - [-1, 1, CA, [1024]] - [-1, 1, SPPELAN, [1024, 256]] # 9 - [-1, 1, PSA, [1024]] # YOLOv8.0n head head: - [-1, 1, nn.Upsample, [None, 2, nearest]] - [[-1, 7], 1, Concat, [1]] # cat backbone P4 - [-1, 3, C2f, [512]] # 12 - [-1, 1, nn.Upsample, [None, 2, nearest]] - [[-1, 4], 1, Concat, [1]] # cat backbone P3 - [-1, 3, C2f, [256]] # 15 (P3/8-small) - [-1, 1, Conv, [256, 3, 2]] - [[-1, 16], 1, Concat, [1]] # cat head P4 - [-1, 3, C2f, [512]] # 18 (P4/16-medium) - [-1, 1, Conv, [512, 3, 2]] - [[-1, 12], 1, Concat, [1]] # cat head P5 - [-1, 3, C2f, [1024]] # 21 (P5/32-large) - [[19, 22, 25], 1, Detect, [nc]] # Detect(P3, P4, P5)实验效果对比实验1结果显示mAP0.5指标上升3.6个百分点性能出现显著提升OK对比实验4结果显示mAP0.5指标上升2.4个百分点性能出现明显提升OK四、实验结论实验结果表明实验5的效果最为显著。该实验训练的YOLOv8模型识别效果图