yolox2_backbone.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import torch
  2. import torch.nn as nn
  3. try:
  4. from .yolox2_basic import Conv, Yolox2StageBlock
  5. except:
  6. from yolox2_basic import Conv, Yolox2StageBlock
  7. # ---------------------------- Backbone ----------------------------
  8. class Yolox2Backbone(nn.Module):
  9. def __init__(self, width=1.0, depth=1.0, act_type='silu', norm_type='BN', depthwise=False):
  10. super(Yolox2Backbone, self).__init__()
  11. self.feat_dims = [round(64 * width), round(128 * width), round(256 * width), round(512 * width), round(1024 * width)]
  12. # P1/2
  13. self.layer_1 = Conv(3, self.feat_dims[0], k=6, p=2, s=2, act_type=act_type, norm_type=norm_type)
  14. # P2/4
  15. self.layer_2 = nn.Sequential(
  16. Conv(self.feat_dims[0], self.feat_dims[1], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type),
  17. Yolox2StageBlock(in_dim = self.feat_dims[1],
  18. out_dim = self.feat_dims[1],
  19. num_blocks = round(3*depth),
  20. shortcut = True,
  21. act_type = act_type,
  22. norm_type = norm_type,
  23. depthwise = depthwise)
  24. )
  25. # P3/8
  26. self.layer_3 = nn.Sequential(
  27. Conv(self.feat_dims[1], self.feat_dims[2], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type),
  28. Yolox2StageBlock(in_dim = self.feat_dims[2],
  29. out_dim = self.feat_dims[2],
  30. num_blocks = round(9*depth),
  31. shortcut = True,
  32. act_type = act_type,
  33. norm_type = norm_type,
  34. depthwise = depthwise)
  35. )
  36. # P4/16
  37. self.layer_4 = nn.Sequential(
  38. Conv(self.feat_dims[2], self.feat_dims[3], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type),
  39. Yolox2StageBlock(in_dim = self.feat_dims[3],
  40. out_dim = self.feat_dims[3],
  41. num_blocks = round(9*depth),
  42. shortcut = True,
  43. act_type = act_type,
  44. norm_type = norm_type,
  45. depthwise = depthwise)
  46. )
  47. # P5/32
  48. self.layer_5 = nn.Sequential(
  49. Conv(self.feat_dims[3], self.feat_dims[4], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type),
  50. Yolox2StageBlock(in_dim = self.feat_dims[4],
  51. out_dim = self.feat_dims[4],
  52. num_blocks = round(3*depth),
  53. shortcut = True,
  54. act_type = act_type,
  55. norm_type = norm_type,
  56. depthwise = depthwise)
  57. )
  58. def forward(self, x):
  59. c1 = self.layer_1(x)
  60. c2 = self.layer_2(c1)
  61. c3 = self.layer_3(c2)
  62. c4 = self.layer_4(c3)
  63. c5 = self.layer_5(c4)
  64. outputs = [c3, c4, c5]
  65. return outputs
  66. # ---------------------------- Functions ----------------------------
  67. ## build Backbone
  68. def build_backbone(cfg):
  69. # model
  70. backbone = Yolox2Backbone(width=cfg['width'],
  71. depth=cfg['depth'],
  72. act_type=cfg['bk_act'],
  73. norm_type=cfg['bk_norm'],
  74. depthwise=cfg['bk_depthwise']
  75. )
  76. feat_dims = backbone.feat_dims[-3:]
  77. return backbone, feat_dims
  78. if __name__ == '__main__':
  79. import time
  80. from thop import profile
  81. cfg = {
  82. 'bk_act': 'silu',
  83. 'bk_norm': 'BN',
  84. 'bk_depthwise': False,
  85. 'width': 1.0,
  86. 'depth': 1.0,
  87. }
  88. model, feats = build_backbone(cfg)
  89. x = torch.randn(1, 3, 640, 640)
  90. t0 = time.time()
  91. outputs = model(x)
  92. t1 = time.time()
  93. print('Time: ', t1 - t0)
  94. for out in outputs:
  95. print(out.shape)
  96. x = torch.randn(1, 3, 640, 640)
  97. print('==============================')
  98. flops, params = profile(model, inputs=(x, ), verbose=False)
  99. print('==============================')
  100. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  101. print('Params : {:.2f} M'.format(params / 1e6))