yolov4_backbone.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import torch
  2. import torch.nn as nn
  3. try:
  4. from .modules import ConvModule, CSPBlock
  5. except:
  6. from modules import ConvModule, CSPBlock
  7. # IN1K pretrained weight
  8. pretrained_urls = {
  9. 'n': None,
  10. 's': None,
  11. 'm': None,
  12. 'l': None,
  13. 'x': None,
  14. }
  15. # --------------------- Yolov3's Backbone -----------------------
  16. ## Modified DarkNet
  17. class Yolov4Backbone(nn.Module):
  18. def __init__(self, cfg):
  19. super(Yolov4Backbone, self).__init__()
  20. # ------------------ Basic setting ------------------
  21. self.model_scale = cfg.model_scale
  22. self.feat_dims = [round(64 * cfg.width),
  23. round(128 * cfg.width),
  24. round(256 * cfg.width),
  25. round(512 * cfg.width),
  26. round(1024 * cfg.width)]
  27. # ------------------ Network setting ------------------
  28. ## P1/2
  29. self.layer_1 = ConvModule(3, self.feat_dims[0], kernel_size=6, padding=2, stride=2)
  30. # P2/4
  31. self.layer_2 = nn.Sequential(
  32. ConvModule(self.feat_dims[0], self.feat_dims[1], kernel_size=3, padding=1, stride=2),
  33. CSPBlock(in_dim = self.feat_dims[1],
  34. out_dim = self.feat_dims[1],
  35. num_blocks = round(3*cfg.depth),
  36. expansion = 0.5,
  37. shortcut = True,
  38. )
  39. )
  40. # P3/8
  41. self.layer_3 = nn.Sequential(
  42. ConvModule(self.feat_dims[1], self.feat_dims[2], kernel_size=3, padding=1, stride=2),
  43. CSPBlock(in_dim = self.feat_dims[2],
  44. out_dim = self.feat_dims[2],
  45. num_blocks = round(9*cfg.depth),
  46. expansion = 0.5,
  47. shortcut = True,
  48. )
  49. )
  50. # P4/16
  51. self.layer_4 = nn.Sequential(
  52. ConvModule(self.feat_dims[2], self.feat_dims[3], kernel_size=3, padding=1, stride=2),
  53. CSPBlock(in_dim = self.feat_dims[3],
  54. out_dim = self.feat_dims[3],
  55. num_blocks = round(9*cfg.depth),
  56. expansion = 0.5,
  57. shortcut = True,
  58. )
  59. )
  60. # P5/32
  61. self.layer_5 = nn.Sequential(
  62. ConvModule(self.feat_dims[3], self.feat_dims[4], kernel_size=3, padding=1, stride=2),
  63. CSPBlock(in_dim = self.feat_dims[4],
  64. out_dim = self.feat_dims[4],
  65. num_blocks = round(3*cfg.depth),
  66. expansion = 0.5,
  67. shortcut = True,
  68. )
  69. )
  70. # Initialize all layers
  71. self.init_weights()
  72. def init_weights(self):
  73. """Initialize the parameters."""
  74. for m in self.modules():
  75. if isinstance(m, torch.nn.Conv2d):
  76. m.reset_parameters()
  77. def forward(self, x):
  78. c1 = self.layer_1(x)
  79. c2 = self.layer_2(c1)
  80. c3 = self.layer_3(c2)
  81. c4 = self.layer_4(c3)
  82. c5 = self.layer_5(c4)
  83. outputs = [c3, c4, c5]
  84. return outputs
  85. if __name__ == '__main__':
  86. import time
  87. from thop import profile
  88. class BaseConfig(object):
  89. def __init__(self) -> None:
  90. self.width = 0.5
  91. self.depth = 0.34
  92. self.model_scale = "s"
  93. self.use_pretrained = True
  94. cfg = BaseConfig()
  95. model = Yolov4Backbone(cfg)
  96. x = torch.randn(1, 3, 640, 640)
  97. t0 = time.time()
  98. outputs = model(x)
  99. print(model)
  100. t1 = time.time()
  101. print('Time: ', t1 - t0)
  102. for out in outputs:
  103. print(out.shape)
  104. x = torch.randn(1, 3, 640, 640)
  105. print('==============================')
  106. flops, params = profile(model, inputs=(x, ), verbose=False)
  107. print('==============================')
  108. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  109. print('Params : {:.2f} M'.format(params / 1e6))