yolov8_pafpn.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. self.init_weights()
  68. def init_weights(self):
  69. """Initialize the parameters."""
  70. for m in self.modules():
  71. if isinstance(m, torch.nn.Conv2d):
  72. # In order to be consistent with the source code,
  73. # reset the Conv2d initialization parameters
  74. m.reset_parameters()
  75. def forward(self, features):
  76. c3, c4, c5 = features
  77. # Top down
  78. ## P5 -> P4
  79. c6 = F.interpolate(c5, scale_factor=2.0)
  80. c7 = torch.cat([c6, c4], dim=1)
  81. c8 = self.top_down_layer_1(c7)
  82. ## P4 -> P3
  83. c9 = F.interpolate(c8, scale_factor=2.0)
  84. c10 = torch.cat([c9, c3], dim=1)
  85. c11 = self.top_down_layer_2(c10)
  86. # Bottom up
  87. # p3 -> P4
  88. c12 = self.dowmsample_layer_1(c11)
  89. c13 = torch.cat([c12, c8], dim=1)
  90. c14 = self.bottom_up_layer_1(c13)
  91. # P4 -> P5
  92. c15 = self.dowmsample_layer_2(c14)
  93. c16 = torch.cat([c15, c5], dim=1)
  94. c17 = self.bottom_up_layer_2(c16)
  95. out_feats = [c11, c14, c17] # [P3, P4, P5]
  96. return out_feats
  97. def build_fpn(cfg, in_dims):
  98. model = cfg['fpn']
  99. # build neck
  100. if model == 'yolov8_pafpn':
  101. fpn_net = Yolov8PaFPN(in_dims=in_dims,
  102. width=cfg['width'],
  103. depth=cfg['depth'],
  104. ratio=cfg['ratio'],
  105. act_type=cfg['fpn_act'],
  106. norm_type=cfg['fpn_norm'],
  107. depthwise=cfg['fpn_depthwise']
  108. )
  109. return fpn_net
  110. if __name__ == '__main__':
  111. import time
  112. from thop import profile
  113. cfg = {
  114. 'fpn': 'yolov8_pafpn',
  115. 'fpn_act': 'silu',
  116. 'fpn_norm': 'BN',
  117. 'fpn_depthwise': False,
  118. 'width': 0.25,
  119. 'depth': 0.34,
  120. 'ratio': 2.0,
  121. }
  122. model = build_fpn(cfg, in_dims=[64, 128, 256])
  123. pyramid_feats = [torch.randn(1, 64, 80, 80), torch.randn(1, 128, 40, 40), torch.randn(1, 256, 20, 20)]
  124. t0 = time.time()
  125. outputs = model(pyramid_feats)
  126. t1 = time.time()
  127. print('Time: ', t1 - t0)
  128. for out in outputs:
  129. print(out.shape)
  130. print('==============================')
  131. flops, params = profile(model, inputs=(pyramid_feats, ), verbose=False)
  132. print('==============================')
  133. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  134. print('Params : {:.2f} M'.format(params / 1e6))