yolox_fpn.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. try:
  5. from .yolox_basic import Conv, CSPBlock
  6. except:
  7. from yolox_basic import Conv, CSPBlock
  8. # PaFPN-CSP
  9. class YoloPaFPN(nn.Module):
  10. def __init__(self,
  11. in_dims=[256, 512, 1024],
  12. out_dim=256,
  13. width=1.0,
  14. depth=1.0,
  15. act_type='silu',
  16. norm_type='BN',
  17. depthwise=False):
  18. super(YoloPaFPN, self).__init__()
  19. self.in_dims = in_dims
  20. self.out_dim = out_dim
  21. c3, c4, c5 = in_dims
  22. # top dwon
  23. ## P5 -> P4
  24. self.reduce_layer_1 = Conv(c5, int(512*width), k=1, norm_type=norm_type, act_type=act_type)
  25. self.top_down_layer_1 = CSPBlock(c4 + int(512*width),
  26. int(512*width),
  27. expand_ratio=0.5,
  28. kernel=[1, 3],
  29. nblocks=int(3*depth),
  30. shortcut=False,
  31. act_type=act_type,
  32. norm_type=norm_type,
  33. depthwise=depthwise
  34. )
  35. ## P4 -> P3
  36. self.reduce_layer_2 = Conv(c4, int(256*width), k=1, norm_type=norm_type, act_type=act_type) # 14
  37. self.top_down_layer_2 = CSPBlock(c3 + int(256*width),
  38. int(256*width),
  39. expand_ratio=0.5,
  40. kernel=[1, 3],
  41. nblocks=int(3*depth),
  42. shortcut=False,
  43. act_type=act_type,
  44. norm_type=norm_type,
  45. depthwise=depthwise
  46. )
  47. # bottom up
  48. ## P3 -> P4
  49. self.reduce_layer_3 = Conv(int(256*width), int(256*width), k=3, p=1, s=2,
  50. act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  51. self.bottom_up_layer_1 = CSPBlock(int(256*width) + int(256*width),
  52. int(512*width),
  53. expand_ratio=0.5,
  54. kernel=[1, 3],
  55. nblocks=int(3*depth),
  56. shortcut=False,
  57. act_type=act_type,
  58. norm_type=norm_type,
  59. depthwise=depthwise
  60. )
  61. ## P4 -> P5
  62. self.reduce_layer_4 = Conv(int(512*width), int(512*width), k=3, p=1, s=2,
  63. act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  64. self.bottom_up_layer_2 = CSPBlock(int(512*width) + int(512*width),
  65. int(1024*width),
  66. expand_ratio=0.5,
  67. kernel=[1, 3],
  68. nblocks=int(3*depth),
  69. shortcut=False,
  70. act_type=act_type,
  71. norm_type=norm_type,
  72. depthwise=depthwise
  73. )
  74. # output proj layers
  75. if out_dim is not None:
  76. # output proj layers
  77. self.out_layers = nn.ModuleList([
  78. Conv(in_dim, out_dim, k=1,
  79. norm_type=norm_type, act_type=act_type)
  80. for in_dim in [int(256 * width), int(512 * width), int(1024 * width)]
  81. ])
  82. self.out_dim = [out_dim] * 3
  83. else:
  84. self.out_layers = None
  85. self.out_dim = [int(256 * width), int(512 * width), int(1024 * width)]
  86. def forward(self, features):
  87. c3, c4, c5 = features
  88. c6 = self.reduce_layer_1(c5)
  89. c7 = F.interpolate(c6, scale_factor=2.0) # s32->s16
  90. c8 = torch.cat([c7, c4], dim=1)
  91. c9 = self.top_down_layer_1(c8)
  92. # P3/8
  93. c10 = self.reduce_layer_2(c9)
  94. c11 = F.interpolate(c10, scale_factor=2.0) # s16->s8
  95. c12 = torch.cat([c11, c3], dim=1)
  96. c13 = self.top_down_layer_2(c12) # to det
  97. # p4/16
  98. c14 = self.reduce_layer_3(c13)
  99. c15 = torch.cat([c14, c10], dim=1)
  100. c16 = self.bottom_up_layer_1(c15) # to det
  101. # p5/32
  102. c17 = self.reduce_layer_4(c16)
  103. c18 = torch.cat([c17, c6], dim=1)
  104. c19 = self.bottom_up_layer_2(c18) # to det
  105. out_feats = [c13, c16, c19] # [P3, P4, P5]
  106. # output proj layers
  107. if self.out_layers is not None:
  108. # output proj layers
  109. out_feats_proj = []
  110. for feat, layer in zip(out_feats, self.out_layers):
  111. out_feats_proj.append(layer(feat))
  112. return out_feats_proj
  113. return out_feats
  114. def build_fpn(cfg, in_dims, out_dim=None):
  115. model = cfg['fpn']
  116. # build neck
  117. if model == 'yolo_pafpn':
  118. fpn_net = YoloPaFPN(in_dims=in_dims,
  119. out_dim=out_dim,
  120. width=cfg['width'],
  121. depth=cfg['depth'],
  122. act_type=cfg['fpn_act'],
  123. norm_type=cfg['fpn_norm'],
  124. depthwise=cfg['fpn_depthwise']
  125. )
  126. return fpn_net