gelan_pafpn.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from typing import List
  5. from .gelan_basic import RepGElanLayer, ADown
  6. # PaFPN-ELAN
  7. class GElanPaFPN(nn.Module):
  8. def __init__(self,
  9. cfg,
  10. in_dims :List = [256, 512, 256],
  11. ) -> None:
  12. super(GElanPaFPN, self).__init__()
  13. print('==============================')
  14. print('FPN: {}'.format("GELAN PaFPN"))
  15. # --------------------------- Basic Parameters ---------------------------
  16. self.in_dims = in_dims[::-1]
  17. self.out_dims = [cfg.fpn_feats_td["p3"][1], cfg.fpn_feats_bu["p4"][1], cfg.fpn_feats_bu["p5"][1]]
  18. # ---------------- Top dwon ----------------
  19. ## P5 -> P4
  20. self.top_down_layer_1 = RepGElanLayer(in_dim = self.in_dims[0] + self.in_dims[1],
  21. inter_dims = cfg.fpn_feats_td["p4"][0],
  22. out_dim = cfg.fpn_feats_td["p4"][1],
  23. num_blocks = cfg.fpn_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 = RepGElanLayer(in_dim = cfg.fpn_feats_td["p4"][1] + self.in_dims[2],
  31. inter_dims = cfg.fpn_feats_td["p3"][0],
  32. out_dim = cfg.fpn_feats_td["p3"][1],
  33. num_blocks = cfg.fpn_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 = ADown(cfg.fpn_feats_td["p3"][1], cfg.fpn_feats_td["p3"][1],
  42. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  43. self.bottom_up_layer_1 = RepGElanLayer(in_dim = cfg.fpn_feats_td["p3"][1] + cfg.fpn_feats_td["p4"][1],
  44. inter_dims = cfg.fpn_feats_bu["p4"][0],
  45. out_dim = cfg.fpn_feats_bu["p4"][1],
  46. num_blocks = cfg.fpn_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 = ADown(cfg.fpn_feats_bu["p4"][1], cfg.fpn_feats_bu["p4"][1],
  54. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  55. self.bottom_up_layer_2 = RepGElanLayer(in_dim = cfg.fpn_feats_td["p4"][1] + self.in_dims[0],
  56. inter_dims = cfg.fpn_feats_bu["p5"][0],
  57. out_dim = cfg.fpn_feats_bu["p5"][1],
  58. num_blocks = cfg.fpn_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