yolov10_backbone.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import torch
  2. import torch.nn as nn
  3. try:
  4. from .modules import ConvModule, C2fBlock, SCDown, SPPF, PSABlock
  5. except:
  6. from modules import ConvModule, C2fBlock, SCDown, SPPF, PSABlock
  7. # ---------------------------- Basic functions ----------------------------
  8. class Yolov10Backbone(nn.Module):
  9. def __init__(self, cfg):
  10. super(Yolov10Backbone, self).__init__()
  11. # ------------------ Basic setting ------------------
  12. self.model_scale = cfg.model_scale
  13. self.feat_dims = [round(64 * cfg.width),
  14. round(128 * cfg.width),
  15. round(256 * cfg.width),
  16. round(512 * cfg.width),
  17. round(512 * cfg.width * cfg.ratio)]
  18. # ------------------ Network setting ------------------
  19. ## P1/2
  20. self.layer_1 = ConvModule(3, self.feat_dims[0], kernel_size=3, stride=2)
  21. # P2/4
  22. self.layer_2 = nn.Sequential(
  23. ConvModule(self.feat_dims[0], self.feat_dims[1], kernel_size=3, stride=2),
  24. C2fBlock(in_dim = self.feat_dims[1],
  25. out_dim = self.feat_dims[1],
  26. num_blocks = round(3*cfg.depth),
  27. expansion = 0.5,
  28. shortcut = True,
  29. use_cib = False,
  30. )
  31. )
  32. # P3/8
  33. self.layer_3 = nn.Sequential(
  34. ConvModule(self.feat_dims[1], self.feat_dims[2], kernel_size=3, stride=2),
  35. C2fBlock(in_dim = self.feat_dims[2],
  36. out_dim = self.feat_dims[2],
  37. num_blocks = round(6*cfg.depth),
  38. expansion = 0.5,
  39. shortcut = True,
  40. use_cib = False,
  41. )
  42. )
  43. # P4/16
  44. self.layer_4 = nn.Sequential(
  45. SCDown(self.feat_dims[2], self.feat_dims[3], kernel_size=3, stride=2),
  46. C2fBlock(in_dim = self.feat_dims[3],
  47. out_dim = self.feat_dims[3],
  48. num_blocks = round(6*cfg.depth),
  49. expansion = 0.5,
  50. shortcut = True,
  51. use_cib = False,
  52. )
  53. )
  54. # P5/32
  55. self.layer_5 = nn.Sequential(
  56. SCDown(self.feat_dims[3], self.feat_dims[4], kernel_size=3, stride=2),
  57. C2fBlock(in_dim = self.feat_dims[4],
  58. out_dim = self.feat_dims[4],
  59. num_blocks = round(3*cfg.depth),
  60. expansion = 0.5,
  61. shortcut = True,
  62. use_cib = True if self.model_scale in "smlx" else False,
  63. )
  64. )
  65. # Extra module (no pretrained weight)
  66. self.layer_6 = SPPF(in_dim = int(512 * cfg.width * cfg.ratio),
  67. out_dim = int(512 * cfg.width * cfg.ratio),
  68. )
  69. self.layer_7 = PSABlock(in_dim = int(512 * cfg.width * cfg.ratio),
  70. out_dim = int(512 * cfg.width * cfg.ratio),
  71. expansion = 0.5,
  72. )
  73. # Initialize all layers
  74. self.init_weights()
  75. def init_weights(self):
  76. """Initialize the parameters."""
  77. for m in self.modules():
  78. if isinstance(m, torch.nn.Conv2d):
  79. m.reset_parameters()
  80. def forward(self, x):
  81. c1 = self.layer_1(x)
  82. c2 = self.layer_2(c1)
  83. c3 = self.layer_3(c2)
  84. c4 = self.layer_4(c3)
  85. c5 = self.layer_5(c4)
  86. c5 = self.layer_6(c5)
  87. c5 = self.layer_7(c5)
  88. outputs = [c3, c4, c5]
  89. return outputs
  90. if __name__ == '__main__':
  91. import time
  92. from thop import profile
  93. class BaseConfig(object):
  94. def __init__(self) -> None:
  95. self.width = 0.25
  96. self.depth = 0.34
  97. self.ratio = 2.0
  98. self.model_scale = "n"
  99. self.width = 0.50
  100. self.depth = 0.34
  101. self.ratio = 2.0
  102. self.model_scale = "s"
  103. self.width = 0.75
  104. self.depth = 0.67
  105. self.ratio = 1.5
  106. self.model_scale = "m"
  107. self.width = 1.0
  108. self.depth = 1.0
  109. self.ratio = 1.0
  110. self.model_scale = "l"
  111. cfg = BaseConfig()
  112. model = Yolov10Backbone(cfg)
  113. x = torch.randn(1, 3, 640, 640)
  114. t0 = time.time()
  115. outputs = model(x)
  116. t1 = time.time()
  117. print('Time: ', t1 - t0)
  118. for out in outputs:
  119. print(out.shape)
  120. x = torch.randn(1, 3, 640, 640)
  121. print('==============================')
  122. flops, params = profile(model, inputs=(x, ), verbose=False)
  123. print('==============================')
  124. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  125. print('Params : {:.2f} M'.format(params / 1e6))