yolov5_af_neck.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import torch
  2. import torch.nn as nn
  3. try:
  4. from .yolov5_af_basic import BasicConv
  5. except:
  6. from yolov5_af_basic import BasicConv
  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):
  13. super().__init__()
  14. ## ----------- Basic Parameters -----------
  15. inter_dim = round(in_dim * cfg.neck_expand_ratio)
  16. self.out_dim = out_dim
  17. ## ----------- Network Parameters -----------
  18. self.cv1 = BasicConv(in_dim, inter_dim,
  19. kernel_size=1, padding=0, stride=1,
  20. act_type=cfg.neck_act, norm_type=cfg.neck_norm)
  21. self.cv2 = BasicConv(inter_dim * 4, out_dim,
  22. kernel_size=1, padding=0, stride=1,
  23. act_type=cfg.neck_act, norm_type=cfg.neck_norm)
  24. self.m = nn.MaxPool2d(kernel_size=cfg.spp_pooling_size,
  25. stride=1,
  26. padding=cfg.spp_pooling_size // 2)
  27. def forward(self, x):
  28. x = self.cv1(x)
  29. y1 = self.m(x)
  30. y2 = self.m(y1)
  31. return self.cv2(torch.cat((x, y1, y2, self.m(y2)), 1))
  32. if __name__=='__main__':
  33. import time
  34. from thop import profile
  35. # Model config
  36. # YOLOv5-Base config
  37. class Yolov5BaseConfig(object):
  38. def __init__(self) -> None:
  39. # ---------------- Model config ----------------
  40. self.out_stride = 32
  41. self.max_stride = 32
  42. ## Neck
  43. self.neck_act = 'lrelu'
  44. self.neck_norm = 'BN'
  45. self.neck_depthwise = False
  46. self.neck_expand_ratio = 0.5
  47. self.spp_pooling_size = 5
  48. cfg = Yolov5BaseConfig()
  49. # Build a head
  50. in_dim = 512
  51. out_dim = 512
  52. neck = SPPF(cfg, in_dim, out_dim)
  53. # Inference
  54. x = torch.randn(1, in_dim, 20, 20)
  55. t0 = time.time()
  56. output = neck(x)
  57. t1 = time.time()
  58. print('Time: ', t1 - t0)
  59. print('Neck output: ', output.shape)
  60. flops, params = profile(neck, inputs=(x, ), verbose=False)
  61. print('==============================')
  62. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  63. print('Params : {:.2f} M'.format(params / 1e6))