rtcdet_v2_pafpn.py 4.4 KB

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