赞
踩
前 言
作为当前先进的深度学习目标检测算法YOLOv5,已经集合了大量的trick,但是在处理一些复杂背景问题的时候,还是容易出现错漏检的问题。此后的系列文章,将重点对YOLOv5的如何改进进行详细的介绍,目的是为了给那些搞科研的同学需要创新点或者搞工程项目的朋友需要达到更好的效果提供自己的微薄帮助和参考。
加入ECA注意力机制,可以让网络更加关注待检测目标,提高检测效果。该注意力机制为一篇CVPR2020上对通道注意力进行改进的文章---ECANet,ECANet主要对SENet模块进行了一些改进,提出了一种不降维的局部跨信道交互策略(ECA模块)和自适应选择一维卷积核大小的方法,从而实现了性能上的提优。
第一步:确定添加的位置,作为即插即用的注意力模块,可以添加到YOLOv7网络中的任何地方。
第二步:common.py构建 Eca_layer模块。
- class Eca_layer(nn.Module):
- """Constructs a ECA module.
- Args:
- channel: Number of channels of the input feature map
- k_size: Adaptive selection of kernel size
- """
- def __init__(self, channel, k_size=3):
- super(Eca_layer, self).__init__()
- self.avg_pool = nn.AdaptiveAvgPool2d(1)
- self.conv = nn.Conv1d(1, 1, kernel_size=k_size, padding=(k_size - 1) // 2, bias=False)
- self.sigmoid = nn.Sigmoid()
-
- def forward(self, x):
- # x: input features with shape [b, c, h, w]
- b, c, h, w = x.size()
-
- # feature descriptor on the global spatial information
- y = self.avg_pool(x)
-
- # Two different branches of ECA module
- y = self.conv(y.squeeze(-1).transpose(-1, -2)).transpose(-1, -2).unsqueeze(-1)
-
- # Multi-scale information fusion
- y = self.sigmoid(y)
-
- return x * y.expand_as(x)
第三步:yolo.py中注册Eca_layer模块
- elif m is Eca_layer:
- channel,k_size=args[0],args[1]
- channel = make_divisible(channel * gw, 8) if channel != no else channel
- args = [channel, k_size]
第四步:修改YOLOv7.yaml文件,本文以修改backbone为例。
- # parameters
- nc: 1 # number of classes
- depth_multiple: 1.0 # model depth multiple
- width_multiple: 1.0 # layer channel multiple
-
- # anchors
- anchors:
- - [12,16, 19,36, 40,28] # P3/8
- - [36,75, 76,55, 72,146] # P4/16
- - [142,110, 192,243, 459,401] # P5/32
-
- # yolov7 backbone
- backbone:
- # [from, number, module, args]
- [[-1, 1, Conv, [32, 3, 1]], # 0
-
- [-1, 1, Conv, [64, 3, 2]], # 1-P1/2
- [-1, 1, Conv, [64, 3, 1]],
-
- [-1, 1, Conv, [128, 3, 2]], # 3-P2/4
- [-1, 1, Conv, [64, 1, 1]],
- [-2, 1, Conv, [64, 1, 1]],
- [-1, 1, Conv, [64, 3, 1]],
- [-1, 1, Conv, [64, 3, 1]],
- [-1, 1, Conv, [64, 3, 1]],
- [-1, 1, Conv, [64, 3, 1]],
- [[-1, -3, -5, -6], 1, Concat, [1]],
- [-1, 1, Conv, [256, 1, 1]], # 11
-
- [-1, 1, MP, []],
- [-1, 1, Conv, [128, 1, 1]],
- [-3, 1, Conv, [128, 1, 1]],
- [-1, 1, Conv, [128, 3, 2]],
- [[-1, -3], 1, Concat, [1]], # 16-P3/8
- [-1, 1, Conv, [128, 1, 1]],
- [-2, 1, Conv, [128, 1, 1]],
- [-1, 1, Conv, [128, 3, 1]],
- [-1, 1, Conv, [128, 3, 1]],
- [-1, 1, Conv, [128, 3, 1]],
- [-1, 1, Conv, [128, 3, 1]],
- [[-1, -3, -5, -6], 1, Concat, [1]],
- [-1, 1, Conv, [512, 1, 1]], # 24
-
- [-1, 1, MP, []],
- [-1, 1, Conv, [256, 1, 1]],
- [-3, 1, Conv, [256, 1, 1]],
- [-1, 1, Conv, [256, 3, 2]],
- [[-1, -3], 1, Concat, [1]], # 29-P4/16
- [-1, 1, Conv, [256, 1, 1]],
- [-2, 1, Conv, [256, 1, 1]],
- [-1, 1, Conv, [256, 3, 1]],
- [-1, 1, Conv, [256, 3, 1]],
- [-1, 1, Conv, [256, 3, 1]],
- [-1, 1, Conv, [256, 3, 1]],
- [[-1, -3, -5, -6], 1, Concat, [1]],
- [-1, 1, Conv, [1024, 1, 1]], # 37
-
- [-1, 1, MP, []],
- [-1, 1, Conv, [512, 1, 1]],
- [-3, 1, Conv, [512, 1, 1]],
- [-1, 1, Conv, [512, 3, 2]],
- [[-1, -3], 1, Concat, [1]], # 42-P5/32
- [-1, 1, Conv, [256, 1, 1]],
- [-2, 1, Conv, [256, 1, 1]],
- [-1, 1, Conv, [256, 3, 1]],
- [-1, 1, Conv, [256, 3, 1]],
- [-1, 1, Conv, [256, 3, 1]],
- [-1, 1, Conv, [256, 3, 1]],
- [[-1, -3, -5, -6], 1, Concat, [1]],
- [-1, 1, Conv, [1024, 1, 1]], # 50
- [-1,1,Eca_layer,[1024,3]],
- ]
-
- # yolov7 head
- head:
- [[-1, 1, SPPCSPC, [512]], # 51
-
- [-1, 1, Conv, [256, 1, 1]],
- [-1, 1, nn.Upsample, [None, 2, 'nearest']],
- [37, 1, Conv, [256, 1, 1]], # route backbone P4
- [[-1, -2], 1, Concat, [1]],
-
- [-1, 1, Conv, [256, 1, 1]],
- [-2, 1, Conv, [256, 1, 1]],
- [-1, 1, Conv, [128, 3, 1]],
- [-1, 1, Conv, [128, 3, 1]],
- [-1, 1, Conv, [128, 3, 1]],
- [-1, 1, Conv, [128, 3, 1]],
- [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
-
-
- [-1, 1, Conv, [256, 1, 1]], # 63
-
- [-1, 1, Conv, [128, 1, 1]],
- [-1, 1, nn.Upsample, [None, 2, 'nearest']],
- [24, 1, Conv, [128, 1, 1]], # route backbone P3
- [[-1, -2], 1, Concat, [1]],
-
- [-1, 1, Conv, [128, 1, 1]],
- [-2, 1, Conv, [128, 1, 1]],
- [-1, 1, Conv, [64, 3, 1]],
- [-1, 1, Conv, [64, 3, 1]],
- [-1, 1, Conv, [64, 3, 1]],
- [-1, 1, Conv, [64, 3, 1]],
- [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
-
- [-1, 1, Conv, [128, 1, 1]], # 75
-
- [-1, 1, MP, []],
- [-1, 1, Conv, [128, 1, 1]],
- [-3, 1, Conv, [128, 1, 1]],
- [-1, 1, Conv, [128, 3, 2]],
- [[-1, -3, 63], 1, Concat, [1]],
-
- [-1, 1, Conv, [256, 1, 1]],
-
- [-2, 1, Conv, [256, 1, 1]],
- [-1, 1, Conv, [128, 3, 1]],
- [-1, 1, Conv, [128, 3, 1]],
- [-1, 1, Conv, [128, 3, 1]],
- [-1, 1, Conv, [128, 3, 1]],
- [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
-
- [-1, 1, Conv, [256, 1, 1]], # 88
-
- [-1, 1, MP, []],
- [-1, 1, Conv, [256, 1, 1]],
- [-3, 1, Conv, [256, 1, 1]],
- [-1, 1, Conv, [256, 3, 2]],
- [[-1, -3, 52], 1, Concat, [1]],
-
- [-1, 1, Conv, [512, 1, 1]],
- [-2, 1, Conv, [512, 1, 1]],
- [-1, 1, Conv, [256, 3, 1]],
- [-1, 1, Conv, [256, 3, 1]],
- [-1, 1, Conv, [256, 3, 1]],
- [-1, 1, Conv, [256, 3, 1]],
- [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
- [-1, 1, Conv, [512, 1, 1]], # 101
-
- [76, 1, RepConv, [256, 3, 1]],
- [89, 1, RepConv, [512, 3, 1]],
- [102, 1, RepConv, [1024, 3, 1]],
-
- [[103,104,105], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5)
- ]
根据yaml文件画出网络结构图如下所示:
前三步同YOLOv7方法。
第四步:修改yaml文件,本文以修改backbone为例,将原C3模块后加入该模块。
- # YOLOv5 声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/346259推荐阅读
相关标签
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。