yolov8_pafpn.py 5.5 KB

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