[编程代码] 空间金字塔池化改进 SPP / SPPF / SimSPPF / ASPP / RFB / SPPCSPC / SPPFC

3199 0
黑夜隐士 2022-10-13 14:38:51 | 显示全部楼层 |阅读模式


更新日志:2022年8月16日上午9:33分前在图片中增加感受野标注

更新日志:2022年8月29日晚上11点40分在文中增加了SimSPPF模块,并测试了速度

更新日志:2022年8月30日修正了SPPCSPC的结构图

更新日志:2022年8月30日增加了SPPFCSPC的结构


1 原理

1.1 SPP(Spatial Pyramid Pooling)

SPP模块是何凯明大神在2015年的论文《Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition》中被提出。

SPP全程为空间金字塔池化结构,主要是为了解决两个问题:

  1. 有效避免了对图像区域裁剪、缩放操作导致的图像失真等问题;
  2. 解决了卷积神经网络对图相关重复特征提取的问题,大大提高了产生候选框的速度,且节省了计算成本。

在这里插入图片描述

请添加图片描述

  1. class SPP(nn.Module):
  2. # Spatial Pyramid Pooling (SPP) layer https://arxiv.org/abs/1406.4729
  3. def __init__(self, c1, c2, k=(5, 9, 13)):
  4. super().__init__()
  5. c_ = c1 // 2 # hidden channels
  6. self.cv1 = Conv(c1, c_, 1, 1)
  7. self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1)
  8. self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])
  9. def forward(self, x):
  10. x = self.cv1(x)
  11. with warnings.catch_warnings():
  12. warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning
  13. return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1))
复制代码

1.2 SPPF(Spatial Pyramid Pooling - Fast)

这个是YOLOv5作者Glenn Jocher基于SPP提出的,速度较SPP快很多,所以叫SPP-Fast

请添加图片描述

  1. class SPPF(nn.Module):
  2. # Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher
  3. def __init__(self, c1, c2, k=5): # equivalent to SPP(k=(5, 9, 13))
  4. super().__init__()
  5. c_ = c1 // 2 # hidden channels
  6. self.cv1 = Conv(c1, c_, 1, 1)
  7. self.cv2 = Conv(c_ * 4, c2, 1, 1)
  8. self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2)
  9. def forward(self, x):
  10. x = self.cv1(x)
  11. with warnings.catch_warnings():
  12. warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning
  13. y1 = self.m(x)
  14. y2 = self.m(y1)
  15. return self.cv2(torch.cat((x, y1, y2, self.m(y2)), 1))
复制代码

1.3 SimSPPF(Simplified SPPF)

