YOLO11改进 | 卷积模块 | SAConv可切换空洞卷积【详细步骤】

秋招面试专栏推荐 :深度学习算法工程师面试问题总结【百面算法工程师】——点击即可跳转 


💡💡💡本专栏所有程序均经过测试,可成功执行💡💡💡


本文介绍了可切换空洞卷积(Switchable Atrous Convolution),它用不同的空洞率对特征进行卷积,并使用切换函数收集结果。将它们结合起来就得到了DetectoRS,它显著提高了目标检测的性能。 文章在介绍主要的原理后,将手把手教学如何进行模块的代码添加和修改,并将修改后的完整代码放在文章的最后,方便大家一键运行,小白也可轻松上手实践。以帮助您更好地学习深度学习目标检测YOLO系列的挑战。

专栏地址:YOLO11入门 + 改进涨点——点击即可跳转 欢迎订阅

目录

1. 论文

2. 将SAConv添加到YOLO11代码

2.1 SAConv代码实现

2.2 更改init.py文件 

2.3 新增yaml文件

2.4 在task.py中进行注册

2.5 执行程序

3.修改后的网络结构图

4. 完整代码分享</

### YOLOv11 中引入卷积模块的技术细节与实现方式 #### CoordConv 的应用 为了增强YOLOv11的空间感知能力,在卷积层中加入CoordConv可以显著提升模型对于空间位置的理解。通过增加额外的坐标信息通道,网络能更高效地学习空间变换特性,进而改善对物体定位精度[^1]。 ```python import torch.nn as nn class ConvWithCoords(nn.Module): def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1): super(ConvWithCoords, self).__init__() self.conv = nn.Conv2d(in_channels + 2, out_channels, kernel_size, stride, padding) def forward(self, x): b, _, h, w = x.size() xx_channel = torch.arange(w).repeat(h, 1).float() / (w - 1) yy_channel = torch.arange(h).repeat(w, 1).transpose(0, 1).float() / (h - 1) xx_channel = xx_channel.expand(b, 1, h, w) yy_channel = yy_channel.expand(b, 1, h, w) ret = torch.cat([ x, xx_channel.type_as(x), yy_channel.type_as(x)], dim=1) return self.conv(ret) ``` #### 蛇形动态卷积的应用 针对特定应用场景下的细长或弯曲形状的目标识别需求,可以在YOLOv11框架内集成蛇形动态卷积技术。该方法允许卷积核权重根据输入数据自适应调整,特别适用于捕捉血管等管状结构中的细微变化,从而优化这类对象的检测效果[^2]。 ```python def snake_conv(input_tensor, weight_matrix, bias_vector=None): output_shape = list(input_tensor.shape[:2]) + [input_tensor.shape[2]+weight_matrix.shape[0]-1]*2 padded_input = F.pad( input=input_tensor.unsqueeze(dim=-3), pad=(int((weight_matrix.shape[-1]-1)/2),)*4, mode='replicate' ) conv_result = [] for i in range(weight_matrix.shape[0]): shifted_weight = torch.roll(weight_matrix[i], shifts=i-weight_matrix.shape[0]//2, dims=[-2,-1]) temp_output = F.conv2d(padded_input, shifted_weight.unsqueeze(dim=0)) conv_result.append(temp_output.squeeze()) final_output = sum(conv_result) if isinstance(bias_vector, torch.Tensor): final_output += bias_vector.view(-1,*([1]*len(final_output.shape)[1:])) return final_output.reshape(output_shape) ``` #### 可切换空洞卷积(SAConv)的应用 SAConv作为另一种有效的改进措施被纳入考虑范围之内。此机制不仅继承了标准空洞卷积的优势——即扩大感受野而不增加过多参数数量;还具备灵活调节膨胀率的功能,有助于应对多尺度目标带来的挑战[^3]。 ```python from functools import partial class SwitchableASPPModule(nn.ModuleList): """Switchable Atrous Spatial Pyramid Pooling Module.""" def __init__(self, dilations, in_channels, channels): super().__init__([ nn.Sequential( nn.Conv2d(in_channels=in_channels, out_channels=channels, kernel_size=1, dilation=dilation), nn.BatchNorm2d(channels), nn.ReLU(inplace=True)) for dilation in dilations]) def forward(self, feats): outs = [] for module in self: outs.append(module(feats)) return tuple(outs) saconv_layer = SwitchableASPPModule(dilations=[1, 6, 12, 18], in_channels=256, channels=64) ``` #### 替换普通卷积为GSConv 考虑到模型轻量化的需求以及性能增益的要求,采用GSConv来替代传统的卷积操作不失为一个好的选择。这种方法能在减少资源消耗的同时带来一定的准确度提升,非常适合部署于移动端或其他计算资源有限的情况下工作[^4]。 ```yaml # yolov8_GS.yaml snippet showing how to define a layer using GSConv instead of regular convolution. backbone: ... [[...]] - from: [-1] number: 1 module: models.common.GhostBottleneckCSP args: [c_, c_ * 2, True] head: ... [[...]] - from: [-1] number: 1 module: models.yolo.Detect args: [ nc=nc, anchors=anchors, ch=[ch[f] for f in yolo_indices], export=False, amp=True, inplace=True, concat=True, gsconv=True # Enable GSConv here by setting this flag true. ] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kay_545

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值