yolov5_pafpn.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from typing import List
  2. import torch
  3. import torch.nn as nn
  4. import torch.nn.functional as F
  5. from .yolov5_basic import BasicConv, CSPBlock
  6. # Yolov5FPN
  7. class Yolov5PaFPN(nn.Module):
  8. def __init__(self, cfg, in_dims: List = [256, 512, 1024]):
  9. super(Yolov5PaFPN, self).__init__()
  10. self.in_dims = in_dims
  11. c3, c4, c5 = in_dims
  12. # ---------------------- Yolov5's Top down FPN ----------------------
  13. ## P5 -> P4
  14. self.reduce_layer_1 = BasicConv(c5, round(512*cfg.width), kernel_size=1, act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  15. self.top_down_layer_1 = CSPBlock(in_dim = c4 + round(512*cfg.width),
  16. out_dim = round(512*cfg.width),
  17. num_blocks = round(3*cfg.depth),
  18. expansion = 0.5,
  19. shortcut = False,
  20. act_type = cfg.fpn_act,
  21. norm_type = cfg.fpn_norm,
  22. depthwise = cfg.fpn_depthwise)
  23. ## P4 -> P3
  24. self.reduce_layer_2 = BasicConv(round(512*cfg.width), round(256*cfg.width), kernel_size=1, act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  25. self.top_down_layer_2 = CSPBlock(in_dim = c3 + round(256*cfg.width),
  26. out_dim = round(256*cfg.width),
  27. num_blocks = round(3*cfg.depth),
  28. expansion = 0.5,
  29. shortcut = False,
  30. act_type = cfg.fpn_act,
  31. norm_type = cfg.fpn_norm,
  32. depthwise = cfg.fpn_depthwise)
  33. # ---------------------- Yolov5's Bottom up PAN ----------------------
  34. ## P3 -> P4
  35. self.downsample_layer_1 = BasicConv(round(256*cfg.width), round(256*cfg.width),
  36. kernel_size=3, padding=1, stride=2,
  37. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  38. self.bottom_up_layer_1 = CSPBlock(in_dim = round(256*cfg.width) + round(256*cfg.width),
  39. out_dim = round(512*cfg.width),
  40. num_blocks = round(3*cfg.depth),
  41. expansion = 0.5,
  42. shortcut = False,
  43. act_type = cfg.fpn_act,
  44. norm_type = cfg.fpn_norm,
  45. depthwise = cfg.fpn_depthwise)
  46. ## P4 -> P5
  47. self.downsample_layer_2 = BasicConv(round(512*cfg.width), round(512*cfg.width),
  48. kernel_size=3, padding=1, stride=2,
  49. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  50. self.bottom_up_layer_2 = CSPBlock(in_dim = round(512*cfg.width) + round(512*cfg.width),
  51. out_dim = round(1024*cfg.width),
  52. num_blocks = round(3*cfg.depth),
  53. expansion = 0.5,
  54. shortcut = False,
  55. act_type = cfg.fpn_act,
  56. norm_type = cfg.fpn_norm,
  57. depthwise = cfg.fpn_depthwise)
  58. # ---------------------- Yolov5's output projection ----------------------
  59. self.out_layers = nn.ModuleList([
  60. BasicConv(in_dim, round(cfg.head_dim*cfg.width), kernel_size=1,
  61. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  62. for in_dim in [round(256*cfg.width), round(512*cfg.width), round(1024*cfg.width)]
  63. ])
  64. self.out_dims = [round(cfg.head_dim*cfg.width)] * 3
  65. def forward(self, features):
  66. c3, c4, c5 = features
  67. # P5 -> P4
  68. p5 = self.reduce_layer_1(c5)
  69. p5_up = F.interpolate(p5, scale_factor=2.0)
  70. p4 = self.top_down_layer_1(torch.cat([c4, p5_up], dim=1))
  71. # P4 -> P3
  72. p4 = self.reduce_layer_2(p4)
  73. p4_up = F.interpolate(p4, scale_factor=2.0)
  74. p3 = self.top_down_layer_2(torch.cat([c3, p4_up], dim=1))
  75. # P3 -> P4
  76. p3_ds = self.downsample_layer_1(p3)
  77. p4 = self.bottom_up_layer_1(torch.cat([p4, p3_ds], dim=1))
  78. # P4 -> P5
  79. p4_ds = self.downsample_layer_2(p4)
  80. p5 = self.bottom_up_layer_2(torch.cat([p5, p4_ds], dim=1))
  81. out_feats = [p3, p4, p5]
  82. # output proj layers
  83. out_feats_proj = []
  84. for feat, layer in zip(out_feats, self.out_layers):
  85. out_feats_proj.append(layer(feat))
  86. return out_feats_proj