yolov5_neck.py 3.1 KB

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