yolov8_pafpn.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. c3, c4, c5 = in_dims
  25. # top dwon
  26. ## P5 -> P4
  27. self.head_elan_1 = ELAN_CSP_Block(in_dim=c5 + c4,
  28. out_dim=int(512*width),
  29. expand_ratio=0.5,
  30. nblocks=int(3*depth),
  31. shortcut=False,
  32. depthwise=depthwise,
  33. norm_type=norm_type,
  34. act_type=act_type
  35. )
  36. # P4 -> P3
  37. self.head_elan_2 = ELAN_CSP_Block(in_dim=int(512*width) + c3,
  38. out_dim=int(256*width),
  39. expand_ratio=0.5,
  40. nblocks=int(3*depth),
  41. shortcut=False,
  42. depthwise=depthwise,
  43. norm_type=norm_type,
  44. act_type=act_type
  45. )
  46. # bottom up
  47. # P3 -> P4
  48. self.mp1 = Conv(int(256*width), int(256*width), k=3, p=1, s=2,
  49. act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  50. self.head_elan_3 = ELAN_CSP_Block(in_dim=int(256*width) + int(512*width),
  51. out_dim=int(512*width),
  52. expand_ratio=0.5,
  53. nblocks=int(3*depth),
  54. shortcut=False,
  55. depthwise=depthwise,
  56. norm_type=norm_type,
  57. act_type=act_type
  58. )
  59. # P4 -> P5
  60. self.mp2 = Conv(int(512 * width), int(512 * width), k=3, p=1, s=2,
  61. act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  62. self.head_elan_4 = ELAN_CSP_Block(in_dim=int(512 * width) + c5,
  63. out_dim=int(512 * width * ratio),
  64. expand_ratio=0.5,
  65. nblocks=int(3*depth),
  66. shortcut=False,
  67. depthwise=depthwise,
  68. norm_type=norm_type,
  69. act_type=act_type
  70. )
  71. self.out_dim = [int(256 * width), int(512 * width), int(512 * width * ratio)]
  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))