YOLO11改进|卷积篇|引入可变核卷积AKConv

目录

一、AKConv卷积

1.1AKConv卷积介绍

AKConv允许卷积参数的数量以线性方式增加或减少,而不是传统的平方增长趋势。这种特性对于硬件环境非常有益,因为它可以根据实际需求动态调整参数数量,从而实现更高效的资源利用。在资源有限的情况下,AKConv能够帮助网络模型在保持性能的同时,减少参数数量和计算开销,这对于轻量级模型的设计尤为重要。

其次,AKConv为网络性能的提升提供了更多的选择。在资源充足的情况下,使用大核卷积时,AKConv能够利用更多的选项来优化网络性能。由于其采样形状的灵活性和可调整性,AKConv能够更好地适应不同目标形状的变化,从而提高网络的识别能力和泛化性能。

此外,AKConv的思想还可以扩展到特定领域。根据先验知识,可以创建特定的采样形状用于卷积操作,并通过偏移量动态、自动地适应目标形状的变化。这种灵活性使得AKConv能够更好地适应各种复杂场景和任务需求。

AKConv的结构与工作流程如下所示

  1. 输入

    输入的数据为 (C,H,W) 的特征图,表示通道数为 C,高度为 H,宽度为 W。

  2. 卷积层生成偏移量

    首先,通过一个标准的卷积操作(Conv2d)来生成偏移量(offset)。该偏移量的输出维度为 (2𝑁,𝐻,𝑊),其中 N 是卷积核的大小,比如 3×3 的卷积核则 𝑁=9,2 表示偏移量包含 𝑥 和 𝑦 方向上的坐标变化。

    偏移量解释:偏移量是用于调整采样位置的。普通卷积在固定的规则网格上进行采样,而加入偏移量后,采样点可以在原来固定的位置上根据偏移量进行微调,从而能够更好地捕捉几何变化。

  3. 计算新的采样坐标

    原始卷积的采样点是规则排列的坐标集,比如 3×3

    的卷积核对应固定的9个采样点坐标。现在,将这些原始采样点加上通过卷积生成的偏移量,得到新的采样点坐标。

    公式:新的坐标 = 原始坐标 + 偏移量

    这样可以在原始特征图中动态选择采样点,以更好地适应特征的几何变形和空间分布。

  4. 重采样(Resample)

    接下来,根据计算得到的新采样坐标,对原始输入的特征图进行重采样操作。这一步类似于根据新的坐标在原始特征图上重新采集局部特征。

    通过偏移量调整后的采样形状和采样点可以更灵活地捕捉到特征的局部变化。

  5. 合并特征通道对于每个通道 (𝐶,𝑘𝑠,𝑘𝑠)

    的特征,经过采样后形成新的特征块。这些重采样后的特征块被合并在一起,得到一个新的特征图 (𝐶×𝑠,𝐻,𝑊),其中 𝑠 是卷积核的数量,如 𝑠=5。

  6. 后处理(Reshape, Conv, Norm, SiLU)

    经过重采样后的特征图经过一系列标准的后处理步骤:

    Reshape:调整特征图的维度,使其适合后续的卷积操作。

    Conv:标准的卷积操作进一步处理融合后的特征。

    Norm:归一化层,用于稳定网络训练。

    SiLU:激活函数,增强非线性表达能力。

  7. 输出

    最后,经过处理的特征图输出结果,其尺寸仍为 (𝐶,𝐻,𝑊)

    ,但这些特征包含了经过动态调整采样位置后的增强特征。

1.2AKConv核心代码

python 复制代码
import torch
import torch.nn as nn
import math
from einops import rearrange