美团YOLOv6提出的模块,感觉和SPPF只差了一个激活函数,简单测试了一下,单个ConvBNReLU速度要比ConvBNSiLU快18%
请添加图片描述

  1. class SimConv(nn.Module):
  2. '''Normal Conv with ReLU activation'''
  3. def __init__(self, in_channels, out_channels, kernel_size, stride, groups=1, bias=False):
  4. super().__init__()
  5. padding = kernel_size // 2
  6. self.conv = nn.Conv2d(
  7. in_channels,
  8. out_channels,
  9. kernel_size=kernel_size,
  10. stride=stride,
  11. padding=padding,
  12. groups=groups,
  13. bias=bias,
  14. )
  15. self.bn = nn.BatchNorm2d(out_channels)
  16. self.act = nn.ReLU()
  17. def forward(self, x):
  18. return self.act(self.bn(self.conv(x)))
  19. def forward_fuse(self, x):
  20. return self.act(self.conv(x))
  21. class SimSPPF(nn.Module):
  22. '''Simplified SPPF with ReLU activation'''
  23. def __init__(self, in_channels, out_channels, kernel_size=5):
  24. super().__init__()
  25. c_ = in_channels // 2 # hidden channels
  26. self.cv1 = SimConv(in_channels, c_, 1, 1)
  27. self.cv2 = SimConv(c_ * 4, out_channels, 1, 1)
  28. self.m = nn.MaxPool2d(kernel_size=kernel_size, stride=1, padding=kernel_size // 2)
  29. def forward(self, x):
  30. x = self.cv1(x)
  31. with warnings.catch_warnings():
  32. warnings.simplefilter('ignore')
  33. y1 = self.m(x)
  34. y2 = self.m(y1)
  35. return self.cv2(torch.cat([x, y1, y2, self.m(y2)], 1))
复制代码

1.4 ASPP(Atrous Spatial Pyramid Pooling)

受到SPP的启发,语义分割模型DeepLabv2中提出了ASPP模块(空洞空间卷积池化金字塔),该模块使用具有不同采样率的多个并行空洞卷积层。为每个采样率提取的特征在单独的分支中进一步处理,并融合以生成最终结果。该模块通过不同的空洞率构建不同感受野的卷积核,用来获取多尺度物体信息,具体结构比较简单如下图所示:

请添加图片描述

ASPP是在DeepLab中提出来的,在后续的DeepLab版本中对其做了改进,如加入BN层、加入深度可分离卷积等,但基本的思路还是没变。

  1. # without BN version
  2. class ASPP(nn.Module):
  3. def __init__(self, in_channel=512, out_channel=256):
  4. super(ASPP, self).__init__()
  5. self.mean = nn.AdaptiveAvgPool2d((1, 1)) # (1,1)means ouput_dim
  6. self.conv = nn.Conv2d(in_channel,out_channel, 1, 1)
  7. self.atrous_block1 = nn.Conv2d(in_channel, out_channel, 1, 1)
  8. self.atrous_block6 = nn.Conv2d(in_channel, out_channel, 3, 1, padding=6, dilation=6)
  9. self.atrous_block12 = nn.Conv2d(in_channel, out_channel, 3, 1, padding=12, dilation=12)
  10. self.atrous_block18 = nn.Conv2d(in_channel, out_channel, 3, 1, padding=18, dilation=18)
  11. self.conv_1x1_output = nn.Conv2d(out_channel * 5, out_channel, 1, 1)
  12. def forward(self, x):
  13. size = x.shape[2:]
  14. image_features = self.mean(x)
  15. image_features = self.conv(image_features)
  16. image_features = F.upsample(image_features, size=size, mode='bilinear')
  17. atrous_block1 = self.atrous_block1(x)
  18. atrous_block6 = self.atrous_block6(x)
  19. atrous_block12 = self.atrous_block12(x)
  20. atrous_block18 = self.atrous_block18(x)
  21. net = self.conv_1x1_output(torch.cat([image_features, atrous_block1, atrous_block6,
  22. atrous_block12, atrous_block18], dim=1))
  23. return net
复制代码

1.5 RFB(Receptive Field Block)

RFB模块是在《ECCV2018:Receptive Field Block Net for Accurate and Fast Object Detection》一文中提出的,该文的出发点是模拟人类视觉的感受野从而加强网络的特征提取能力,在结构上RFB借鉴了Inception的思想,主要是在Inception的基础上加入了空洞卷积,从而有效增大了感受野
在这里插入图片描述
请添加图片描述

RFB和RFB-s的架构。RFB-s用于在浅层人类视网膜主题图中模拟较小的pRF,使用具有较小内核的更多分支。

  1. class BasicConv(nn.Module):
  2. def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, dilation=1, groups=1, relu=True, bn=True):
  3. super(BasicConv, self).__init__()
  4. self.out_channels = out_planes
  5. if bn:
  6. self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=False)
  7. self.bn = nn.BatchNorm2d(out_planes, eps=1e-5, momentum=0.01, affine=True)
  8. self.relu = nn.ReLU(inplace=True) if relu else None
  9. else:
  10. self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=True)
  11. self.bn = None
  12. self.relu = nn.ReLU(inplace=True) if relu else None
  13. def forward(self, x):
  14. x = self.conv(x)
  15. if self.bn is not None:
  16. x = self.bn(x)
  17. if self.relu is not None:
  18. x = self.relu(x)
  19. return x
  20. class BasicRFB(nn.Module):
  21. def __init__(self, in_planes, out_planes, stride=1, scale=0.1, map_reduce=8, vision=1, groups=1):
  22. super(BasicRFB, self).__init__()
  23. self.scale = scale
  24. self.out_channels = out_planes
  25. inter_planes = in_planes // map_reduce
  26. self.branch0 = nn.Sequential(
  27. BasicConv(in_planes, inter_planes, kernel_size=1, stride=1, groups=groups, relu=False),
  28. BasicConv(inter_planes, 2 * inter_planes, kernel_size=(3, 3), stride=stride, padding=(1, 1), groups=groups),
  29. BasicConv(2 * inter_planes, 2 * inter_planes, kernel_size=3, stride=1, padding=vision + 1, dilation=vision + 1, relu=False, groups=groups)
  30. )
  31. self.branch1 = nn.Sequential(
  32. BasicConv(in_planes, inter_planes, kernel_size=1, stride=1, groups=groups, relu=False),
  33. BasicConv(inter_planes, 2 * inter_planes, kernel_size=(3, 3), stride=stride, padding=(1, 1), groups=groups),
  34. BasicConv(2 * inter_planes, 2 * inter_planes, kernel_size=3, stride=1, padding=vision + 2, dilation=vision + 2, relu=False, groups=groups)
  35. )
  36. self.branch2 = nn.Sequential(
  37. BasicConv(in_planes, inter_planes, kernel_size=1, stride=1, groups=groups, relu=False),
  38. BasicConv(inter_planes, (inter_planes // 2) * 3, kernel_size=3, stride=1, padding=1, groups=groups),
  39. BasicConv((inter_planes // 2) * 3, 2 * inter_planes, kernel_size=3, stride=stride, padding=1, groups=groups),
  40. BasicConv(2 * inter_planes, 2 * inter_planes, kernel_size=3, stride=1, padding=vision + 4, dilation=vision + 4, relu=False, groups=groups)
  41. )
  42. self.ConvLinear = BasicConv(6 * inter_planes, out_planes, kernel_size=1, stride=1, relu=False)
  43. self.shortcut = BasicConv(in_planes, out_planes, kernel_size=1, stride=stride, relu=False)
  44. self.relu = nn.ReLU(inplace=False)
  45. def forward(self, x):
  46. x0 = self.branch0(x)
  47. x1 = self.branch1(x)
  48. x2 = self.branch2(x)
  49. out = torch.cat((x0, x1, x2), 1)
  50. out = self.ConvLinear(out)
  51. short = self.shortcut(x)
  52. out = out * self.scale + short
  53. out = self.relu(out)
  54. return out
复制代码

1.6 SPPCSPC

该模块是YOLOv7中使用的SPP结构,表现优于SPPF,但参数量和计算量提升了很多

请添加图片描述

  1. class SPPCSPC(nn.Module):
  2. # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
  3. def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5, k=(5, 9, 13)):
  4. super(SPPCSPC, self).__init__()
  5. c_ = int(2 * c2 * e) # hidden channels
  6. self.cv1 = Conv(c1, c_, 1, 1)
  7. self.cv2 = Conv(c1, c_, 1, 1)
  8. self.cv3 = Conv(c_, c_, 3, 1)
  9. self.cv4 = Conv(c_, c_, 1, 1)
  10. self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])
  11. self.cv5 = Conv(4 * c_, c_, 1, 1)
  12. self.cv6 = Conv(c_, c_, 3, 1)
  13. self.cv7 = Conv(2 * c_, c2, 1, 1)
  14. def forward(self, x):
  15. x1 = self.cv4(self.cv3(self.cv1(x)))
  16. y1 = self.cv6(self.cv5(torch.cat([x1] + [m(x1) for m in self.m], 1)))
  17. y2 = self.cv2(x)
  18. return self.cv7(torch.cat((y1, y2), dim=1))
复制代码
  1. #分组SPPCSPC 分组后参数量和计算量与原本差距不大,不知道效果怎么样
  2. class SPPCSPC_group(nn.Module):
  3. def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5, k=(5, 9, 13)):
  4. super(SPPCSPC_group, self).__init__()
  5. c_ = int(2 * c2 * e) # hidden channels
  6. self.cv1 = Conv(c1, c_, 1, 1, g=4)
  7. self.cv2 = Conv(c1, c_, 1, 1, g=4)
  8. self.cv3 = Conv(c_, c_, 3, 1, g=4)
  9. self.cv4 = Conv(c_, c_, 1, 1, g=4)
  10. self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])
  11. self.cv5 = Conv(4 * c_, c_, 1, 1, g=4)
  12. self.cv6 = Conv(c_, c_, 3, 1, g=4)
  13. self.cv7 = Conv(2 * c_, c2, 1, 1, g=4)
  14. def forward(self, x):
  15. x1 = self.cv4(self.cv3(self.cv1(x)))
  16. y1 = self.cv6(self.cv5(torch.cat([x1] + [m(x1) for m in self.m], 1)))
  17. y2 = self.cv2(x)
  18. return self.cv7(torch.cat((y1, y2), dim=1))
