yolov8_pafpn.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from typing import List
  5. from .yolov8_basic import BasicConv, ELANLayer
  6. # Modified YOLOv8's PaFPN
  7. class Yolov8PaFPN(nn.Module):
  8. def __init__(self,
  9. cfg,
  10. in_dims :List = [256, 512, 1024],
  11. ) -> None:
  12. super(Yolov8PaFPN, self).__init__()
  13. print('==============================')
  14. print('FPN: {}'.format("Yolo PaFPN"))
  15. # --------------------------- Basic Parameters ---------------------------
  16. self.in_dims = in_dims[::-1]
  17. self.out_dims = [round(cfg.head_dim * cfg.width)] * 3
  18. # ---------------- Top dwon ----------------
  19. ## P5 -> P4
  20. self.top_down_layer_1 = ELANLayer(in_dim = self.in_dims[0] + self.in_dims[1],
  21. out_dim = round(512*cfg.width),
  22. expansion = 0.5,
  23. num_blocks = round(3 * cfg.depth),
  24. shortcut = False,
  25. act_type = cfg.fpn_act,
  26. norm_type = cfg.fpn_norm,
  27. depthwise = cfg.fpn_depthwise,
  28. )
  29. ## P4 -> P3
  30. self.top_down_layer_2 = ELANLayer(in_dim = self.in_dims[2] + round(512*cfg.width),
  31. out_dim = round(256*cfg.width),
  32. expansion = 0.5,
  33. num_blocks = round(3 * cfg.depth),
  34. shortcut = False,
  35. act_type = cfg.fpn_act,
  36. norm_type = cfg.fpn_norm,
  37. depthwise = cfg.fpn_depthwise,
  38. )
  39. # ---------------- Bottom up ----------------
  40. ## P3 -> P4
  41. self.dowmsample_layer_1 = BasicConv(round(256*cfg.width), round(256*cfg.width),
  42. kernel_size=3, padding=1, stride=2,
  43. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  44. self.bottom_up_layer_1 = ELANLayer(in_dim = round(256*cfg.width) + round(512*cfg.width),
  45. out_dim = round(512*cfg.width),
  46. expansion = 0.5,
  47. num_blocks = round(3 * cfg.depth),
  48. shortcut = False,
  49. act_type = cfg.fpn_act,
  50. norm_type = cfg.fpn_norm,
  51. depthwise = cfg.fpn_depthwise,
  52. )
  53. ## P4 -> P5
  54. self.dowmsample_layer_2 = BasicConv(round(512*cfg.width), round(512*cfg.width),
  55. kernel_size=3, padding=1, stride=2,
  56. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  57. self.bottom_up_layer_2 = ELANLayer(in_dim = round(512*cfg.width) + self.in_dims[0],
  58. out_dim = round(512*cfg.width*cfg.ratio),
  59. expansion = 0.5,
  60. num_blocks = round(3 * cfg.depth),
  61. shortcut = False,
  62. act_type = cfg.fpn_act,
  63. norm_type = cfg.fpn_norm,
  64. depthwise = cfg.fpn_depthwise,
  65. )
  66. self.out_layers = nn.ModuleList([
  67. BasicConv(feat_dim, self.out_dims[i], kernel_size=1, act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  68. for i, feat_dim in enumerate([round(256*cfg.width), round(512*cfg.width), round(512*cfg.width*cfg.ratio)])
  69. ])
  70. self.init_weights()
  71. def init_weights(self):
  72. """Initialize the parameters."""
  73. for m in self.modules():
  74. if isinstance(m, torch.nn.Conv2d):
  75. # In order to be consistent with the source code,
  76. # reset the Conv2d initialization parameters
  77. m.reset_parameters()
  78. def forward(self, features):
  79. c3, c4, c5 = features
  80. # ------------------ Top down FPN ------------------
  81. ## P5 -> P4
  82. p5_up = F.interpolate(c5, scale_factor=2.0)
  83. p4 = self.top_down_layer_1(torch.cat([p5_up, c4], dim=1))
  84. ## P4 -> P3
  85. p4_up = F.interpolate(p4, scale_factor=2.0)
  86. p3 = self.top_down_layer_2(torch.cat([p4_up, c3], dim=1))
  87. # ------------------ Bottom up FPN ------------------
  88. ## p3 -> P4
  89. p3_ds = self.dowmsample_layer_1(p3)
  90. p4 = self.bottom_up_layer_1(torch.cat([p3_ds, p4], dim=1))
  91. ## P4 -> 5
  92. p4_ds = self.dowmsample_layer_2(p4)
  93. p5 = self.bottom_up_layer_2(torch.cat([p4_ds, c5], dim=1))
  94. out_feats = [p3, p4, p5] # [P3, P4, P5]
  95. # output proj layers
  96. out_feats_proj = []
  97. for feat, layer in zip(out_feats, self.out_layers):
  98. out_feats_proj.append(layer(feat))
  99. return out_feats_proj