yolov7_af_backbone.py 4.9 KB

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