yolov7_backbone.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import torch
  2. import torch.nn as nn
  3. try:
  4. from .yolov7_basic import BasicConv, MDown, ELANLayer
  5. except:
  6. from yolov7_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 = BasicConv(3, self.feat_dims[0],
  26. kernel_size=6, padding=2, stride=2,
  27. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise)
  28. self.layer_2 = nn.Sequential(
  29. BasicConv(self.feat_dims[0], self.feat_dims[1],
  30. kernel_size=3, padding=1, stride=2,
  31. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise),
  32. ELANLayer(self.feat_dims[1], self.feat_dims[2],
  33. expansion=0.5, num_blocks=self.elan_depth,
  34. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise),
  35. )
  36. self.layer_3 = nn.Sequential(
  37. MDown(self.feat_dims[2], self.feat_dims[2],
  38. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise),
  39. ELANLayer(self.feat_dims[2], self.feat_dims[3],
  40. expansion=0.5, num_blocks=self.elan_depth,
  41. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise),
  42. )
  43. self.layer_4 = nn.Sequential(
  44. MDown(self.feat_dims[3], self.feat_dims[3],
  45. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise),
  46. ELANLayer(self.feat_dims[3], self.feat_dims[4],
  47. expansion=0.5, num_blocks=self.elan_depth,
  48. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise),
  49. )
  50. self.layer_5 = nn.Sequential(
  51. MDown(self.feat_dims[4], self.feat_dims[4],
  52. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise),
  53. ELANLayer(self.feat_dims[4], self.feat_dims[5],
  54. expansion=self.last_stage_eratio, num_blocks=self.elan_depth,
  55. act_type=cfg.bk_act, norm_type=cfg.bk_norm, depthwise=cfg.bk_depthwise),
  56. )
  57. # Initialize all layers
  58. self.init_weights()
  59. def init_weights(self):
  60. """Initialize the parameters."""
  61. for m in self.modules():
  62. if isinstance(m, torch.nn.Conv2d):
  63. # In order to be consistent with the source code,
  64. # reset the Conv2d initialization parameters
  65. m.reset_parameters()
  66. def forward(self, x):
  67. c1 = self.layer_1(x)
  68. c2 = self.layer_2(c1)
  69. c3 = self.layer_3(c2)
  70. c4 = self.layer_4(c3)
  71. c5 = self.layer_5(c4)
  72. outputs = [c3, c4, c5]
  73. return outputs
  74. if __name__ == '__main__':
  75. import time
  76. from thop import profile
  77. class BaseConfig(object):
  78. def __init__(self) -> None:
  79. self.bk_act = 'silu'
  80. self.bk_norm = 'BN'
  81. self.bk_depthwise = False
  82. self.width = 0.5
  83. self.depth = 0.34
  84. self.scale = "s"
  85. cfg = BaseConfig()
  86. model = Yolov7Backbone(cfg)
  87. x = torch.randn(1, 3, 640, 640)
  88. t0 = time.time()
  89. outputs = model(x)
  90. t1 = time.time()
  91. print('Time: ', t1 - t0)
  92. for out in outputs:
  93. print(out.shape)
  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))