yolov8_pafpn.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from .yolov8_basic import Conv, Yolov8StageBlock
  5. # PaFPN-ELAN
  6. class Yolov8PaFPN(nn.Module):
  7. def __init__(self,
  8. in_dims = [256, 512, 512],
  9. width = 1.0,
  10. depth = 1.0,
  11. ratio = 1.0,
  12. act_type = 'silu',
  13. norm_type = 'BN',
  14. depthwise = False):
  15. super(Yolov8PaFPN, self).__init__()
  16. print('==============================')
  17. print('FPN: {}'.format("Yolov8 PaFPN"))
  18. # ---------------- Basic parameters ----------------
  19. self.in_dims = in_dims
  20. self.width = width
  21. self.depth = depth
  22. self.out_dim = [round(256 * width), round(512 * width), round(512 * width * ratio)]
  23. c3, c4, c5 = in_dims
  24. # ---------------- Top dwon ----------------
  25. ## P5 -> P4
  26. self.top_down_layer_1 = Yolov8StageBlock(in_dim = c5 + c4,
  27. out_dim = round(512*width),
  28. num_blocks = round(3*depth),
  29. shortcut = False,
  30. act_type = act_type,
  31. norm_type = norm_type,
  32. depthwise = depthwise,
  33. )
  34. ## P4 -> P3
  35. self.top_down_layer_2 = Yolov8StageBlock(in_dim = round(512*width) + c3,
  36. out_dim = round(256*width),
  37. num_blocks = round(3*depth),
  38. shortcut = False,
  39. act_type = act_type,
  40. norm_type = norm_type,
  41. depthwise = depthwise,
  42. )
  43. # ---------------- Bottom up ----------------
  44. ## P3 -> P4
  45. 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)
  46. self.bottom_up_layer_1 = Yolov8StageBlock(in_dim = round(256*width) + round(512*width),
  47. out_dim = round(512*width),
  48. num_blocks = round(3*depth),
  49. shortcut = False,
  50. act_type = act_type,
  51. norm_type = norm_type,
  52. depthwise = depthwise,
  53. )
  54. ## P4 -> P5
  55. 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)
  56. self.bottom_up_layer_2 = Yolov8StageBlock(in_dim = round(512 * width) + c5,
  57. out_dim = round(512 * width * ratio),
  58. num_blocks = round(3*depth),
  59. shortcut = False,
  60. act_type = act_type,
  61. norm_type = norm_type,
  62. depthwise = depthwise,
  63. )
  64. self.init_weights()
  65. def init_weights(self):
  66. """Initialize the parameters."""
  67. for m in self.modules():
  68. if isinstance(m, torch.nn.Conv2d):
  69. # In order to be consistent with the source code,
  70. # reset the Conv2d initialization parameters
  71. m.reset_parameters()
  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.top_down_layer_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.top_down_layer_2(c10)
  83. # Bottom up
  84. # p3 -> P4
  85. c12 = self.dowmsample_layer_1(c11)
  86. c13 = torch.cat([c12, c8], dim=1)
  87. c14 = self.bottom_up_layer_1(c13)
  88. # P4 -> P5
  89. c15 = self.dowmsample_layer_2(c14)
  90. c16 = torch.cat([c15, c5], dim=1)
  91. c17 = self.bottom_up_layer_2(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