yolov8_neck.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. def __init__(self, in_dim, out_dim, expand_ratio=0.5, pooling_size=5, act_type='', norm_type=''):
  10. super().__init__()
  11. inter_dim = int(in_dim * expand_ratio)
  12. self.out_dim = out_dim
  13. self.cv1 = Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type)
  14. self.cv2 = Conv(inter_dim * 4, out_dim, k=1, act_type=act_type, norm_type=norm_type)
  15. self.m = nn.MaxPool2d(kernel_size=pooling_size, stride=1, padding=pooling_size // 2)
  16. def forward(self, x):
  17. x = self.cv1(x)
  18. y1 = self.m(x)
  19. y2 = self.m(y1)
  20. return self.cv2(torch.cat((x, y1, y2, self.m(y2)), 1))
  21. # SPPF block with CSP module
  22. class SPPFBlockCSP(nn.Module):
  23. """
  24. CSP Spatial Pyramid Pooling Block
  25. """
  26. def __init__(self,
  27. in_dim,
  28. out_dim,
  29. expand_ratio=0.5,
  30. pooling_size=5,
  31. act_type='lrelu',
  32. norm_type='BN',
  33. depthwise=False
  34. ):
  35. super(SPPFBlockCSP, self).__init__()
  36. inter_dim = int(in_dim * expand_ratio)
  37. self.out_dim = out_dim
  38. self.cv1 = Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type)
  39. self.cv2 = Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type)
  40. self.m = nn.Sequential(
  41. Conv(inter_dim, inter_dim, k=3, p=1,
  42. act_type=act_type, norm_type=norm_type,
  43. depthwise=depthwise),
  44. SPPF(inter_dim,
  45. inter_dim,
  46. expand_ratio=1.0,
  47. pooling_size=pooling_size,
  48. act_type=act_type,
  49. norm_type=norm_type),
  50. Conv(inter_dim, inter_dim, k=3, p=1,
  51. act_type=act_type, norm_type=norm_type,
  52. depthwise=depthwise)
  53. )
  54. self.cv3 = Conv(inter_dim * 2, self.out_dim, k=1, act_type=act_type, norm_type=norm_type)
  55. def forward(self, x):
  56. x1 = self.cv1(x)
  57. x2 = self.cv2(x)
  58. x3 = self.m(x2)
  59. y = self.cv3(torch.cat([x1, x3], dim=1))
  60. return y
  61. def build_neck(cfg, in_dim, out_dim):
  62. model = cfg['neck']
  63. print('==============================')
  64. print('Neck: {}'.format(model))
  65. # build neck
  66. if model == 'sppf':
  67. neck = SPPF(
  68. in_dim=in_dim,
  69. out_dim=out_dim,
  70. expand_ratio=cfg['expand_ratio'],
  71. pooling_size=cfg['pooling_size'],
  72. act_type=cfg['neck_act'],
  73. norm_type=cfg['neck_norm']
  74. )
  75. elif model == 'sppf_block_csp':
  76. neck = SPPFBlockCSP(
  77. in_dim=in_dim,
  78. out_dim=out_dim,
  79. expand_ratio=cfg['expand_ratio'],
  80. pooling_size=cfg['pooling_size'],
  81. act_type=cfg['neck_act'],
  82. norm_type=cfg['neck_norm'],
  83. depthwise=cfg['neck_depthwise']
  84. )
  85. return neck