当前位置:   article > 正文

理解yolov7网络结构_[-1, 1, conv, [32, 3, 1]

[-1, 1, conv, [32, 3, 1]

 以下是yolov7网络结构配置的yaml,对每一层的输出加了注释。

  1. # parameters
  2. nc: 80 # number of classes
  3. depth_multiple: 1.0 # model depth multiple
  4. width_multiple: 1.0 # layer channel multiple
  5. # anchors
  6. anchors:
  7. - [12,16, 19,36, 40,28] # P3/8
  8. - [36,75, 76,55, 72,146] # P4/16
  9. - [142,110, 192,243, 459,401] # P5/32
  10. # yolov7 backbone
  11. backbone:
  12. # [from, number, module, args] [N,3,640,640]
  13. [[-1, 1, Conv, [32, 3, 1]], # 0 [N,32,640,640]
  14. [-1, 1, Conv, [64, 3, 2]], # 1-P1/2 [N,64,320,320]
  15. [-1, 1, Conv, [64, 3, 1]], #[N,64,320,320]
  16. [-1, 1, Conv, [128, 3, 2]], # 3-P2/4 [N,128,160,160]
  17. [-1, 1, Conv, [64, 1, 1]], # -6 [N,64,160,160]
  18. [-2, 1, Conv, [64, 1, 1]], # -5 [N,64,160,160]
  19. [-1, 1, Conv, [64, 3, 1]], #[N,64,160,160]
  20. [-1, 1, Conv, [64, 3, 1]], # -3 #[N,64,160,160]
  21. [-1, 1, Conv, [64, 3, 1]], #[N,64,160,160]
  22. [-1, 1, Conv, [64, 3, 1]], # -1 #[N,64,160,160]
  23. [[-1, -3, -5, -6], 1, Concat, [1]], #[N,256,160,160]
  24. [-1, 1, Conv, [256, 1, 1]], # 11 #[N,256,160,160]
  25. [-1, 1, MP, []], #[N,256,80,80]
  26. [-1, 1, Conv, [128, 1, 1]], #[N,128,80,80]
  27. [-3, 1, Conv, [128, 1, 1]], #[N,128,160,160]
  28. [-1, 1, Conv, [128, 3, 2]], #[N,128,80,80]
  29. [[-1, -3], 1, Concat, [1]], # 16-P3/8 #[N,256,80,80]
  30. [-1, 1, Conv, [128, 1, 1]], #[N,128,80,80]
  31. [-2, 1, Conv, [128, 1, 1]], #[N,128,80,80]
  32. [-1, 1, Conv, [128, 3, 1]], #[N,128,80,80]
  33. [-1, 1, Conv, [128, 3, 1]], #[N,128,80,80]
  34. [-1, 1, Conv, [128, 3, 1]], #[N,128,80,80]
  35. [-1, 1, Conv, [128, 3, 1]], #[N,128,80,80]
  36. [[-1, -3, -5, -6], 1, Concat, [1]], #[N,512,80,80]
  37. [-1, 1, Conv, [512, 1, 1]], # 24 #[N,512,80,80]
  38. [-1, 1, MP, []], #[N,512,40,40]
  39. [-1, 1, Conv, [256, 1, 1]], #[N,256,40,40]
  40. [-3, 1, Conv, [256, 1, 1]], #[N,256,80,80]
  41. [-1, 1, Conv, [256, 3, 2]], #[N,256,40,40]
  42. [[-1, -3], 1, Concat, [1]], # 29-P4/16 #[N,512,40,40]
  43. [-1, 1, Conv, [256, 1, 1]], #[N,256,40,40]
  44. [-2, 1, Conv, [256, 1, 1]], #[N,256,40,40]
  45. [-1, 1, Conv, [256, 3, 1]], #[N,256,40,40]
  46. [-1, 1, Conv, [256, 3, 1]], #[N,256,40,40]
  47. [-1, 1, Conv, [256, 3, 1]], #[N,256,40,40]
  48. [-1, 1, Conv, [256, 3, 1]], #[N,256,40,40]
  49. [[-1, -3, -5, -6], 1, Concat, [1]], #[N,1024,40,40]
  50. [-1, 1, Conv, [1024, 1, 1]], # 37 #[N,1024,40,40]
  51. [-1, 1, MP, []], #[N,1024,20,20]
  52. [-1, 1, Conv, [512, 1, 1]], #[N,512,20,20]
  53. [-3, 1, Conv, [512, 1, 1]], #[N,512,40,40]
  54. [-1, 1, Conv, [512, 3, 2]], #[N,512,20,20]
  55. [[-1, -3], 1, Concat, [1]], # 42-P5/32 #[N,1024,20,20]
  56. [-1, 1, Conv, [256, 1, 1]], #[N,256,20,20]
  57. [-2, 1, Conv, [256, 1, 1]], #[N,256,20,20]
  58. [-1, 1, Conv, [256, 3, 1]], #[N,256,20,20]
  59. [-1, 1, Conv, [256, 3, 1]], #[N,256,20,20]
  60. [-1, 1, Conv, [256, 3, 1]], #[N,256,20,20]
  61. [-1, 1, Conv, [256, 3, 1]], #[N,256,20,20]
  62. [[-1, -3, -5, -6], 1, Concat, [1]], #[N,1024,20,20]
  63. [-1, 1, Conv, [1024, 1, 1]], # 50 #[N,1024,20,20]
  64. ]
  65. # yolov7 head
  66. head:
  67. [[-1, 1, SPPCSPC, [512]], # 51 #[N,512,20,20]
  68. [-1, 1, Conv, [256, 1, 1]], #[N,256,20,20]
  69. [-1, 1, nn.Upsample, [None, 2, 'nearest']], #[N,256,40,40]
  70. [37, 1, Conv, [256, 1, 1]], # route backbone P4 #[N,256,40,40]
  71. [[-1, -2], 1, Concat, [1]], #[N,512,40,40]
  72. [-1, 1, Conv, [256, 1, 1]], #[N,256,40,40]
  73. [-2, 1, Conv, [256, 1, 1]], #[N,256,40,40]
  74. [-1, 1, Conv, [128, 3, 1]], #[N,128,40,40]
  75. [-1, 1, Conv, [128, 3, 1]], #[N,128,40,40]
  76. [-1, 1, Conv, [128, 3, 1]], #[N,128,40,40]
  77. [-1, 1, Conv, [128, 3, 1]], #[N,128,40,40]
  78. [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]], #[N,1024,40,40]
  79. [-1, 1, Conv, [256, 1, 1]], # 63 #[N,256,40,40]
  80. [-1, 1, Conv, [128, 1, 1]], #[N,128,40,40]
  81. [-1, 1, nn.Upsample, [None, 2, 'nearest']], #[N,128,80,80]
  82. [24, 1, Conv, [128, 1, 1]], # route backbone P3 #[N,128,80,80]
  83. [[-1, -2], 1, Concat, [1]], #[N,256,80,80]
  84. [-1, 1, Conv, [128, 1, 1]], #[N,128,80,80]
  85. [-2, 1, Conv, [128, 1, 1]], #[N,128,80,80]
  86. [-1, 1, Conv, [64, 3, 1]], #[N,64,80,80]
  87. [-1, 1, Conv, [64, 3, 1]], #[N,64,80,80]
  88. [-1, 1, Conv, [64, 3, 1]], #[N,64,80,80]
  89. [-1, 1, Conv, [64, 3, 1]], #[N,64,80,80]
  90. [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]], #[N,512,80,80]
  91. [-1, 1, Conv, [128, 1, 1]], # 75 #[N,128,80,80]
  92. [-1, 1, MP, []], #[N,128,40,40]
  93. [-1, 1, Conv, [128, 1, 1]], #[N,128,40,40]
  94. [-3, 1, Conv, [128, 1, 1]], #[N,128,80,80]
  95. [-1, 1, Conv, [128, 3, 2]], #[N,128,40,40]
  96. [[-1, -3, 63], 1, Concat, [1]], #[N,512,40,40]
  97. [-1, 1, Conv, [256, 1, 1]], #[N,256,40,40]
  98. [-2, 1, Conv, [256, 1, 1]], #[N,256,40,40]
  99. [-1, 1, Conv, [128, 3, 1]], #[N,128,40,40]
  100. [-1, 1, Conv, [128, 3, 1]], #[N,128,40,40]
  101. [-1, 1, Conv, [128, 3, 1]], #[N,128,40,40]
  102. [-1, 1, Conv, [128, 3, 1]], #[N,128,40,40]
  103. [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]], #[N,1024,40,40]
  104. [-1, 1, Conv, [256, 1, 1]], # 88 #[N,256,40,40]
  105. [-1, 1, MP, []], #[N,256,20,20]
  106. [-1, 1, Conv, [256, 1, 1]], #[N,256,20,20]
  107. [-3, 1, Conv, [256, 1, 1]], #[N,256,40,40]
  108. [-1, 1, Conv, [256, 3, 2]], #[N,256,20,20]
  109. [[-1, -3, 51], 1, Concat, [1]], #[N,1024,20,20]
  110. [-1, 1, Conv, [512, 1, 1]], #[N,512,20,20]
  111. [-2, 1, Conv, [512, 1, 1]], #[N,512,20,20]
  112. [-1, 1, Conv, [256, 3, 1]], #[N,256,20,20]
  113. [-1, 1, Conv, [256, 3, 1]], #[N,256,20,20]
  114. [-1, 1, Conv, [256, 3, 1]], #[N,256,20,20]
  115. [-1, 1, Conv, [256, 3, 1]], #[N,256,20,20]
  116. [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]], #[N,2048,20,20]
  117. [-1, 1, Conv, [512, 1, 1]], # 101 #[N,512,20,20]
  118. [75, 1, RepConv, [256, 3, 1]], #[N,256,80,80]
  119. [88, 1, RepConv, [512, 3, 1]], #[N,512,40,40]
  120. [101, 1, RepConv, [1024, 3, 1]], #[N,1024,20,20]
  121. [[102,103,104], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5)
  122. ]

