首页 > 其他分享 >YOLOv11模型改进-注意力-引入简单无参数注意力模块SimAM 提升小目标和遮挡检测

YOLOv11模型改进-注意力-引入简单无参数注意力模块SimAM 提升小目标和遮挡检测

时间:2024-10-22 16:45:26浏览次数:3  
标签:__ SimAM shortcut 注意力 self YOLOv11 C3k2 simam

            本篇文章将介绍一个新的改进机制——卷积和注意力融合模块SimAM ,并阐述如何将其应用于YOLOv11中,显著提升模型性能。首先,SimAM 是一种用于卷积神经网络的简单且无参数的注意力模块,它基于神经科学理论定义能量函数来计算 3-D 注意力权重,能有效提升网络的表征能力,且具有轻量级、高效等优势。随后,我们将详细讨论他的模型结构,以及如何将SimAM 模块与YOLOv11相结合,以提升目标检测的性能。

1. SimAM 结构介绍          

      SimAM 的核心结构围绕其独特的注意力机制构建,以下是其主要结构特点:

        1. 能量函数计算部分:基于视觉神经科学理论,为每个神经元定义能量函数通过最小化这个能量函数,找到目标神经元与其他神经元的线性可分性,从而确定神经元在视觉处理中的重要程度。

        2. 特征精炼部分:缩放算子应用:根据哺乳动物大脑中注意力调制表现为对神经元反应的增益效应,使用缩放算子进行特征精炼。具体来说,通过来实现,其中包含所有通道和空间维度的(即每个神经元的最小能量),函数用于限制中的过大值,以确保特征精炼的合理性。

2. YOLOv11与SimAM 的结合

       本文将YOLOv11模型的C3K2模块相结合 ,组合成C3k2_simam模块。利用SimAM 能够推断 3 - D 注意力权重,同时考虑空间和通道维度的能力。这有助于C3K2模块更全面地关注目标的不同特征维度。

3. SimAM 代码部分

import torch
import torch.nn as nn
from .conv import Conv
from .block import C2f, C3, Bottleneck


class simam_module(torch.nn.Module):
    def __init__(self, channels=None, e_lambda=1e-4):
        super(simam_module, self).__init__()

        self.activaton = nn.Sigmoid()
        self.e_lambda = e_lambda

    def __repr__(self):
        s = self.__class__.__name__ + '('
        s += ('lambda=%f)' % self.e_lambda)
        return s

    @staticmethod
    def get_module_name():
        return "simam"

    def forward(self, x):
        b, c, h, w = x.size()

        n = w * h - 1

        x_minus_mu_square = (x - x.mean(dim=[2, 3], keepdim=True)).pow(2)
        y = x_minus_mu_square / (4 * (x_minus_mu_square.sum(dim=[2, 3], keepdim=True) / n + self.e_lambda)) + 0.5

        return x * self.activaton(y)



class Bottleneck_simam(nn.Module):
    """Standard bottleneck."""

    def __init__(self, c1, c2, shortcut=True, g=1, k=(3, 3), e=0.5):
        """Initializes a standard bottleneck module with optional shortcut connection and configurable parameters."""
        super().__init__()
        c_ = int(c2 * e)  # hidden channels
        self.cv1 = Conv(c1, c_, k[0], 1)
        self.cv2 = simam_module(c_)
        self.add = shortcut and c1 == c2

    def forward(self, x):
        """Applies the YOLO FPN to input data."""
        return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))

class C3k(C3):
    """C3k is a CSP bottleneck module with customizable kernel sizes for feature extraction in neural networks."""

    def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, k=3):
        """Initializes the C3k module with specified channels, number of layers, and configurations."""
        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_simam(c_, c_, shortcut, g, k=(k, k), e=1.0) for _ in range(n)))

# 在c3k=True时,使用Bottleneck_simam特征融合,为false的时候我们使用普通的Bottleneck提取特征
class C3k2_simam(C2f):
    """Faster Implementation of CSP Bottleneck with 2 convolutions."""

    def __init__(self, c1, c2, n=1, c3k=False, e=0.5, g=1, shortcut=True):
        """Initializes the C3k2 module, a faster CSP Bottleneck with 2 convolutions and optional C3k blocks."""
        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)
        )


if __name__ =='__main__':

    simam = simam_module(256)
    #创建一个输入张量
    batch_size = 1
    input_tensor=torch.randn(batch_size, 256, 64, 64 )
    #运行模型并打印输入和输出的形状
    output_tensor =simam(input_tensor)
    print("Input shape:",input_tensor.shape)
    print("0utput shape:",output_tensor.shape)

 4. 将SimAM 引入到YOLOv11中

第一: 将下面的核心代码复制到D:\bilibili\model\YOLO11\ultralytics-main\ultralytics\nn路径下,如下图所示。

第二:在task.py中导入SimAM 

第三:在task.py中的模型配置部分下面代码

第四:将模型配置文件复制到YOLOV11.YAMY文件中

# Ultralytics YOLO 

标签:__,SimAM,shortcut,注意力,self,YOLOv11,C3k2,simam
From: https://blog.csdn.net/qq_64693987/article/details/143055432

相关文章