yolov8_backbone.py 4.4 KB

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