yolov5_neck.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import torch
  2. import torch.nn as nn
  3. try:
  4. from .yolov5_basic import Conv
  5. except:
  6. from yolov5_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, in_dim, out_dim, expand_ratio=0.5, pooling_size=5, act_type='silu', norm_type='BN'):
  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=act_type, norm_type=norm_type)
  17. self.cv2 = Conv(inter_dim * 4, out_dim, k=1, act_type=act_type, norm_type=norm_type)
  18. self.m = nn.MaxPool2d(kernel_size=pooling_size, stride=1, padding=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,
  30. in_dim,
  31. out_dim,
  32. expand_ratio=0.5,
  33. pooling_size=5,
  34. act_type='silu',
  35. norm_type='BN',
  36. depthwise=False
  37. ):
  38. super(SPPFBlockCSP, self).__init__()
  39. inter_dim = int(in_dim * expand_ratio)
  40. self.out_dim = out_dim
  41. self.cv1 = Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type)
  42. self.cv2 = Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type)
  43. self.m = nn.Sequential(
  44. Conv(inter_dim, inter_dim, k=3, p=1,
  45. act_type=act_type, norm_type=norm_type,
  46. depthwise=depthwise),
  47. SPPF(inter_dim,
  48. inter_dim,
  49. expand_ratio=1.0,
  50. pooling_size=pooling_size,
  51. act_type=act_type,
  52. norm_type=norm_type),
  53. Conv(inter_dim, inter_dim, k=3, p=1,
  54. act_type=act_type, norm_type=norm_type,
  55. depthwise=depthwise)
  56. )
  57. self.cv3 = Conv(inter_dim * 2, self.out_dim, k=1, act_type=act_type, norm_type=norm_type)
  58. def forward(self, x):
  59. x1 = self.cv1(x)
  60. x2 = self.cv2(x)
  61. x3 = self.m(x2)
  62. y = self.cv3(torch.cat([x1, x3], dim=1))
  63. return y
  64. def build_neck(cfg, in_dim, out_dim):
  65. model = cfg['neck']
  66. print('==============================')
  67. print('Neck: {}'.format(model))
  68. # build neck
  69. if model == 'sppf':
  70. neck = SPPF(
  71. in_dim=in_dim,
  72. out_dim=out_dim,
  73. expand_ratio=cfg['expand_ratio'],
  74. pooling_size=cfg['pooling_size'],
  75. act_type=cfg['neck_act'],
  76. norm_type=cfg['neck_norm']
  77. )
  78. elif model == 'csp_sppf':
  79. neck = SPPFBlockCSP(
  80. in_dim=in_dim,
  81. out_dim=out_dim,
  82. expand_ratio=cfg['expand_ratio'],
  83. pooling_size=cfg['pooling_size'],
  84. act_type=cfg['neck_act'],
  85. norm_type=cfg['neck_norm'],
  86. depthwise=cfg['neck_depthwise']
  87. )
  88. return neck