rtcdet_pafpn.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. try:
  5. from .rtcdet_basic import (Conv, build_reduce_layer, build_downsample_layer, build_fpn_block)
  6. except:
  7. from rtcdet_basic import (Conv, build_reduce_layer, build_downsample_layer, build_fpn_block)
  8. # RTCDet-Style PaFPN
  9. class RTCDetPaFPN(nn.Module):
  10. def __init__(self, cfg, in_dims=[512, 1024, 512], out_dim=None):
  11. super(RTCDetPaFPN, self).__init__()
  12. # --------------------------- Basic Parameters ---------------------------
  13. self.in_dims = in_dims
  14. # --------------------------- Top-down FPN ---------------------------
  15. ## P5 -> P4
  16. self.reduce_layer_1 = build_reduce_layer(cfg, in_dims[2], round(256*cfg['width']))
  17. self.reduce_layer_2 = build_reduce_layer(cfg, in_dims[1], round(256*cfg['width']))
  18. self.top_down_layer_1 = build_fpn_block(cfg, round(256*cfg['width']) + round(256*cfg['width']), round(256*cfg['width']))
  19. ## P4 -> P3
  20. self.reduce_layer_3 = build_reduce_layer(cfg, round(256*cfg['width']), round(128*cfg['width']))
  21. self.reduce_layer_4 = build_reduce_layer(cfg, in_dims[0], round(128*cfg['width']))
  22. self.top_down_layer_2 = build_fpn_block(cfg, round(128*cfg['width']) + round(128*cfg['width']), round(128*cfg['width']))
  23. # --------------------------- Bottom-up FPN ---------------------------
  24. ## P3 -> P4
  25. self.downsample_layer_1 = build_downsample_layer(cfg, round(128*cfg['width']), round(256*cfg['width']))
  26. self.bottom_up_layer_1 = build_fpn_block(cfg, round(256*cfg['width']) + round(256*cfg['width']), round(256*cfg['width']))
  27. ## P4 -> P5
  28. self.downsample_layer_2 = build_downsample_layer(cfg, round(256*cfg['width']), round(512*cfg['width']))
  29. self.bottom_up_layer_2 = build_fpn_block(cfg, round(512*cfg['width']) + in_dims[2], round(512*cfg['width']))
  30. ## Head convs
  31. self.head_conv_1 = Conv(round(128*cfg['width']), round(256*cfg['width']), k=3, s=1, p=1, act_type=cfg['fpn_act'], norm_type=cfg['fpn_norm'])
  32. self.head_conv_2 = Conv(round(256*cfg['width']), round(512*cfg['width']), k=3, s=1, p=1, act_type=cfg['fpn_act'], norm_type=cfg['fpn_norm'])
  33. self.head_conv_3 = Conv(round(512*cfg['width']), round(1024*cfg['width']), k=3, s=1, p=1, act_type=cfg['fpn_act'], norm_type=cfg['fpn_norm'])
  34. # --------------------------- Output proj ---------------------------
  35. if out_dim is not None:
  36. self.out_layers = nn.ModuleList([
  37. Conv(in_dim, out_dim, k=1, act_type=cfg['fpn_act'], norm_type=cfg['fpn_norm'])
  38. for in_dim in [round(256*cfg['width']), round(512*cfg['width']), round(1024*cfg['width'])]])
  39. self.out_dim = [out_dim] * 3
  40. else:
  41. self.out_layers = None
  42. self.out_dim = [round(256*cfg['width']), round(512*cfg['width']), round(1024*cfg['width'])]
  43. def forward(self, fpn_feats):
  44. c3, c4, c5 = fpn_feats
  45. # Top down
  46. ## P5 -> P4
  47. c6 = self.reduce_layer_1(c5)
  48. c7 = F.interpolate(c6, scale_factor=2.0)
  49. c8 = torch.cat([c7, self.reduce_layer_2(c4)], dim=1)
  50. c9 = self.top_down_layer_1(c8)
  51. ## P4 -> P3
  52. c10 = self.reduce_layer_3(c9)
  53. c11 = F.interpolate(c10, scale_factor=2.0)
  54. c12 = torch.cat([c11, self.reduce_layer_4(c3)], dim=1)
  55. c13 = self.top_down_layer_2(c12)
  56. # Bottom up
  57. ## p3 -> P4
  58. c14 = self.downsample_layer_1(c13)
  59. c15 = torch.cat([c14, c9], dim=1)
  60. c16 = self.bottom_up_layer_1(c15)
  61. ## P4 -> P5
  62. c17 = self.downsample_layer_2(c16)
  63. c18 = torch.cat([c17, c5], dim=1)
  64. c19 = self.bottom_up_layer_2(c18)
  65. c20 = self.head_conv_1(c13)
  66. c21 = self.head_conv_2(c16)
  67. c22 = self.head_conv_3(c19)
  68. out_feats = [c20, c21, c22] # [P3, P4, P5]
  69. # output proj layers
  70. if self.out_layers is not None:
  71. out_feats_proj = []
  72. for feat, layer in zip(out_feats, self.out_layers):
  73. out_feats_proj.append(layer(feat))
  74. return out_feats_proj
  75. return out_feats
  76. def build_fpn(cfg, in_dims, out_dim=None):
  77. model = cfg['fpn']
  78. # build pafpn
  79. if model == 'rtcdet_pafpn':
  80. fpn_net = RTCDetPaFPN(cfg, in_dims, out_dim)
  81. return fpn_net
  82. if __name__ == '__main__':
  83. import time
  84. from thop import profile
  85. cfg = {
  86. 'width': 1.0,
  87. 'depth': 1.0,
  88. 'fpn': 'rtcdet_pafpn',
  89. 'fpn_reduce_layer': 'conv',
  90. 'fpn_downsample_layer': 'conv',
  91. 'fpn_core_block': 'elan_block',
  92. 'fpn_branch_depth': 3,
  93. 'fpn_expand_ratio': 0.5,
  94. 'fpn_act': 'silu',
  95. 'fpn_norm': 'BN',
  96. 'fpn_depthwise': False,
  97. }
  98. fpn_dims = [512, 1024, 512]
  99. out_dim = 256
  100. # Head-1
  101. model = build_fpn(cfg, fpn_dims, out_dim)
  102. fpn_feats = [torch.randn(1, fpn_dims[0], 80, 80), torch.randn(1, fpn_dims[1], 40, 40), torch.randn(1, fpn_dims[2], 20, 20)]
  103. t0 = time.time()
  104. outputs = model(fpn_feats)
  105. t1 = time.time()
  106. print('Time: ', t1 - t0)
  107. # for out in outputs:
  108. # print(out.shape)
  109. print('==============================')
  110. flops, params = profile(model, inputs=(fpn_feats, ), verbose=False)
  111. print('==============================')
  112. print('FPN: GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  113. print('FPN: Params : {:.2f} M'.format(params / 1e6))