yolov8_pafpn.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. num_blocks = round(3*depth),
  32. shortcut = False,
  33. act_type = act_type,
  34. norm_type = norm_type,
  35. depthwise = depthwise,
  36. )
  37. ## P4 -> P3
  38. self.top_down_layer_2 = Yolov8StageBlock(in_dim = round(512*width) + c3,
  39. out_dim = round(256*width),
  40. num_blocks = round(3*depth),
  41. shortcut = False,
  42. act_type = act_type,
  43. norm_type = norm_type,
  44. depthwise = depthwise,
  45. )
  46. # ---------------- Bottom up ----------------
  47. ## P3 -> P4
  48. 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)
  49. self.bottom_up_layer_1 = Yolov8StageBlock(in_dim = round(256*width) + round(512*width),
  50. out_dim = round(512*width),
  51. num_blocks = round(3*depth),
  52. shortcut = False,
  53. act_type = act_type,
  54. norm_type = norm_type,
  55. depthwise = depthwise,
  56. )
  57. ## P4 -> P5
  58. 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)
  59. self.bottom_up_layer_2 = Yolov8StageBlock(in_dim = round(512 * width) + c5,
  60. out_dim = round(512 * width * ratio),
  61. num_blocks = round(3*depth),
  62. shortcut = False,
  63. act_type = act_type,
  64. norm_type = norm_type,
  65. depthwise = depthwise,
  66. )
  67. def forward(self, features):
  68. c3, c4, c5 = features
  69. # Top down
  70. ## P5 -> P4
  71. c6 = F.interpolate(c5, scale_factor=2.0)
  72. c7 = torch.cat([c6, c4], dim=1)
  73. c8 = self.top_down_layer_1(c7)
  74. ## P4 -> P3
  75. c9 = F.interpolate(c8, scale_factor=2.0)
  76. c10 = torch.cat([c9, c3], dim=1)
  77. c11 = self.top_down_layer_2(c10)
  78. # Bottom up
  79. # p3 -> P4
  80. c12 = self.dowmsample_layer_1(c11)
  81. c13 = torch.cat([c12, c8], dim=1)
  82. c14 = self.bottom_up_layer_1(c13)
  83. # P4 -> P5
  84. c15 = self.dowmsample_layer_2(c14)
  85. c16 = torch.cat([c15, c5], dim=1)
  86. c17 = self.bottom_up_layer_2(c16)
  87. out_feats = [c11, c14, c17] # [P3, P4, P5]
  88. return out_feats
  89. def build_fpn(cfg, in_dims):
  90. model = cfg['fpn']
  91. # build neck
  92. if model == 'yolov8_pafpn':
  93. fpn_net = Yolov8PaFPN(in_dims=in_dims,
  94. width=cfg['width'],
  95. depth=cfg['depth'],
  96. ratio=cfg['ratio'],
  97. act_type=cfg['fpn_act'],
  98. norm_type=cfg['fpn_norm'],
  99. depthwise=cfg['fpn_depthwise']
  100. )
  101. return fpn_net
  102. if __name__ == '__main__':
  103. import time
  104. from thop import profile
  105. cfg = {
  106. 'fpn': 'yolov8_pafpn',
  107. 'fpn_act': 'silu',
  108. 'fpn_norm': 'BN',
  109. 'fpn_depthwise': False,
  110. 'width': 1.0,
  111. 'depth': 1.0,
  112. 'ratio': 1.0,
  113. }
  114. model = build_fpn(cfg, in_dims=[256, 512, 512])
  115. pyramid_feats = [torch.randn(1, 256, 80, 80), torch.randn(1, 512, 40, 40), torch.randn(1, 512, 20, 20)]
  116. t0 = time.time()
  117. outputs = model(pyramid_feats)
  118. t1 = time.time()
  119. print('Time: ', t1 - t0)
  120. for out in outputs:
  121. print(out.shape)
  122. print('==============================')
  123. flops, params = profile(model, inputs=(pyramid_feats, ), verbose=False)
  124. print('==============================')
  125. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  126. print('Params : {:.2f} M'.format(params / 1e6))