yolov6_pafpn.py 5.4 KB

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