class AKConv(nn.Module):
    def __init__(self, inc, outc, num_param, stride=1, bias=None):
        super(AKConv, self).__init__()
        self.num_param = num_param
        self.stride = stride
        self.conv = nn.Sequential(nn.Conv2d(inc, outc, kernel_size=(num_param, 1), stride=(num_param, 1), bias=bias),nn.BatchNorm2d(outc),nn.SiLU())  # the conv adds the BN and SiLU to compare original Conv in YOLOv5.
        self.p_conv = nn.Conv2d(inc, 2 * num_param, kernel_size=3, padding=1, stride=stride)
        nn.init.constant_(self.p_conv.weight, 0)
        self.p_conv.register_full_backward_hook(self._set_lr)

    @staticmethod
    def _set_lr(module, grad_input, grad_output):
        grad_input = (grad_input[i] * 0.1 for i in range(len(grad_input)))
        grad_output = (grad_output[i] * 0.1 for i in range(len(grad_output)))

    def forward(self, x):
        # N is num_param.
        offset = self.p_conv(x)
        dtype = offset.data.type()
        N = offset.size(1) // 2
        # (b, 2N, h, w)
        p = self._get_p(offset, dtype)

        # (b, h, w, 2N)
        p = p.contiguous().permute(0, 2, 3, 1)
        q_lt = p.detach().floor()
        q_rb = q_lt + 1

        q_lt = torch.cat([torch.clamp(q_lt[..., :N], 0, x.size(2) - 1), torch.clamp(q_lt[..., N:], 0, x.size(3) - 1)],
                         dim=-1).long()
        q_rb = torch.cat([torch.clamp(q_rb[..., :N], 0, x.size(2) - 1), torch.clamp(q_rb[..., N:], 0, x.size(3) - 1)],
                         dim=-1).long()
        q_lb = torch.cat([q_lt[..., :N], q_rb[..., N:]], dim=-1)
        q_rt = torch.cat([q_rb[..., :N], q_lt[..., N:]], dim=-1)

        # clip p
        p = torch.cat([torch.clamp(p[..., :N], 0, x.size(2) - 1), torch.clamp(p[..., N:], 0, x.size(3) - 1)], dim=-1)

        # bilinear kernel (b, h, w, N)
        g_lt = (1 + (q_lt[..., :N].type_as(p) - p[..., :N])) * (1 + (q_lt[..., N:].type_as(p) - p[..., N:]))
        g_rb = (1 - (q_rb[..., :N].type_as(p) - p[..., :N])) * (1 - (q_rb[..., N:].type_as(p) - p[..., N:]))
        g_lb = (1 + (q_lb[..., :N].type_as(p) - p[..., :N])) * (1 - (q_lb[..., N:].type_as(p) - p[..., N:]))
        g_rt = (1 - (q_rt[..., :N].type_as(p) - p[..., :N])) * (1 + (q_rt[..., N:].type_as(p) - p[..., N:]))

        # resampling the features based on the modified coordinates.
        x_q_lt = self._get_x_q(x, q_lt, N)
        x_q_rb = self._get_x_q(x, q_rb, N)
        x_q_lb = self._get_x_q(x, q_lb, N)
        x_q_rt = self._get_x_q(x, q_rt, N)

        # bilinear
        x_offset = g_lt.unsqueeze(dim=1) * x_q_lt + \
                   g_rb.unsqueeze(dim=1) * x_q_rb + \
                   g_lb.unsqueeze(dim=1) * x_q_lb + \
                   g_rt.unsqueeze(dim=1) * x_q_rt

        x_offset = self._reshape_x_offset(x_offset, self.num_param)
        out = self.conv(x_offset)

        return out

    # generating the inital sampled shapes for the AKConv with different sizes.
    def _get_p_n(self, N, dtype):
        base_int = round(math.sqrt(self.num_param))
        row_number = self.num_param // base_int
        mod_number = self.num_param % base_int
        p_n_x,p_n_y = torch.meshgrid(
            torch.arange(0, row_number),
            torch.arange(0,base_int))
        p_n_x = torch.flatten(p_n_x)
        p_n_y = torch.flatten(p_n_y)
        if mod_number >  0:
            mod_p_n_x,mod_p_n_y = torch.meshgrid(
                torch.arange(row_number,row_number+1),
                torch.arange(0,mod_number))

            mod_p_n_x = torch.flatten(mod_p_n_x)
            mod_p_n_y = torch.flatten(mod_p_n_y)
            p_n_x,p_n_y  = torch.cat((p_n_x,mod_p_n_x)),torch.cat((p_n_y,mod_p_n_y))
        p_n = torch.cat([p_n_x,p_n_y], 0)
        p_n = p_n.view(1, 2 * N, 1, 1).type(dtype)
        return p_n

    # no zero-padding
    def _get_p_0(self, h, w, N, dtype):
        p_0_x, p_0_y = torch.meshgrid(
            torch.arange(0, h * self.stride, self.stride),
            torch.arange(0, w * self.stride, self.stride))

        p_0_x = torch.flatten(p_0_x).view(1, 1, h, w).repeat(1, N, 1, 1)
        p_0_y = torch.flatten(p_0_y).view(1, 1, h, w).repeat(1, N, 1, 1)
        p_0 = torch.cat([p_0_x, p_0_y], 1).type(dtype)

        return p_0

    def _get_p(self, offset, dtype):
        N, h, w = offset.size(1) // 2, offset.size(2), offset.size(3)

        # (1, 2N, 1, 1)
        p_n = self._get_p_n(N, dtype)
        # (1, 2N, h, w)
        p_0 = self._get_p_0(h, w, N, dtype)
        p = p_0 + p_n + offset
        return p

    def _get_x_q(self, x, q, N):
        b, h, w, _ = q.size()
        padded_w = x.size(3)
        c = x.size(1)
        # (b, c, h*w)
        x = x.contiguous().view(b, c, -1)

        # (b, h, w, N)
        index = q[..., :N] * padded_w + q[..., N:]  # offset_x*w + offset_y
        # (b, c, h*w*N)
        index = index.contiguous().unsqueeze(dim=1).expand(-1, c, -1, -1, -1).contiguous().view(b, c, -1)

        x_offset = x.gather(dim=-1, index=index).contiguous().view(b, c, h, w, N)

        return x_offset

    
    #  Stacking resampled features in the row direction.
    @staticmethod
    def _reshape_x_offset(x_offset, num_param):
        b, c, h, w, n = x_offset.size()
        # using Conv3d
        # x_offset = x_offset.permute(0,1,4,2,3), then Conv3d(c,c_out, kernel_size =(num_param,1,1),stride=(num_param,1,1),bias= False)
        # using 1 × 1 Conv
        # x_offset = x_offset.permute(0,1,4,2,3), then, x_offset.view(b,c×num_param,h,w)  finally, Conv2d(c×num_param,c_out, kernel_size =1,stride=1,bias= False)
        # using the column conv as follow, then, Conv2d(inc, outc, kernel_size=(num_param, 1), stride=(num_param, 1), bias=bias)
        
        x_offset = rearrange(x_offset, 'b c h w n -> b c (h n) w')
        return x_offset

