rtcdet_backbone.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.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 = BasicConv(3, self.feat_dims[0],
  21. kernel_size=6, padding=2, stride=2,
  22. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise)
  23. # P2/4
  24. self.layer_2 = nn.Sequential(
  25. BasicConv(self.feat_dims[0], self.feat_dims[1],
  26. kernel_size=3, padding=1, stride=2,
  27. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise),
  28. ELANLayer(in_dim = self.feat_dims[1],
  29. out_dim = self.feat_dims[1],
  30. num_blocks = round(3*cfg.depth),
  31. expansion = 0.5,
  32. shortcut = True,
  33. act_type = cfg.bk_act,
  34. norm_type = cfg.bk_norm,
  35. depthwise = cfg.bk_depthwise)
  36. )
  37. # P3/8
  38. self.layer_3 = nn.Sequential(
  39. MDown(self.feat_dims[1], self.feat_dims[2],
  40. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise),
  41. ELANLayer(in_dim = self.feat_dims[2],
  42. out_dim = self.feat_dims[2],
  43. num_blocks = round(6*cfg.depth),
  44. expansion = 0.5,
  45. shortcut = True,
  46. act_type = cfg.bk_act,
  47. norm_type = cfg.bk_norm,
  48. depthwise = cfg.bk_depthwise)
  49. )
  50. # P4/16
  51. self.layer_4 = nn.Sequential(
  52. MDown(self.feat_dims[2], self.feat_dims[3],
  53. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise),
  54. ELANLayer(in_dim = self.feat_dims[3],
  55. out_dim = self.feat_dims[3],
  56. num_blocks = round(6*cfg.depth),
  57. expansion = 0.5,
  58. shortcut = True,
  59. act_type = cfg.bk_act,
  60. norm_type = cfg.bk_norm,
  61. depthwise = cfg.bk_depthwise)
  62. )
  63. # P5/32
  64. self.layer_5 = nn.Sequential(
  65. MDown(self.feat_dims[3], self.feat_dims[4],
  66. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise),
  67. ELANLayer(in_dim = self.feat_dims[4],
  68. out_dim = self.feat_dims[4],
  69. num_blocks = round(3*cfg.depth),
  70. expansion = 0.5,
  71. shortcut = True,
  72. act_type = cfg.bk_act,
  73. norm_type = cfg.bk_norm,
  74. depthwise = cfg.bk_depthwise)
  75. )
  76. # Initialize all layers
  77. self.init_weights()
  78. def init_weights(self):
  79. """Initialize the parameters."""
  80. for m in self.modules():
  81. if isinstance(m, torch.nn.Conv2d):
  82. # In order to be consistent with the source code,
  83. # reset the Conv2d initialization parameters
  84. m.reset_parameters()
  85. def forward(self, x):
  86. c1 = self.layer_1(x)
  87. c2 = self.layer_2(c1)
  88. c3 = self.layer_3(c2)
  89. c4 = self.layer_4(c3)
  90. c5 = self.layer_5(c4)
  91. outputs = [c3, c4, c5]
  92. return outputs
  93. # ---------------------------- Functions ----------------------------
  94. ## build Yolo's Backbone
  95. def build_backbone(cfg):
  96. # model
  97. backbone = RTCBackbone(cfg)
  98. return backbone
  99. if __name__ == '__main__':
  100. import time
  101. from thop import profile
  102. class BaseConfig(object):
  103. def __init__(self) -> None:
  104. self.bk_act = 'silu'
  105. self.bk_norm = 'BN'
  106. self.bk_depthwise = False
  107. self.width = 1.0
  108. self.depth = 1.0
  109. self.ratio = 1.0
  110. self.scale = "n"
  111. cfg = BaseConfig()
  112. model = build_backbone(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))