yolov8_pafpn.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. try:
  5. from .yolov8_basic import Conv, Yolov8StageBlock
  6. except:
  7. from yolov8_basic import Conv, Yolov8StageBlock
  8. # PaFPN-ELAN
  9. class Yolov8PaFPN(nn.Module):
  10. def __init__(self,
  11. in_dims = [256, 512, 512],
  12. width = 1.0,
  13. depth = 1.0,
  14. ratio = 1.0,
  15. act_type = 'silu',
  16. norm_type = 'BN',
  17. depthwise = False):
  18. super(Yolov8PaFPN, self).__init__()
  19. print('==============================')
  20. print('FPN: {}'.format("Yolov8 PaFPN"))
  21. # ---------------- Basic parameters ----------------
  22. self.in_dims = in_dims
  23. self.width = width
  24. self.depth = depth
  25. self.out_dim = [round(256 * width), round(512 * width), round(512 * width * ratio)]
  26. c3, c4, c5 = in_dims
  27. # ---------------- Top dwon ----------------
  28. ## P5 -> P4
  29. self.top_down_layer_1 = Yolov8StageBlock(in_dim = c5 + c4,
  30. out_dim = round(512*width),
  31. expand_ratio = 0.5,
  32. num_blocks = round(3*depth),
  33. shortcut = False,
  34. act_type = act_type,
  35. norm_type = norm_type,
  36. depthwise = depthwise,
  37. )
  38. ## P4 -> P3
  39. self.top_down_layer_2 = Yolov8StageBlock(in_dim = round(512*width) + c3,
  40. out_dim = round(256*width),
  41. expand_ratio = 0.5,
  42. num_blocks = round(3*depth),
  43. shortcut = False,
  44. act_type = act_type,
  45. norm_type = norm_type,
  46. depthwise = depthwise,
  47. )
  48. # ---------------- Bottom up ----------------
  49. ## P3 -> P4
  50. self.dowmsample_layer_1 = Conv(round(256*width), round(256*width), k=3, p=1, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  51. self.bottom_up_layer_1 = Yolov8StageBlock(in_dim = round(256*width) + round(512*width),
  52. out_dim = round(512*width),
  53. expand_ratio = 0.5,
  54. num_blocks = round(3*depth),
  55. shortcut = False,
  56. act_type = act_type,
  57. norm_type = norm_type,
  58. depthwise = depthwise,
  59. )
  60. ## P4 -> P5
  61. self.dowmsample_layer_2 = Conv(round(512*width), round(512*width), k=3, p=1, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  62. self.bottom_up_layer_2 = Yolov8StageBlock(in_dim = round(512 * width) + c5,
  63. out_dim = round(512 * width * ratio),
  64. expand_ratio = 0.5,
  65. num_blocks = round(3*depth),
  66. shortcut = False,
  67. act_type = act_type,
  68. norm_type = norm_type,
  69. depthwise = depthwise,
  70. )
  71. def forward(self, features):
  72. c3, c4, c5 = features
  73. # Top down
  74. ## P5 -> P4
  75. c6 = F.interpolate(c5, scale_factor=2.0)
  76. c7 = torch.cat([c6, c4], dim=1)
  77. c8 = self.top_down_layer_1(c7)
  78. ## P4 -> P3
  79. c9 = F.interpolate(c8, scale_factor=2.0)
  80. c10 = torch.cat([c9, c3], dim=1)
  81. c11 = self.top_down_layer_2(c10)
  82. # Bottom up
  83. # p3 -> P4
  84. c12 = self.dowmsample_layer_1(c11)
  85. c13 = torch.cat([c12, c8], dim=1)
  86. c14 = self.bottom_up_layer_1(c13)
  87. # P4 -> P5
  88. c15 = self.dowmsample_layer_2(c14)
  89. c16 = torch.cat([c15, c5], dim=1)
  90. c17 = self.bottom_up_layer_2(c16)
  91. out_feats = [c11, c14, c17] # [P3, P4, P5]
  92. return out_feats
  93. def build_fpn(cfg, in_dims):
  94. model = cfg['fpn']
  95. # build neck
  96. if model == 'yolov8_pafpn':
  97. fpn_net = Yolov8PaFPN(in_dims=in_dims,
  98. width=cfg['width'],
  99. depth=cfg['depth'],
  100. ratio=cfg['ratio'],
  101. act_type=cfg['fpn_act'],
  102. norm_type=cfg['fpn_norm'],
  103. depthwise=cfg['fpn_depthwise']
  104. )
  105. return fpn_net
  106. if __name__ == '__main__':
  107. import time
  108. from thop import profile
  109. cfg = {
  110. 'fpn': 'yolov8_pafpn',
  111. 'fpn_act': 'silu',
  112. 'fpn_norm': 'BN',
  113. 'fpn_depthwise': False,
  114. 'width': 1.0,
  115. 'depth': 1.0,
  116. 'ratio': 1.0,
  117. }
  118. model = build_fpn(cfg, in_dims=[256, 512, 512])
  119. pyramid_feats = [torch.randn(1, 256, 80, 80), torch.randn(1, 512, 40, 40), torch.randn(1, 512, 20, 20)]
  120. t0 = time.time()
  121. outputs = model(pyramid_feats)
  122. t1 = time.time()
  123. print('Time: ', t1 - t0)
  124. for out in outputs:
  125. print(out.shape)
  126. print('==============================')
  127. flops, params = profile(model, inputs=(pyramid_feats, ), verbose=False)
  128. print('==============================')
  129. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  130. print('Params : {:.2f} M'.format(params / 1e6))