rtcdet_backbone.py 5.2 KB

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