当前位置:   article > 正文

【YOLOv8/YOLOv7/YOLOv5/YOLOv4/Faster-rcnn系列算法改进NO.61】结合最新的CVPR2023年Fasternet网络模块_yolov7应用fasternet

yolov7应用fasternet
前言
作为当前先进的深度学习目标检测算法YOLOv8,已经集合了大量的trick,但是还是有提高和改进的空间,针对具体应用场景下的检测难点,可以不同的改进方法。此后的系列文章,将重点对YOLOv8的如何改进进行详细的介绍,目的是为了给那些搞科研的同学需要创新点或者搞工程项目的朋友需要达到更好的效果提供自己的微薄帮助和参考。由于出到YOLOv8,YOLOv7、YOLOv5算法2020年至今已经涌现出大量改进论文,这个不论对于搞科研的同学或者已经工作的朋友来说,研究的价值和新颖度都不太够了,为与时俱进,以后改进算法以YOLOv7为基础,此前YOLOv5改进方法在YOLOv7同样适用,所以继续YOLOv5系列改进的序号。另外改进方法在YOLOv5等其他算法同样可以适用进行改进。希望能够对大家有帮助。

一、解决问题

尝试将原YOLOv7/v5中的损失函数改为wiou,提升精度和效果。此前修改更先进的eiou,siou,a-iou边框位置回归函数,精度有所提升,新出的wiou可进行尝试改进。

二、基本原理

Github代码

论文

三、​添加方法

Fsternet相关代码如下:具体改进方法,关注后私信

  1. class FasterNet(nn.Module):
  2. def __init__(self,
  3. in_chans=3,
  4. num_classes=1000,
  5. embed_dim=96,
  6. depths=(1, 2, 8, 2),
  7. mlp_ratio=2.,
  8. n_div=4,
  9. patch_size=4,
  10. patch_stride=4,
  11. patch_size2=2, # for subsequent layers
  12. patch_stride2=2,
  13. patch_norm=True,
  14. feature_dim=1280,
  15. drop_path_rate=0.1,
  16. layer_scale_init_value=0,
  17. norm_layer='BN',
  18. act_layer='RELU',
  19. fork_feat=False,
  20. init_cfg=None,
  21. pretrained=None,
  22. pconv_fw_type='split_cat',
  23. **kwargs):
  24. super().__init__()
  25. if norm_layer == 'BN':
  26. norm_layer = nn.BatchNorm2d
  27. else:
  28. raise NotImplementedError
  29. if act_layer == 'GELU':
  30. act_layer = nn.GELU
  31. elif act_layer == 'RELU':
  32. act_layer = partial(nn.ReLU, inplace=True)
  33. else:
  34. raise NotImplementedError
  35. if not fork_feat:
  36. self.num_classes = num_classes
  37. self.num_stages = len(depths)
  38. self.embed_dim = embed_dim
  39. self.patch_norm = patch_norm
  40. self.num_features = int(embed_dim * 2 ** (self.num_stages - 1))
  41. self.mlp_ratio = mlp_ratio
  42. self.depths = depths
  43. # split image into non-overlapping patches
  44. self.patch_embed = PatchEmbed(
  45. patch_size=patch_size,
  46. patch_stride=patch_stride,
  47. in_chans=in_chans,
  48. embed_dim=embed_dim,
  49. norm_layer=norm_layer if self.patch_norm else None
  50. )
  51. # stochastic depth decay rule
  52. dpr = [x.item()
  53. for x in torch.linspace(0, drop_path_rate, sum(depths))]
  54. # build layers
  55. stages_list = []
  56. for i_stage in range(self.num_stages):
  57. stage = BasicStage(dim=int(embed_dim * 2 ** i_stage),
  58. n_div=n_div,
  59. depth=depths[i_stage],
  60. mlp_ratio=self.mlp_ratio,
  61. drop_path=dpr[sum(depths[:i_stage]):sum(depths[:i_stage + 1])],
  62. layer_scale_init_value=layer_scale_init_value,
  63. norm_layer=norm_layer,
  64. act_layer=act_layer,
  65. pconv_fw_type=pconv_fw_type
  66. )
  67. stages_list.append(stage)
  68. # patch merging layer
  69. if i_stage < self.num_stages - 1:
  70. stages_list.append(
  71. PatchMerging(patch_size2=patch_size2,
  72. patch_stride2=patch_stride2,
  73. dim=int(embed_dim * 2 ** i_stage),
  74. norm_layer=norm_layer)
  75. )
  76. self.stages = nn.Sequential(*stages_list)
  77. self.fork_feat = fork_feat
  78. if self.fork_feat:
  79. self.forward = self.forward_det
  80. # add a norm layer for each output
  81. self.out_indices = [0, 2, 4, 6]
  82. for i_emb, i_layer in enumerate(self.out_indices):
  83. if i_emb == 0 and os.environ.get('FORK_LAST3', None):
  84. raise NotImplementedError
  85. else:
  86. layer = norm_layer(int(embed_dim * 2 ** i_emb))
  87. layer_name = f'norm{i_layer}'
  88. self.add_module(layer_name, layer)
  89. else:
  90. self.forward = self.forward_cls
  91. # Classifier head
  92. self.avgpool_pre_head = nn.Sequential(
  93. nn.AdaptiveAvgPool2d(1),
  94. nn.Conv2d(self.num_features, feature_dim, 1, bias=False),
  95. act_layer()
  96. )
  97. self.head = nn.Linear(feature_dim, num_classes) \
  98. if num_classes > 0 else nn.Identity()
  99. self.apply(self.cls_init_weights)
  100. self.init_cfg = copy.deepcopy(init_cfg)
  101. if self.fork_feat and (self.init_cfg is not None or pretrained is not None):
  102. self.init_weights()
  103. def cls_init_weights(self, m):
  104. if isinstance(m, nn.Linear):
  105. trunc_normal_(m.weight, std=.02)
  106. if isinstance(m, nn.Linear) and m.bias is not None:
  107. nn.init.constant_(m.bias, 0)
  108. elif isinstance(m, (nn.Conv1d, nn.Conv2d)):
  109. trunc_normal_(m.weight, std=.02)
  110. if m.bias is not None:
  111. nn.init.constant_(m.bias, 0)
  112. elif isinstance(m, (nn.LayerNorm, nn.GroupNorm)):
  113. nn.init.constant_(m.bias, 0)
  114. nn.init.constant_(m.weight, 1.0)

Model Summary: 281 layers, 5821465 parameters, 5821465 gradients, 12.9 GFLOPs

四、总结

预告一下:下一篇内容将继续分享深度学习算法相关改进方法。有兴趣的朋友可以关注一下我,有问题可以留言或者私聊我哦

PS:该方法不仅仅是适用改进YOLOv5,也可以改进其他的YOLO网络以及目标检测网络,比如YOLOv7、v6、v4、v3,Faster rcnn ,ssd等。

最后,有需要的请关注私信我吧。关注免费领取深度学习算法学习资料!

YOLO系列算法改进方法 | 目录一览表

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/170910
推荐阅读
相关标签
  

闽ICP备14008679号