五、添加MLCA注意力机制

5.1STEP1

首先找到ultralytics/nn文件路径下新建一个Add-module的python文件包【这里注意一定是python文件包,新建后会自动生成_init_.py】,如果已经跟着我的教程建立过一次了可以省略此步骤,随后新建一个AKConv.py文件并将上文中提到的注意力机制的代码全部粘贴到此文件中,如下图所示

5.2STEP2

在STEP1中新建的_init_.py文件中导入增加改进模块的代码包如下图所示

5.3STEP3

找到ultralytics/nn文件夹中的task.py文件,在其中按照下图添加,同样的如果已经跟我的教程走过一遍了这部分可以省略

5.4STEP4

定位到ultralytics/nn文件夹中的task.py文件中的def parse_model(d, ch, verbose=True): # model_dict, input_channels(3)函数添加如图代码,【如果不好定位可以直接ctrl+f搜索定位】

六、yaml文件与运行

6.1yaml文件

以下是在yolo11.yaml中添加AKConv卷积的yaml文件,大家可以注释自行调节,效果以自己的数据集结果为准

python 复制代码
# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLO11 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect

# Parameters
nc: 80 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolo11n.yaml' will call yolo11.yaml with scale 'n'
  # [depth, width, max_channels]
  n: [0.50, 0.25, 1024] # summary: 319 layers, 2624080 parameters, 2624064 gradients, 6.6 GFLOPs
  s: [0.50, 0.50, 1024] # summary: 319 layers, 9458752 parameters, 9458736 gradients, 21.7 GFLOPs
  m: [0.50, 1.00, 512] # summary: 409 layers, 20114688 parameters, 20114672 gradients, 68.5 GFLOPs
  l: [1.00, 1.00, 512] # summary: 631 layers, 25372160 parameters, 25372144 gradients, 87.6 GFLOPs
  x: [1.00, 1.50, 512] # summary: 631 layers, 56966176 parameters, 56966160 gradients, 196.0 GFLOPs

