yolov5_backbone.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import torch
  2. import torch.nn as nn
  3. try:
  4. from .yolov5_basic import Conv, CSPBlock
  5. from .yolov5_neck import SPPF
  6. except:
  7. from yolov5_basic import Conv, CSPBlock
  8. from yolov5_neck import SPPF
  9. # CSPDarkNet
  10. class CSPDarkNet(nn.Module):
  11. def __init__(self, depth=1.0, width=1.0, act_type='silu', norm_type='BN', depthwise=False):
  12. super(CSPDarkNet, self).__init__()
  13. self.feat_dims = [round(64 * width), round(128 * width), round(256 * width), round(512 * width), round(1024 * width)]
  14. # P1/2
  15. self.layer_1 = Conv(3, self.feat_dims[0], k=6, p=2, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  16. # P2/4
  17. self.layer_2 = nn.Sequential(
  18. Conv(self.feat_dims[0], self.feat_dims[1], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise),
  19. CSPBlock(in_dim = self.feat_dims[1],
  20. out_dim = self.feat_dims[1],
  21. expand_ratio = 0.5,
  22. nblocks = round(3*depth),
  23. shortcut = True,
  24. act_type = act_type,
  25. norm_type = norm_type,
  26. depthwise = depthwise)
  27. )
  28. # P3/8
  29. self.layer_3 = nn.Sequential(
  30. Conv(self.feat_dims[1], self.feat_dims[2], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise),
  31. CSPBlock(in_dim = self.feat_dims[2],
  32. out_dim = self.feat_dims[2],
  33. expand_ratio = 0.5,
  34. nblocks = round(9*depth),
  35. shortcut = True,
  36. act_type = act_type,
  37. norm_type = norm_type,
  38. depthwise = depthwise)
  39. )
  40. # P4/16
  41. self.layer_4 = nn.Sequential(
  42. Conv(self.feat_dims[2], self.feat_dims[3], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise),
  43. CSPBlock(in_dim = self.feat_dims[3],
  44. out_dim = self.feat_dims[3],
  45. expand_ratio = 0.5,
  46. nblocks = round(9*depth),
  47. shortcut = True,
  48. act_type = act_type,
  49. norm_type = norm_type,
  50. depthwise = depthwise)
  51. )
  52. # P5/32
  53. self.layer_5 = nn.Sequential(
  54. Conv(self.feat_dims[3], self.feat_dims[4], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise),
  55. SPPF(self.feat_dims[4], self.feat_dims[4], expand_ratio=0.5),
  56. CSPBlock(in_dim = self.feat_dims[4],
  57. out_dim = self.feat_dims[4],
  58. expand_ratio = 0.5,
  59. nblocks = round(3*depth),
  60. shortcut = True,
  61. act_type = act_type,
  62. norm_type = norm_type,
  63. depthwise = depthwise)
  64. )
  65. def forward(self, x):
  66. c1 = self.layer_1(x)
  67. c2 = self.layer_2(c1)
  68. c3 = self.layer_3(c2)
  69. c4 = self.layer_4(c3)
  70. c5 = self.layer_5(c4)
  71. outputs = [c3, c4, c5]
  72. return outputs
  73. # ---------------------------- Functions ----------------------------
  74. ## build CSPDarkNet
  75. def build_backbone(cfg):
  76. backbone = CSPDarkNet(cfg['depth'], cfg['width'], cfg['bk_act'], cfg['bk_norm'], cfg['bk_dpw'])
  77. feat_dims = backbone.feat_dims[-3:]
  78. return backbone, feat_dims
  79. if __name__ == '__main__':
  80. import time
  81. from thop import profile
  82. cfg = {
  83. 'pretrained': False,
  84. 'bk_act': 'lrelu',
  85. 'bk_norm': 'BN',
  86. 'bk_dpw': False,
  87. 'p6_feat': False,
  88. 'p7_feat': False,
  89. 'width': 1.0,
  90. 'depth': 1.0,
  91. }
  92. model, feats = build_backbone(cfg)
  93. x = torch.randn(1, 3, 224, 224)
  94. t0 = time.time()
  95. outputs = model(x)
  96. t1 = time.time()
  97. print('Time: ', t1 - t0)
  98. for out in outputs:
  99. print(out.shape)
  100. x = torch.randn(1, 3, 224, 224)
  101. print('==============================')
  102. flops, params = profile(model, inputs=(x, ), verbose=False)
  103. print('==============================')
  104. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  105. print('Params : {:.2f} M'.format(params / 1e6))