rtcdet_v2_pafpn.py 5.0 KB

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