网络结构图

网络结构分块讲解

1、基础模块

 主要是1x1以及3x3的卷积,3x3的卷积区别主要是stride不同,最后都是用SiLU进行激活。

 2、多路卷积模块

[-1, 1, Conv, [256, 1, 1]],                     #[4,256,40,40]
[-2, 1, Conv, [256, 1, 1]],                     #[4,256,40,40]
[-1, 1, Conv, [128, 3, 1]],                     #[4,128,40,40]
[-1, 1, Conv, [128, 3, 1]],                     #[4,128,40,40]
[-1, 1, Conv, [128, 3, 1]],                     #[4,128,40,40]
[-1, 1, Conv, [128, 3, 1]],                     #[4,128,40,40]
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],     #[4,1024,40,40]
[-1, 1, Conv, [256, 1, 1]], # 63                #[4,256,40,40]

or

[-1, 1, Conv, [64, 1, 1]],  # -6         [4,64,160,160]
[-2, 1, Conv, [64, 1, 1]],  # -5         [4,64,160,160]
[-1, 1, Conv, [64, 3, 1]],              #[4,64,160,160]
[-1, 1, Conv, [64, 3, 1]],  # -3        #[4,64,160,160]
[-1, 1, Conv, [64, 3, 1]],              #[4,64,160,160]
[-1, 1, Conv, [64, 3, 1]],  # -1        #[4,64,160,160]
[[-1, -3, -5, -6], 1, Concat, [1]],     #[4,256,160,160]
[-1, 1, Conv, [256, 1, 1]],  # 11       #[4,256,160,160]

 对应的模块

