yolov3_fpn.py 4.8 KB

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