# YOLO11n backbone
backbone:
  # [from, repeats, module, args]
  - [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
  - [-1, 1, AKConv, [128, 3, 2]] # 1-P2/4
  - [-1, 2, C3k2, [256, False, 0.25]]
  - [-1, 1, AKConv, [256, 3, 2]] # 3-P3/8
  - [-1, 2, C3k2, [512, False, 0.25]]
  - [-1, 1, AKConv, [512, 3, 2]] # 5-P4/16
  - [-1, 2, C3k2, [512, True]]
  - [-1, 1, AKConv, [1024, 3, 2]] # 7-P5/32
  - [-1, 2, C3k2, [1024, True]]
  - [-1, 1, SPPF, [1024, 5]] # 9
  - [-1, 2, C2PSA, [1024]] # 10

# YOLO11n head
head:
  - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  - [[-1, 6], 1, Concat, [1]] # cat backbone P4
  - [-1, 2, C3k2, [512, False]] # 13

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

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

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

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

以上添加位置仅供参考,具体添加位置以及模块效果以自己的数据集结果为准 ,同时根据我的实验,使用这个模块训练我的数据集map有点奇怪,大家也可以实验一下

6.2运行成功截图

OK 以上就是添加AKConv卷积的全部过程了,后续将持续更新尽情期待

相关推荐
jay神8 天前
基于 YOLOv8 的传送带异物检测系统(Web 版)
前端·人工智能·深度学习·yolo·计算机视觉·计算机毕业设计
jay神10 天前
基于 YOLOv8 的手势识别系统(Web 版)
前端·人工智能·python·深度学习·yolo·计算机视觉·计算机毕业设计
鱼听禅10 天前
Ubuntu安装YOLOv8进行菌落识别训练
linux·python·yolo·ubuntu
jay神12 天前
基于深度学习的交通流量预测系统(Web 版)
前端·人工智能·深度学习·yolo·计算机毕业设计
带电的小王13 天前
YOLO27:官方预览页放出全部规格,60.5 mAP 破 YOLO 纪录,但权重还没发布
yolo
jay神13 天前
基于 YOLOv8 的轴承缺陷检测(Web 版)
前端·人工智能·yolo
jay神13 天前
基于 YOLOv8 的森林火灾检测系统(Web 版)
前端·yolo
微尘hjx14 天前
【Deep-OC-SORT 06】多轨迹算法Deep-OC-SORT-CMC相机运动补偿
yolo·目标检测·目标跟踪·ultralytics·轨迹算法·deep-oc-sort·车流统计
LongRunning14 天前
【Linux】RK3568-OpenCV+YOLO(八)
linux·opencv·yolo
微尘hjx18 天前
【Deep-OC-SORT 01】多轨迹算法Deep-OC-SORT源码分析
深度学习·yolo·目标跟踪·ultralytics·轨迹算法·deep-oc-sort·车流统计