复制代码

1.7 SPPFCSPC

我借鉴了SPPF的思想将SPPCSPC优化了一下,得到了SPPFCSPC,在保持感受野不变的情况下获得速度提升;我把这个模块给v7作者看了,并没有得到否定,详细回答可以看4 Issue
请添加图片描述

  1. class SPPFCSPC(nn.Module):
  2. # 本代码由YOLOAir目标检测交流群 心动 大佬贡献
  3. def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5, k=5):
  4. super(SPPFCSPC, self).__init__()
  5. c_ = int(2 * c2 * e) # hidden channels
  6. self.cv1 = Conv(c1, c_, 1, 1)
  7. self.cv2 = Conv(c1, c_, 1, 1)
  8. self.cv3 = Conv(c_, c_, 3, 1)
  9. self.cv4 = Conv(c_, c_, 1, 1)
  10. self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2)
  11. self.cv5 = Conv(4 * c_, c_, 1, 1)
  12. self.cv6 = Conv(c_, c_, 3, 1)
  13. self.cv7 = Conv(2 * c_, c2, 1, 1)
  14. def forward(self, x):
  15. x1 = self.cv4(self.cv3(self.cv1(x)))
  16. x2 = self.m(x1)
  17. x3 = self.m(x2)
  18. y1 = self.cv6(self.cv5(torch.cat((x1,x2,x3, self.m(x3)),1)))
  19. y2 = self.cv2(x)
  20. return self.cv7(torch.cat((y1, y2), dim=1))
