yolov5_pafpn.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from typing import List
  2. import torch
  3. import torch.nn as nn
  4. import torch.nn.functional as F
  5. try:
  6. from .yolov5_basic import BasicConv, CSPBlock
  7. except:
  8. from yolov5_basic import BasicConv, CSPBlock
  9. # Yolov5FPN
  10. class Yolov5PaFPN(nn.Module):
  11. def __init__(self, cfg, in_dims: List = [256, 512, 1024]):
  12. super(Yolov5PaFPN, self).__init__()
  13. self.in_dims = in_dims
  14. c3, c4, c5 = in_dims
  15. # ---------------------- Yolov5's Top down FPN ----------------------
  16. ## P5 -> P4
  17. self.reduce_layer_1 = BasicConv(c5, round(512*cfg.width),
  18. kernel_size=1, padding=0, stride=1,
  19. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  20. self.top_down_layer_1 = CSPBlock(in_dim = c4 + round(512*cfg.width),
  21. out_dim = round(512*cfg.width),
  22. num_blocks = round(3*cfg.depth),
  23. expansion = 0.5,
  24. shortcut = False,
  25. act_type = cfg.fpn_act,
  26. norm_type = cfg.fpn_norm,
  27. depthwise = cfg.fpn_depthwise)
  28. ## P4 -> P3
  29. self.reduce_layer_2 = BasicConv(round(512*cfg.width), round(256*cfg.width),
  30. kernel_size=1, padding=0, stride=1,
  31. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  32. self.top_down_layer_2 = CSPBlock(in_dim = c3 + round(256*cfg.width),
  33. out_dim = round(256*cfg.width),
  34. num_blocks = round(3*cfg.depth),
  35. expansion = 0.5,
  36. shortcut = False,
  37. act_type = cfg.fpn_act,
  38. norm_type = cfg.fpn_norm,
  39. depthwise = cfg.fpn_depthwise)
  40. # ---------------------- Yolov5's Bottom up PAN ----------------------
  41. ## P3 -> P4
  42. self.downsample_layer_1 = BasicConv(round(256*cfg.width), round(256*cfg.width),
  43. kernel_size=3, padding=1, stride=2,
  44. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  45. self.bottom_up_layer_1 = CSPBlock(in_dim = round(256*cfg.width) + round(256*cfg.width),
  46. out_dim = round(512*cfg.width),
  47. num_blocks = round(3*cfg.depth),
  48. expansion = 0.5,
  49. shortcut = False,
  50. act_type = cfg.fpn_act,
  51. norm_type = cfg.fpn_norm,
  52. depthwise = cfg.fpn_depthwise)
  53. ## P4 -> P5
  54. self.downsample_layer_2 = BasicConv(round(512*cfg.width), round(512*cfg.width),
  55. kernel_size=3, padding=1, stride=2,
  56. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  57. self.bottom_up_layer_2 = CSPBlock(in_dim = round(512*cfg.width) + round(512*cfg.width),
  58. out_dim = round(1024*cfg.width),
  59. num_blocks = round(3*cfg.depth),
  60. expansion = 0.5,
  61. shortcut = False,
  62. act_type = cfg.fpn_act,
  63. norm_type = cfg.fpn_norm,
  64. depthwise = cfg.fpn_depthwise)
  65. # ---------------------- Yolov5's output projection ----------------------
  66. self.out_layers = nn.ModuleList([
  67. BasicConv(in_dim, round(cfg.head_dim*cfg.width), kernel_size=1,
  68. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  69. for in_dim in [round(256*cfg.width), round(512*cfg.width), round(1024*cfg.width)]
  70. ])
  71. self.out_dims = [round(cfg.head_dim*cfg.width)] * 3
  72. def forward(self, features):
  73. c3, c4, c5 = features
  74. # ------------------ Top down FPN ------------------
  75. ## P5 -> P4
  76. p5 = self.reduce_layer_1(c5)
  77. p5_up = F.interpolate(p5, scale_factor=2.0)
  78. p4 = self.top_down_layer_1(torch.cat([c4, p5_up], dim=1))
  79. ## P4 -> P3
  80. p4 = self.reduce_layer_2(p4)
  81. p4_up = F.interpolate(p4, scale_factor=2.0)
  82. p3 = self.top_down_layer_2(torch.cat([c3, p4_up], dim=1))
  83. # ------------------ Bottom up PAN ------------------
  84. ## P3 -> P4
  85. p3_ds = self.downsample_layer_1(p3)
  86. p4 = self.bottom_up_layer_1(torch.cat([p4, p3_ds], dim=1))
  87. ## P4 -> P5
  88. p4_ds = self.downsample_layer_2(p4)
  89. p5 = self.bottom_up_layer_2(torch.cat([p5, p4_ds], dim=1))
  90. out_feats = [p3, p4, p5]
  91. # output proj layers
  92. out_feats_proj = []
  93. for feat, layer in zip(out_feats, self.out_layers):
  94. out_feats_proj.append(layer(feat))
  95. return out_feats_proj
  96. if __name__=='__main__':
  97. import time
  98. from thop import profile
  99. # Model config
  100. # YOLOv5-Base config
  101. class Yolov5BaseConfig(object):
  102. def __init__(self) -> None:
  103. # ---------------- Model config ----------------
  104. self.width = 0.50
  105. self.depth = 0.34
  106. self.out_stride = [8, 16, 32]
  107. self.max_stride = 32
  108. self.num_levels = 3
  109. ## FPN
  110. self.fpn_act = 'silu'
  111. self.fpn_norm = 'BN'
  112. self.fpn_depthwise = False
  113. ## Head
  114. self.head_dim = 256
  115. cfg = Yolov5BaseConfig()
  116. # Build a head
  117. in_dims = [128, 256, 512]
  118. fpn = Yolov5PaFPN(cfg, in_dims)
  119. # Inference
  120. x = [torch.randn(1, in_dims[0], 80, 80),
  121. torch.randn(1, in_dims[1], 40, 40),
  122. torch.randn(1, in_dims[2], 20, 20)]
  123. t0 = time.time()
  124. output = fpn(x)
  125. t1 = time.time()
  126. print('Time: ', t1 - t0)
  127. print('====== FPN output ====== ')
  128. for level, feat in enumerate(output):
  129. print("- Level-{} : ".format(level), feat.shape)
  130. flops, params = profile(fpn, inputs=(x, ), verbose=False)
  131. print('==============================')
  132. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  133. print('Params : {:.2f} M'.format(params / 1e6))