yolov8_neck.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import torch
  2. import torch.nn as nn
  3. try:
  4. from .yolov8_basic import Conv
  5. except:
  6. from yolov8_basic import Conv
  7. # Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher
  8. class SPPF(nn.Module):
  9. """
  10. This code referenced to https://github.com/ultralytics/yolov5
  11. """
  12. def __init__(self, cfg, in_dim, out_dim, expand_ratio=0.5):
  13. super().__init__()
  14. inter_dim = int(in_dim * expand_ratio)
  15. self.out_dim = out_dim
  16. self.cv1 = Conv(in_dim, inter_dim, k=1, act_type=cfg['neck_act'], norm_type=cfg['neck_norm'])
  17. self.cv2 = Conv(inter_dim * 4, out_dim, k=1, act_type=cfg['neck_act'], norm_type=cfg['neck_norm'])
  18. self.m = nn.MaxPool2d(kernel_size=cfg['pooling_size'], stride=1, padding=cfg['pooling_size'] // 2)
  19. def forward(self, x):
  20. x = self.cv1(x)
  21. y1 = self.m(x)
  22. y2 = self.m(y1)
  23. return self.cv2(torch.cat((x, y1, y2, self.m(y2)), 1))
  24. # SPPF block with CSP module
  25. class SPPFBlockCSP(nn.Module):
  26. """
  27. CSP Spatial Pyramid Pooling Block
  28. """
  29. def __init__(self, cfg, in_dim, out_dim, expand_ratio):
  30. super(SPPFBlockCSP, self).__init__()
  31. inter_dim = int(in_dim * expand_ratio)
  32. self.out_dim = out_dim
  33. self.cv1 = Conv(in_dim, inter_dim, k=1, act_type=cfg['neck_act'], norm_type=cfg['neck_norm'])
  34. self.cv2 = Conv(in_dim, inter_dim, k=1, act_type=cfg['neck_act'], norm_type=cfg['neck_norm'])
  35. self.m = nn.Sequential(
  36. Conv(inter_dim, inter_dim, k=3, p=1,
  37. act_type=cfg['neck_act'], norm_type=cfg['neck_norm'],
  38. depthwise=cfg['neck_depthwise']),
  39. SPPF(cfg, inter_dim, inter_dim, expand_ratio=1.0),
  40. Conv(inter_dim, inter_dim, k=3, p=1,
  41. act_type=cfg['neck_act'], norm_type=cfg['neck_norm'],
  42. depthwise=cfg['neck_depthwise'])
  43. )
  44. self.cv3 = Conv(inter_dim * 2, self.out_dim, k=1, act_type=cfg['neck_act'], norm_type=cfg['neck_norm'])
  45. def forward(self, x):
  46. x1 = self.cv1(x)
  47. x2 = self.cv2(x)
  48. x3 = self.m(x2)
  49. y = self.cv3(torch.cat([x1, x3], dim=1))
  50. return y
  51. def build_neck(cfg, in_dim, out_dim):
  52. model = cfg['neck']
  53. print('==============================')
  54. print('Neck: {}'.format(model))
  55. # build neck
  56. if model == 'sppf':
  57. neck = SPPF(cfg, in_dim, out_dim, cfg['neck_expand_ratio'])
  58. elif model == 'csp_sppf':
  59. neck = SPPFBlockCSP(cfg, in_dim, out_dim, cfg['neck_expand_ratio'])
  60. return neck
  61. if __name__ == '__main__':
  62. import time
  63. from thop import profile
  64. cfg = {
  65. ## Neck: SPP
  66. 'neck': 'sppf',
  67. 'neck_expand_ratio': 0.5,
  68. 'pooling_size': 5,
  69. 'neck_act': 'silu',
  70. 'neck_norm': 'BN',
  71. 'neck_depthwise': False,
  72. }
  73. in_dim = 512
  74. out_dim = 512
  75. # Head-1
  76. model = build_neck(cfg, in_dim, out_dim)
  77. feat = torch.randn(1, in_dim, 20, 20)
  78. t0 = time.time()
  79. outputs = model(feat)
  80. t1 = time.time()
  81. print('Time: ', t1 - t0)
  82. # for out in outputs:
  83. # print(out.shape)
  84. print('==============================')
  85. flops, params = profile(model, inputs=(feat, ), verbose=False)
  86. print('==============================')
  87. print('FPN: GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  88. print('FPN: Params : {:.2f} M'.format(params / 1e6))