yolov6_backbone.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import torch
  2. import torch.nn as nn
  3. try:
  4. from .yolov6_basic import RepBlock, RepVGGBlock
  5. except:
  6. from yolov6_basic import RepBlock, RepVGGBlock
  7. # --------------------- Yolov3's Backbone -----------------------
  8. ## Modified DarkNet
  9. class Yolov6Backbone(nn.Module):
  10. def __init__(self, cfg):
  11. super(Yolov6Backbone, self).__init__()
  12. # ------------------ Basic setting ------------------
  13. self.model_scale = cfg.scale
  14. self.feat_dims = [round(64 * cfg.width),
  15. round(128 * cfg.width),
  16. round(256 * cfg.width),
  17. round(512 * cfg.width),
  18. round(1024 * cfg.width)]
  19. # ------------------ Network setting ------------------
  20. ## P1/2
  21. self.layer_1 = RepVGGBlock(3, self.feat_dims[0],
  22. kernel_size=3, padding=1, stride=2)
  23. # P2/4
  24. self.layer_2 = nn.Sequential(
  25. RepVGGBlock(self.feat_dims[0], self.feat_dims[1],
  26. kernel_size=3, padding=1, stride=2),
  27. RepBlock(in_channels = self.feat_dims[1],
  28. out_channels = self.feat_dims[1],
  29. num_blocks = round(6*cfg.depth))
  30. )
  31. # P3/8
  32. self.layer_3 = nn.Sequential(
  33. RepVGGBlock(self.feat_dims[1], self.feat_dims[2],
  34. kernel_size=3, padding=1, stride=2),
  35. RepBlock(in_channels = self.feat_dims[2],
  36. out_channels = self.feat_dims[2],
  37. num_blocks = round(12*cfg.depth))
  38. )
  39. # P4/16
  40. self.layer_4 = nn.Sequential(
  41. RepVGGBlock(self.feat_dims[2], self.feat_dims[3],
  42. kernel_size=3, padding=1, stride=2),
  43. RepBlock(in_channels = self.feat_dims[3],
  44. out_channels = self.feat_dims[3],
  45. num_blocks = round(18*cfg.depth))
  46. )
  47. # P5/32
  48. self.layer_5 = nn.Sequential(
  49. RepVGGBlock(self.feat_dims[3], self.feat_dims[4],
  50. kernel_size=3, padding=1, stride=2),
  51. RepBlock(in_channels = self.feat_dims[4],
  52. out_channels = self.feat_dims[4],
  53. num_blocks = round(6*cfg.depth))
  54. )
  55. # Initialize all layers
  56. self.init_weights()
  57. def init_weights(self):
  58. """Initialize the parameters."""
  59. for m in self.modules():
  60. if isinstance(m, torch.nn.Conv2d):
  61. # In order to be consistent with the source code,
  62. # reset the Conv2d initialization parameters
  63. m.reset_parameters()
  64. def forward(self, x):
  65. c1 = self.layer_1(x)
  66. c2 = self.layer_2(c1)
  67. c3 = self.layer_3(c2)
  68. c4 = self.layer_4(c3)
  69. c5 = self.layer_5(c4)
  70. outputs = [c3, c4, c5]
  71. return outputs
  72. if __name__ == '__main__':
  73. import time
  74. from thop import profile
  75. class BaseConfig(object):
  76. def __init__(self) -> None:
  77. self.bk_depthwise = False
  78. self.width = 0.50
  79. self.depth = 0.34
  80. self.scale = "s"
  81. self.use_pretrained = True
  82. cfg = BaseConfig()
  83. model = Yolov6Backbone(cfg)
  84. x = torch.randn(1, 3, 640, 640)
  85. t0 = time.time()
  86. outputs = model(x)
  87. t1 = time.time()
  88. print('Time: ', t1 - t0)
  89. for out in outputs:
  90. print(out.shape)
  91. for m in model.modules():
  92. if hasattr(m, "switch_to_deploy"):
  93. m.switch_to_deploy()
  94. x = torch.randn(1, 3, 640, 640)
  95. print('==============================')
  96. flops, params = profile(model, inputs=(x, ), verbose=False)
  97. print('==============================')
  98. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  99. print('Params : {:.2f} M'.format(params / 1e6))