yolov8_backbone.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. self.init_weights()
  60. def init_weights(self):
  61. """Initialize the parameters."""
  62. for m in self.modules():
  63. if isinstance(m, torch.nn.Conv2d):
  64. # In order to be consistent with the source code,
  65. # reset the Conv2d initialization parameters
  66. m.reset_parameters()
  67. def forward(self, x):
  68. c1 = self.layer_1(x)
  69. c2 = self.layer_2(c1)
  70. c3 = self.layer_3(c2)
  71. c4 = self.layer_4(c3)
  72. c5 = self.layer_5(c4)
  73. outputs = [c3, c4, c5]
  74. return outputs
  75. # ---------------------------- Functions ----------------------------
  76. ## build Yolov8's Backbone
  77. def build_backbone(cfg):
  78. # model
  79. backbone = Yolov8Backbone(width=cfg['width'],
  80. depth=cfg['depth'],
  81. ratio=cfg['ratio'],
  82. act_type=cfg['bk_act'],
  83. norm_type=cfg['bk_norm'],
  84. depthwise=cfg['bk_depthwise']
  85. )
  86. feat_dims = backbone.feat_dims[-3:]
  87. return backbone, feat_dims
  88. if __name__ == '__main__':
  89. import time
  90. from thop import profile
  91. cfg = {
  92. 'bk_act': 'silu',
  93. 'bk_norm': 'BN',
  94. 'bk_depthwise': False,
  95. 'width': 0.25,
  96. 'depth': 0.34,
  97. 'ratio': 2.0,
  98. }
  99. model, feats = build_backbone(cfg)
  100. x = torch.randn(1, 3, 640, 640)
  101. t0 = time.time()
  102. outputs = model(x)
  103. t1 = time.time()
  104. print('Time: ', t1 - t0)
  105. for out in outputs:
  106. print(out.shape)
  107. x = torch.randn(1, 3, 640, 640)
  108. print('==============================')
  109. flops, params = profile(model, inputs=(x, ), verbose=False)
  110. print('==============================')
  111. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  112. print('Params : {:.2f} M'.format(params / 1e6))