rtcdet_pafpn.py 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 ELANLayerFPN, MDown, BasicConv
  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("RTC-PaFPN"))
  15. # --------------------------- Basic Parameters ---------------------------
  16. self.in_dims = in_dims[::-1]
  17. self.out_dims = [round(256*cfg.channel_width), round(512*cfg.channel_width), round(1024*cfg.channel_width)]
  18. # ----------------------------- Yolov8's Top-down FPN -----------------------------
  19. ## P5 -> P4
  20. self.top_down_layer_1 = ELANLayerFPN(in_dim = self.in_dims[0] + self.in_dims[1],
  21. out_dim = round(512*cfg.channel_width),
  22. expansion = 0.5,
  23. num_blocks = cfg.fpn_num_blocks,
  24. act_type = cfg.fpn_act,
  25. norm_type = cfg.fpn_norm,
  26. depthwise = cfg.fpn_depthwise,
  27. )
  28. ## P4 -> P3
  29. self.top_down_layer_2 = ELANLayerFPN(in_dim = self.in_dims[2] + round(512*cfg.channel_width),
  30. out_dim = round(256*cfg.channel_width),
  31. expansion = 0.5,
  32. num_blocks = cfg.fpn_num_blocks,
  33. act_type = cfg.fpn_act,
  34. norm_type = cfg.fpn_norm,
  35. depthwise = cfg.fpn_depthwise,
  36. )
  37. # ----------------------------- Yolov8's Bottom-up PAN -----------------------------
  38. ## P3 -> P4
  39. self.dowmsample_layer_1 = MDown(round(256*cfg.channel_width), round(256*cfg.channel_width),
  40. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  41. self.bottom_up_layer_1 = ELANLayerFPN(in_dim = round(256*cfg.channel_width) + round(512*cfg.channel_width),
  42. out_dim = round(512*cfg.channel_width),
  43. expansion = 0.5,
  44. num_blocks = cfg.fpn_num_blocks,
  45. act_type = cfg.fpn_act,
  46. norm_type = cfg.fpn_norm,
  47. depthwise = cfg.fpn_depthwise,
  48. )
  49. ## P4 -> P5
  50. self.dowmsample_layer_2 = MDown(round(512*cfg.channel_width), round(512*cfg.channel_width),
  51. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  52. self.bottom_up_layer_2 = ELANLayerFPN(in_dim = round(512*cfg.channel_width) + self.in_dims[0],
  53. out_dim = round(1024*cfg.channel_width),
  54. expansion = 0.5,
  55. num_blocks = cfg.fpn_num_blocks,
  56. act_type = cfg.fpn_act,
  57. norm_type = cfg.fpn_norm,
  58. depthwise = cfg.fpn_depthwise,
  59. )
  60. self.init_weights()
  61. def init_weights(self):
  62. """Initialize the parameters."""
  63. for m in self.modules():
  64. if isinstance(m, torch.nn.Conv2d):
  65. # In order to be consistent with the source code,
  66. # reset the Conv2d initialization parameters
  67. m.reset_parameters()
  68. def forward(self, features):
  69. c3, c4, c5 = features
  70. # ------------------ Top down FPN ------------------
  71. ## P5 -> P4
  72. p5_up = F.interpolate(c5, scale_factor=2.0)
  73. p4 = self.top_down_layer_1(torch.cat([p5_up, c4], dim=1))
  74. ## P4 -> P3
  75. p4_up = F.interpolate(p4, scale_factor=2.0)
  76. p3 = self.top_down_layer_2(torch.cat([p4_up, c3], dim=1))
  77. # ------------------ Bottom up FPN ------------------
  78. ## p3 -> P4
  79. p3_ds = self.dowmsample_layer_1(p3)
  80. p4 = self.bottom_up_layer_1(torch.cat([p3_ds, p4], dim=1))
  81. ## P4 -> 5
  82. p4_ds = self.dowmsample_layer_2(p4)
  83. p5 = self.bottom_up_layer_2(torch.cat([p4_ds, c5], dim=1))
  84. out_feats = [p3, p4, p5] # [P3, P4, P5]
  85. return out_feats