rtcdet_pafpn.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from typing import List
  5. try:
  6. from .rtcdet_basic import BasicConv, DWConv, ElanLayer, MDown, ADown
  7. except:
  8. from rtcdet_basic import BasicConv, DWConv, ElanLayer, MDown, ADown
  9. # -------------- Feature pyramid network --------------
  10. class RTCPaFPN(nn.Module):
  11. def __init__(self,
  12. cfg,
  13. in_dims :List = [256, 512, 1024],
  14. ) -> None:
  15. super(RTCPaFPN, self).__init__()
  16. print('==============================')
  17. print('FPN: {}'.format("RTC-PaFPN"))
  18. # ----------- Basic Parameters -----------
  19. self.in_dims = in_dims[::-1]
  20. # ----------- Yolov8's Top-down FPN -----------
  21. ## P5 -> P4
  22. self.top_down_layer_1 = self.make_fpn_block(cfg, self.in_dims[0] + self.in_dims[1], round(512*cfg.width), round(3 * cfg.depth))
  23. ## P4 -> P3
  24. self.top_down_layer_2 = self.make_fpn_block(cfg, self.in_dims[2] + round(512*cfg.width), round(256*cfg.width), round(3 * cfg.depth))
  25. # ----------- Yolov8's Bottom-up PAN -----------
  26. ## P3 -> P4
  27. self.dowmsample_layer_1 = self.make_downsample_block(cfg, round(256*cfg.width), round(256*cfg.width))
  28. self.bottom_up_layer_1 = self.make_fpn_block(cfg, round(256*cfg.width) + round(512*cfg.width), round(512*cfg.width), round(3 * cfg.depth))
  29. ## P4 -> P5
  30. self.dowmsample_layer_2 = self.make_downsample_block(cfg, round(512*cfg.width), round(512*cfg.width))
  31. self.bottom_up_layer_2 = self.make_fpn_block(cfg, round(512*cfg.width) + self.in_dims[0], round(512*cfg.width*cfg.ratio), round(3 * cfg.depth))
  32. # ----------- Output projection -----------
  33. self.out_layers = nn.ModuleList([
  34. BasicConv(in_dim, round(256*cfg.width), kernel_size=1, act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  35. for in_dim in [round(256*cfg.width), round(512*cfg.width), round(512*cfg.width*cfg.ratio)]])
  36. self.out_dims = [round(256*cfg.width)] * 3
  37. self.init_weights()
  38. def init_weights(self):
  39. """Initialize the parameters."""
  40. for m in self.modules():
  41. if isinstance(m, torch.nn.Conv2d):
  42. m.reset_parameters()
  43. def make_downsample_block(self, cfg, in_dim, out_dim):
  44. if cfg.fpn_ds_block == "conv":
  45. return BasicConv(in_dim, out_dim, kernel_size=3, padding=1, stride=2,
  46. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  47. if cfg.fpn_ds_block == "dw_conv":
  48. return DWConv(in_dim, out_dim, kernel_size=3, padding=1, stride=2,
  49. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  50. if cfg.fpn_ds_block == "mdown":
  51. return MDown(in_dim, out_dim, cfg.bk_act, cfg.bk_norm, cfg.bk_depthwise)
  52. if cfg.fpn_ds_block == "adown":
  53. return ADown(in_dim, out_dim, cfg.bk_act, cfg.bk_norm, cfg.bk_depthwise)
  54. else:
  55. raise NotImplementedError("Unknown fpn downsample block: {}".format(cfg.fpn_ds_block))
  56. def make_fpn_block(self, cfg, in_dim, out_dim, block_depth):
  57. if cfg.fpn_block == "elan_layer":
  58. return ElanLayer(in_dim = in_dim,
  59. out_dim = out_dim,
  60. num_blocks = block_depth,
  61. expansion = 0.5,
  62. shortcut = False,
  63. act_type = cfg.fpn_act,
  64. norm_type = cfg.fpn_norm,
  65. depthwise = cfg.fpn_depthwise)
  66. else:
  67. raise NotImplementedError("Unknown stage block: {}".format(cfg.bk_block))
  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. # ------------------ Output projection ------------------
  86. out_feats_proj = []
  87. for feat, layer in zip(out_feats, self.out_layers):
  88. out_feats_proj.append(layer(feat))
  89. return out_feats_proj