yolov7_pafpn.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from typing import List
  2. import torch
  3. import torch.nn as nn
  4. import torch.nn.functional as F
  5. from .yolov7_basic import BasicConv, ELANLayer, MDown
  6. # PaFPN-ELAN (YOLOv7's)
  7. class Yolov7PaFPN(nn.Module):
  8. def __init__(self, cfg, in_dims: List = [512, 1024, 512]):
  9. super(Yolov7PaFPN, self).__init__()
  10. # ----------------------------- Basic parameters -----------------------------
  11. self.in_dims = in_dims
  12. c3, c4, c5 = in_dims
  13. # ----------------------------- Top-down FPN -----------------------------
  14. ## P5 -> P4
  15. self.reduce_layer_1 = BasicConv(c5, round(256*cfg.width),
  16. kernel_size=1, act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  17. self.reduce_layer_2 = BasicConv(c4, round(256*cfg.width),
  18. kernel_size=1, act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  19. self.top_down_layer_1 = ELANLayer(in_dim = round(256*cfg.width) + round(256*cfg.width),
  20. out_dim = round(256*cfg.width),
  21. expansion = 0.5,
  22. num_blocks = round(3*cfg.depth),
  23. act_type = cfg.fpn_act,
  24. norm_type = cfg.fpn_norm,
  25. depthwise = cfg.fpn_depthwise,
  26. )
  27. ## P4 -> P3
  28. self.reduce_layer_3 = BasicConv(round(256*cfg.width), round(128*cfg.width),
  29. kernel_size=1, act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  30. self.reduce_layer_4 = BasicConv(c3, round(128*cfg.width),
  31. kernel_size=1, act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  32. self.top_down_layer_2 = ELANLayer(in_dim = round(128*cfg.width) + round(128*cfg.width),
  33. out_dim = round(128*cfg.width),
  34. expansion = 0.5,
  35. num_blocks = round(3*cfg.depth),
  36. act_type = cfg.fpn_act,
  37. norm_type = cfg.fpn_norm,
  38. depthwise = cfg.fpn_depthwise,
  39. )
  40. # ----------------------------- Bottom-up FPN -----------------------------
  41. ## P3 -> P4
  42. self.downsample_layer_1 = MDown(round(128*cfg.width), round(256*cfg.width),
  43. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  44. self.bottom_up_layer_1 = ELANLayer(in_dim = round(256*cfg.width) + round(256*cfg.width),
  45. out_dim = round(256*cfg.width),
  46. expansion = 0.5,
  47. num_blocks = round(3*cfg.depth),
  48. act_type = cfg.fpn_act,
  49. norm_type = cfg.fpn_norm,
  50. depthwise = cfg.fpn_depthwise,
  51. )
  52. ## P4 -> P5
  53. self.downsample_layer_2 = MDown(round(256*cfg.width), round(512*cfg.width),
  54. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  55. self.bottom_up_layer_2 = ELANLayer(in_dim = round(512*cfg.width) + c5,
  56. out_dim = round(512*cfg.width),
  57. expansion = 0.5,
  58. num_blocks = round(3*cfg.depth),
  59. act_type = cfg.fpn_act,
  60. norm_type = cfg.fpn_norm,
  61. depthwise = cfg.fpn_depthwise,
  62. )
  63. # ----------------------------- Head conv layers -----------------------------
  64. ## Head convs
  65. self.head_conv_1 = BasicConv(round(128*cfg.width), round(256*cfg.width),
  66. kernel_size=3, padding=1, stride=1,
  67. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  68. self.head_conv_2 = BasicConv(round(256*cfg.width), round(512*cfg.width),
  69. kernel_size=3, padding=1, stride=1,
  70. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  71. self.head_conv_3 = BasicConv(round(512*cfg.width), round(1024*cfg.width),
  72. kernel_size=3, padding=1, stride=1,
  73. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
  74. # ---------------------- Yolov5's output projection ----------------------
  75. self.out_layers = nn.ModuleList([
  76. BasicConv(in_dim, round(cfg.head_dim*cfg.width), kernel_size=1,
  77. act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
  78. for in_dim in [round(256*cfg.width), round(512*cfg.width), round(1024*cfg.width)]
  79. ])
  80. self.out_dims = [round(cfg.head_dim*cfg.width)] * 3
  81. def forward(self, features):
  82. c3, c4, c5 = features
  83. ## P5 -> P4
  84. p5 = self.reduce_layer_1(c5)
  85. p5_up = F.interpolate(p5, scale_factor=2.0)
  86. p4 = self.reduce_layer_2(c4)
  87. p4 = self.top_down_layer_1(torch.cat([p5_up, p4], dim=1))
  88. ## P4 -> P3
  89. p4_in = self.reduce_layer_3(p4)
  90. p4_up = F.interpolate(p4_in, scale_factor=2.0)
  91. p3 = self.reduce_layer_4(c3)
  92. p3 = self.top_down_layer_2(torch.cat([p4_up, p3], dim=1))
  93. ## P3 -> P4
  94. p3_ds = self.downsample_layer_1(p3)
  95. p4 = torch.cat([p3_ds, p4], dim=1)
  96. p4 = self.bottom_up_layer_1(p4)
  97. ## P4 -> P5
  98. p4_ds = self.downsample_layer_2(p4)
  99. p5 = torch.cat([p4_ds, c5], dim=1)
  100. p5 = self.bottom_up_layer_2(p5)
  101. out_feats = [self.head_conv_1(p3), self.head_conv_2(p4), self.head_conv_3(p5)]
  102. # output proj layers
  103. out_feats_proj = []
  104. for feat, layer in zip(out_feats, self.out_layers):
  105. out_feats_proj.append(layer(feat))
  106. return out_feats_proj