yolox_pafpn.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. try:
  5. from .yolox_basic import (Conv, build_reduce_layer, build_downsample_layer, build_fpn_block)
  6. except:
  7. from yolox_basic import (Conv, build_reduce_layer, build_downsample_layer, build_fpn_block)
  8. # YOLO-Style PaFPN
  9. class YoloxPaFPN(nn.Module):
  10. def __init__(self, cfg, in_dims=[256, 512, 1024], out_dim=None):
  11. super(YoloxPaFPN, self).__init__()
  12. # --------------------------- Basic Parameters ---------------------------
  13. self.in_dims = in_dims
  14. c3, c4, c5 = in_dims
  15. width = cfg['width']
  16. # --------------------------- Network Parameters ---------------------------
  17. ## top dwon
  18. ### P5 -> P4
  19. self.reduce_layer_1 = build_reduce_layer(cfg, c5, round(512*width))
  20. self.top_down_layer_1 = build_fpn_block(cfg, c4 + round(512*width), round(512*width))
  21. ### P4 -> P3
  22. self.reduce_layer_2 = build_reduce_layer(cfg, round(512*width), round(256*width))
  23. self.top_down_layer_2 = build_fpn_block(cfg, c3 + round(256*width), round(256*width))
  24. ## bottom up
  25. ### P3 -> P4
  26. self.reduce_layer_3 = build_downsample_layer(cfg, round(256*width), round(256*width))
  27. self.bottom_up_layer_1 = build_fpn_block(cfg, round(256*width) + round(256*width), round(512*width))
  28. ### P4 -> P5
  29. self.reduce_layer_4 = build_downsample_layer(cfg, round(512*width), round(512*width))
  30. self.bottom_up_layer_2 = build_fpn_block(cfg, round(512*width) + round(512*width), round(1024*width))
  31. ## output proj layers
  32. if out_dim is not None:
  33. self.out_layers = nn.ModuleList([
  34. Conv(in_dim, out_dim, k=1,
  35. act_type=cfg['fpn_act'], norm_type=cfg['fpn_norm'])
  36. for in_dim in [round(256*width), round(512*width), round(1024*width)]
  37. ])
  38. self.out_dim = [out_dim] * 3
  39. else:
  40. self.out_layers = None
  41. self.out_dim = [round(256*width), round(512*width), round(1024*width)]
  42. def forward(self, features):
  43. c3, c4, c5 = features
  44. # Top down
  45. ## P5 -> P4
  46. c6 = self.reduce_layer_1(c5)
  47. c7 = F.interpolate(c6, scale_factor=2.0)
  48. c8 = torch.cat([c7, c4], dim=1)
  49. c9 = self.top_down_layer_1(c8)
  50. ## P4 -> P3
  51. c10 = self.reduce_layer_2(c9)
  52. c11 = F.interpolate(c10, scale_factor=2.0)
  53. c12 = torch.cat([c11, c3], dim=1)
  54. c13 = self.top_down_layer_2(c12)
  55. # Bottom up
  56. ## p3 -> P4
  57. c14 = self.reduce_layer_3(c13)
  58. c15 = torch.cat([c14, c10], dim=1)
  59. c16 = self.bottom_up_layer_1(c15)
  60. ## P4 -> P5
  61. c17 = self.reduce_layer_4(c16)
  62. c18 = torch.cat([c17, c6], dim=1)
  63. c19 = self.bottom_up_layer_2(c18)
  64. out_feats = [c13, c16, c19] # [P3, P4, P5]
  65. # output proj layers
  66. if self.out_layers is not None:
  67. out_feats_proj = []
  68. for feat, layer in zip(out_feats, self.out_layers):
  69. out_feats_proj.append(layer(feat))
  70. return out_feats_proj
  71. return out_feats
  72. def build_fpn(cfg, in_dims, out_dim=None):
  73. model = cfg['fpn']
  74. # build pafpn
  75. if model == 'yolox_pafpn':
  76. fpn_net = YoloxPaFPN(cfg, in_dims, out_dim)
  77. return fpn_net
  78. if __name__ == '__main__':
  79. import time
  80. from thop import profile
  81. cfg = {
  82. 'fpn': 'yolox_pafpn',
  83. 'fpn_reduce_layer': 'conv',
  84. 'fpn_downsample_layer': 'conv',
  85. 'fpn_core_block': 'cspblock',
  86. 'fpn_act': 'silu',
  87. 'fpn_norm': 'BN',
  88. 'fpn_depthwise': False,
  89. 'width': 1.0,
  90. 'depth': 1.0,
  91. }
  92. model = build_fpn(cfg, in_dims=[256, 512, 1024], out_dim=256)
  93. pyramid_feats = [torch.randn(1, 256, 80, 80), torch.randn(1, 512, 40, 40), torch.randn(1, 1024, 20, 20)]
  94. t0 = time.time()
  95. outputs = model(pyramid_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=(pyramid_feats, ), verbose=False)
  102. print('==============================')
  103. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  104. print('Params : {:.2f} M'.format(params / 1e6))