rtcdet_pafpn.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from typing import List
  5. from .rtcdet_basic import ELANLayer, MDown
  6. # Modified YOLOv8's PaFPN
  7. class RTCPaFPN(nn.Module):
  8. def __init__(self,
  9. cfg,
  10. in_dims :List = [256, 512, 1024],
  11. ) -> None:
  12. super(RTCPaFPN, 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(256*cfg.width), round(512*cfg.width), round(512*cfg.width*cfg.ratio)]
  18. # ----------------------------- Yolov8's Top-down FPN -----------------------------
  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. # ----------------------------- Yolov8's Bottom-up PAN -----------------------------
  40. ## P3 -> P4
  41. self.dowmsample_layer_1 = MDown(round(256*cfg.width), round(256*cfg.width),
  42. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  43. self.bottom_up_layer_1 = ELANLayer(in_dim = round(256*cfg.width) + round(512*cfg.width),
  44. out_dim = round(512*cfg.width),
  45. expansion = 0.5,
  46. num_blocks = round(3 * cfg.depth),
  47. shortcut = False,
  48. act_type = cfg.fpn_act,
  49. norm_type = cfg.fpn_norm,
  50. depthwise = cfg.fpn_depthwise,
  51. )
  52. ## P4 -> P5
  53. self.dowmsample_layer_2 = MDown(round(512*cfg.width), round(512*cfg.width),
  54. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  55. self.bottom_up_layer_2 = ELANLayer(in_dim = round(512*cfg.width) + self.in_dims[0],
  56. out_dim = round(512*cfg.width*cfg.ratio),
  57. expansion = 0.5,
  58. num_blocks = round(3 * cfg.depth),
  59. shortcut = False,
  60. act_type = cfg.fpn_act,
  61. norm_type = cfg.fpn_norm,
  62. depthwise = cfg.fpn_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 FPN ------------------
  75. ## P5 -> P4
  76. p5_up = F.interpolate(c5, scale_factor=2.0)
  77. p4 = self.top_down_layer_1(torch.cat([p5_up, c4], dim=1))
  78. ## P4 -> P3
  79. p4_up = F.interpolate(p4, scale_factor=2.0)
  80. p3 = self.top_down_layer_2(torch.cat([p4_up, c3], dim=1))
  81. # ------------------ Bottom up FPN ------------------
  82. ## p3 -> P4
  83. p3_ds = self.dowmsample_layer_1(p3)
  84. p4 = self.bottom_up_layer_1(torch.cat([p3_ds, p4], dim=1))
  85. ## P4 -> 5
  86. p4_ds = self.dowmsample_layer_2(p4)
  87. p5 = self.bottom_up_layer_2(torch.cat([p4_ds, c5], dim=1))
  88. out_feats = [p3, p4, p5] # [P3, P4, P5]
  89. return out_feats