复制代码

2 参数量对比

这里我在yolov5s.yaml中使用各个模型替换SPP模块

模型参数量(parameters)计算量(GFLOPs)
SPP722588516.5
SPPF723538916.5
SimSPPF723538916.5
ASPP1548572523.1
BasicRFB789542117.1
SPPCSPC1366354921.7
SPPFCSPC1366354921.7
分组SPPCSPC835513317.4

3 改进方式

第一步;各个代码放入common.py中
第二步;yolo.py中加入类名
第三步;修改配置文件
yolov5配置文件如下:

  1. # YOLOv5 by Ultralytics, GPL-3.0 license
  2. # YOLOv5 v6.0 backbone
  3. backbone:
  4. # [from, number, module, args]
  5. [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
  6. [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
  7. [-1, 3, C3, [128]],
  8. [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
  9. [-1, 6, C3, [256]],
  10. [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
  11. [-1, 9, C3, [512]],
  12. [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
  13. [-1, 3, C3, [1024]],
  14. [-1, 1, SPPF, [1024, 5]], # 9
  15. [-1, 1, ASPP, [1024]], # 9
  16. [-1, 1, SPP, [1024]],
  17. [-1, 1, SimSPPF, [1024, 5]],
  18. [-1, 1, BasicRFB, [1024]],
  19. [-1, 1, SPPCSPC, [1024]],
  20. [-1, 1, SPPFCSPC, [1024, 5]], #
  21. ]
复制代码

4 Issue

Q:Why use SPPCSPC instead of SPPFCSPC? /
yolov5’s SPPF is much faster than SPP.
Why not try to replace SPPCSPC with SPPFCSPC?
请添加图片描述
A:
Max pooling uses very few computation, if you programming well, above one could run three max pool layers in parallel, while below one must process three max pool layers sequentially.
By the way, you could replace SPPCSPC by SPPFCSPC at inference time if your hardware is friendly to SPPFCSPC.

感兴趣的可以试一下


更多内容导航

1.手把手带你调参Yolo v5 (v6.2)(一)强烈推荐

2.手把手带你调参Yolo v5 (v6.2)(二)

3.如何快速使用自己的数据集训练Yolov5模型

4.手把手带你Yolov5 (v6.2)添加注意力机制(一)(并附上30多种顶会Attention原理图)

5.手把手带你Yolov5 (v6.2)添加注意力机制(二)(在C3模块中加入注意力机制)

6.Yolov5如何更换激活函数?

7.Yolov5 (v6.2)数据增强方式解析

8.Yolov5更换上采样方式( 最近邻 / 双线性 / 双立方 / 三线性 / 转置卷积)

9.Yolov5如何更换EIOU / alpha IOU / SIoU?

10.Yolov5更换主干网络之《旷视轻量化卷积神经网络ShuffleNetv2》

11.YOLOv5应用轻量级通用上采样算子CARAFE

12.空间金字塔池化改进 SPP / SPPF / SimSPPF / ASPP / RFB / SPPCSPC

13.用于低分辨率图像和小物体的模块SPD-Conv

14.持续更新中


参考文献:增强感受野SPP、ASPP、RFB、PPM


来源:https://blog.csdn.net/weixin_43694096/article/details/126354660
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行