gelan_backbone.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import torch
  2. import torch.nn as nn
  3. try:
  4. from .gelan_basic import BasicConv, RepGElanLayer, ADown
  5. except:
  6. from gelan_basic import BasicConv, RepGElanLayer, ADown
  7. # ---------------------------- Basic functions ----------------------------
  8. class GElanBackbone(nn.Module):
  9. def __init__(self, cfg):
  10. super(GElanBackbone, self).__init__()
  11. # ------------------ Basic setting ------------------
  12. self.feat_dims = [cfg.backbone_feats["c1"][-1], # 64
  13. cfg.backbone_feats["c2"][-1], # 128
  14. cfg.backbone_feats["c3"][-1], # 256
  15. cfg.backbone_feats["c4"][-1], # 512
  16. cfg.backbone_feats["c5"][-1], # 512
  17. ]
  18. # ------------------ Network setting ------------------
  19. ## P1/2
  20. self.layer_1 = BasicConv(3, cfg.backbone_feats["c1"][0],
  21. kernel_size=3, padding=1, 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(cfg.backbone_feats["c1"][0], cfg.backbone_feats["c2"][0],
  26. kernel_size=3, padding=1, stride=2,
  27. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise),
  28. RepGElanLayer(in_dim = cfg.backbone_feats["c2"][0],
  29. inter_dims = cfg.backbone_feats["c2"][1],
  30. out_dim = cfg.backbone_feats["c2"][2],
  31. num_blocks = cfg.backbone_depth,
  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. ADown(cfg.backbone_feats["c2"][2], cfg.backbone_feats["c3"][0],
  40. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise, use_pooling=cfg.bk_down_pooling),
  41. RepGElanLayer(in_dim = cfg.backbone_feats["c3"][0],
  42. inter_dims = cfg.backbone_feats["c3"][1],
  43. out_dim = cfg.backbone_feats["c3"][2],
  44. num_blocks = cfg.backbone_depth,
  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. ADown(cfg.backbone_feats["c3"][2], cfg.backbone_feats["c4"][0],
  53. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise, use_pooling=cfg.bk_down_pooling),
  54. RepGElanLayer(in_dim = cfg.backbone_feats["c4"][0],
  55. inter_dims = cfg.backbone_feats["c4"][1],
  56. out_dim = cfg.backbone_feats["c4"][2],
  57. num_blocks = cfg.backbone_depth,
  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. ADown(cfg.backbone_feats["c4"][2], cfg.backbone_feats["c5"][0],
  66. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise, use_pooling=cfg.bk_down_pooling),
  67. RepGElanLayer(in_dim = cfg.backbone_feats["c5"][0],
  68. inter_dims = cfg.backbone_feats["c5"][1],
  69. out_dim = cfg.backbone_feats["c5"][2],
  70. num_blocks = cfg.backbone_depth,
  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 = GElanBackbone(cfg)
  98. return backbone
  99. if __name__ == '__main__':
  100. import time
  101. from thop import profile
  102. base_config = {
  103. "bk_act": "silu",
  104. "bk_norm": "BN"
  105. }
  106. class BaseConfig(object):
  107. def __init__(self) -> None:
  108. self.bk_act = 'silu'
  109. self.bk_norm = 'BN'
  110. self.bk_depthwise = False
  111. self.backbone_feats = {
  112. "c1": [64],
  113. "c2": [128, [128, 64], 256],
  114. "c3": [256, [256, 128], 512],
  115. "c4": [512, [512, 256], 512],
  116. "c5": [512, [512, 256], 512],
  117. }
  118. self.backbone_depth = 1
  119. cfg = BaseConfig()
  120. model = build_backbone(cfg)
  121. x = torch.randn(1, 3, 640, 640)
  122. t0 = time.time()
  123. outputs = model(x)
  124. t1 = time.time()
  125. print('Time: ', t1 - t0)
  126. for out in outputs:
  127. print(out.shape)
  128. x = torch.randn(1, 3, 640, 640)
  129. print('==============================')
  130. flops, params = profile(model, inputs=(x, ), verbose=False)
  131. print('==============================')
  132. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  133. print('Params : {:.2f} M'.format(params / 1e6))