yolov4_pafpn.py 5.5 KB

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