or

         其中主要是用了较多的1x1和3x3的卷积,每个卷积的输出会作为下一个卷积的输入,同时还会和其他卷积的输出进行concat,这样的结构提升了网络的精度但是这也会增加一定的耗时。

3、MP模块

[-1, 1, MP, []],                        #[4,256,80,80]
[-1, 1, Conv, [128, 1, 1]],             #[4,128,80,80]
[-3, 1, Conv, [128, 1, 1]],             #[4,128,160,160]
[-1, 1, Conv, [128, 3, 2]],             #[4,128,80,80]

MP代码如下:

  1. class MP(nn.Module):
  2. def __init__(self, k=2):
  3. super(MP, self).__init__()
  4. self.m = nn.MaxPool2d(kernel_size=k, stride=k)
  5. def forward(self, x):
  6. return self.m(x)

图解结构如下: 

          

        将kernel_size和stride设置为2的Maxpool2d,该模块会用到1x1卷积,3x3卷积,以及一个Maxpool2d,之后将卷积输出进行concat,从而组成了该模块。

4、SPPCSPC模块

        该模块是利用金字塔池化和CSP结构得到的模块,其中也是用了较多的支路,具体代码如下:

  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))

        图解之后得到下图:

         其中的MP就是金字塔池化,最后将两条分支的Y1和Y2的输出进行concat。

5、RepConv模块

 代码如下:

  1. class RepConv(nn.Module):
  2. # Represented convolution
  3. # https://arxiv.org/abs/2101.03697
  4. def __init__(self, c1, c2, k=3, s=1, p=None, g=1, act=True, deploy=False):
  5. super(RepConv, self).__init__()
  6. self.deploy = deploy
  7. self.groups = g
  8. self.in_channels = c1
  9. self.out_channels = c2
  10. assert k == 3
  11. assert autopad(k, p) == 1
  12. padding_11 = autopad(k, p) - k // 2
  13. self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
  14. if deploy:
  15. self.rbr_reparam = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=True)
  16. else:
  17. self.rbr_identity = (nn.BatchNorm2d(num_features=c1) if c2 == c1 and s == 1 else None)
  18. self.rbr_dense = nn.Sequential(
  19. nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False),
  20. nn.BatchNorm2d(num_features=c2),
  21. )
  22. self.rbr_1x1 = nn.Sequential(
  23. nn.Conv2d( c1, c2, 1, s, padding_11, groups=g, bias=False),
  24. nn.BatchNorm2d(num_features=c2),
  25. )
  26. def forward(self, inputs):
  27. if hasattr(self, "rbr_reparam"):
  28. return self.act(self.rbr_reparam(inputs))
  29. if self.rbr_identity is None:
  30. id_out = 0
  31. else:
  32. id_out = self.rbr_identity(inputs)
  33. return self.act(self.rbr_dense(inputs) + self.rbr_1x1(inputs) + id_out)

图解之后得到下图:

该模块的具体讲解可以参看:BACKBONE部分。

最后,如有理解不当的地方,欢迎大佬来纠错。

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

闽ICP备14008679号