pafpn.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from .basic import Conv, RTCBlock
  5. # Build PaFPN
  6. def build_pafpn(cfg, in_dims, out_dim):
  7. return
  8. # ----------------- Feature Pyramid Network -----------------
  9. ## Real-time Convolutional PaFPN
  10. class RTCPaFPN(nn.Module):
  11. def __init__(self,
  12. in_dims = [256, 512, 512],
  13. width = 1.0,
  14. depth = 1.0,
  15. ratio = 1.0,
  16. act_type = 'silu',
  17. norm_type = 'BN',
  18. depthwise = False):
  19. super(RTCPaFPN, self).__init__()
  20. print('==============================')
  21. print('FPN: {}'.format("RTC-PaFPN"))
  22. # ---------------- Basic parameters ----------------
  23. self.in_dims = in_dims
  24. self.width = width
  25. self.depth = depth
  26. self.out_dim = [round(256 * width), round(512 * width), round(512 * width * ratio)]
  27. c3, c4, c5 = in_dims
  28. # ---------------- Top dwon ----------------
  29. ## P5 -> P4
  30. self.top_down_layer_1 = RTCBlock(in_dim = c5 + c4,
  31. out_dim = round(512*width),
  32. num_blocks = round(3*depth),
  33. shortcut = False,
  34. act_type = act_type,
  35. norm_type = norm_type,
  36. depthwise = depthwise,
  37. )
  38. ## P4 -> P3
  39. self.top_down_layer_2 = RTCBlock(in_dim = round(512*width) + c3,
  40. out_dim = round(256*width),
  41. num_blocks = round(3*depth),
  42. shortcut = False,
  43. act_type = act_type,
  44. norm_type = norm_type,
  45. depthwise = depthwise,
  46. )
  47. # ---------------- Bottom up ----------------
  48. ## P3 -> P4
  49. 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)
  50. self.bottom_up_layer_1 = RTCBlock(in_dim = round(256*width) + round(512*width),
  51. out_dim = round(512*width),
  52. num_blocks = round(3*depth),
  53. shortcut = False,
  54. act_type = act_type,
  55. norm_type = norm_type,
  56. depthwise = depthwise,
  57. )
  58. ## P4 -> P5
  59. 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)
  60. self.bottom_up_layer_2 = RTCBlock(in_dim = round(512 * width) + c5,
  61. out_dim = round(512 * width * ratio),
  62. num_blocks = round(3*depth),
  63. shortcut = False,
  64. act_type = act_type,
  65. norm_type = norm_type,
  66. depthwise = depthwise,
  67. )
  68. self.init_weights()
  69. def init_weights(self):
  70. """Initialize the parameters."""
  71. for m in self.modules():
  72. if isinstance(m, torch.nn.Conv2d):
  73. # In order to be consistent with the source code,
  74. # reset the Conv2d initialization parameters
  75. m.reset_parameters()
  76. def forward(self, features):
  77. c3, c4, c5 = features
  78. # Top down
  79. ## P5 -> P4
  80. c6 = F.interpolate(c5, scale_factor=2.0)
  81. c7 = torch.cat([c6, c4], dim=1)
  82. c8 = self.top_down_layer_1(c7)
  83. ## P4 -> P3
  84. c9 = F.interpolate(c8, scale_factor=2.0)
  85. c10 = torch.cat([c9, c3], dim=1)
  86. c11 = self.top_down_layer_2(c10)
  87. # Bottom up
  88. # p3 -> P4
  89. c12 = self.dowmsample_layer_1(c11)
  90. c13 = torch.cat([c12, c8], dim=1)
  91. c14 = self.bottom_up_layer_1(c13)
  92. # P4 -> P5
  93. c15 = self.dowmsample_layer_2(c14)
  94. c16 = torch.cat([c15, c5], dim=1)
  95. c17 = self.bottom_up_layer_2(c16)
  96. out_feats = [c11, c14, c17] # [P3, P4, P5]
  97. return out_feats