yolov7_backbone.py 4.5 KB

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