yolov7_fpn.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from .yolov7_basic import Conv, ELANBlockFPN, DownSampleFPN
  5. # PaFPN-ELAN (YOLOv7's)
  6. class Yolov7PaFPN(nn.Module):
  7. def __init__(self,
  8. in_dims=[512, 1024, 512],
  9. out_dim=None,
  10. act_type='silu',
  11. norm_type='BN',
  12. depthwise=False):
  13. super(Yolov7PaFPN, self).__init__()
  14. self.in_dims = in_dims
  15. c3, c4, c5 = in_dims
  16. # top dwon
  17. ## P5 -> P4
  18. self.reduce_layer_1 = Conv(c5, 256, k=1, norm_type=norm_type, act_type=act_type)
  19. self.reduce_layer_2 = Conv(c4, 256, k=1, norm_type=norm_type, act_type=act_type)
  20. self.top_down_layer_1 = ELANBlockFPN(in_dim=256 + 256,
  21. out_dim=256,
  22. act_type=act_type,
  23. norm_type=norm_type,
  24. depthwise=depthwise
  25. )
  26. # P4 -> P3
  27. self.reduce_layer_3 = Conv(256, 128, k=1, norm_type=norm_type, act_type=act_type)
  28. self.reduce_layer_4 = Conv(c3, 128, k=1, norm_type=norm_type, act_type=act_type)
  29. self.top_down_layer_2 = ELANBlockFPN(in_dim=128 + 128,
  30. out_dim=128,
  31. act_type=act_type,
  32. norm_type=norm_type,
  33. depthwise=depthwise
  34. )
  35. # bottom up
  36. # P3 -> P4
  37. self.downsample_layer_1 = DownSampleFPN(128, act_type=act_type,
  38. norm_type=norm_type, depthwise=depthwise)
  39. self.bottom_up_layer_1 = ELANBlockFPN(in_dim=256 + 256,
  40. out_dim=256,
  41. act_type=act_type,
  42. norm_type=norm_type,
  43. depthwise=depthwise
  44. )
  45. # P4 -> P5
  46. self.downsample_layer_2 = DownSampleFPN(256, act_type=act_type,
  47. norm_type=norm_type, depthwise=depthwise)
  48. self.bottom_up_layer_2 = ELANBlockFPN(in_dim=512 + c5,
  49. out_dim=512,
  50. act_type=act_type,
  51. norm_type=norm_type,
  52. depthwise=depthwise
  53. )
  54. # output proj layers
  55. if out_dim is not None:
  56. self.out_layers = nn.ModuleList([
  57. Conv(in_dim, out_dim, k=1,
  58. norm_type=norm_type, act_type=act_type)
  59. for in_dim in [128, 256, 512]
  60. ])
  61. self.out_dim = [out_dim] * 3
  62. else:
  63. self.out_layers = None
  64. self.out_dim = [128, 256, 512]
  65. def forward(self, features):
  66. c3, c4, c5 = features
  67. # Top down
  68. ## P5 -> P4
  69. c6 = self.reduce_layer_1(c5)
  70. c7 = F.interpolate(c6, scale_factor=2.0)
  71. c8 = torch.cat([c7, self.reduce_layer_2(c4)], dim=1)
  72. c9 = self.top_down_layer_1(c8)
  73. ## P4 -> P3
  74. c10 = self.reduce_layer_3(c9)
  75. c11 = F.interpolate(c10, scale_factor=2.0)
  76. c12 = torch.cat([c11, self.reduce_layer_4(c3)], dim=1)
  77. c13 = self.top_down_layer_2(c12)
  78. # Bottom up
  79. # p3 -> P4
  80. c14 = self.downsample_layer_1(c13)
  81. c15 = torch.cat([c14, c9], dim=1)
  82. c16 = self.bottom_up_layer_1(c15)
  83. # P4 -> P5
  84. c17 = self.downsample_layer_2(c16)
  85. c18 = torch.cat([c17, c5], dim=1)
  86. c19 = self.bottom_up_layer_2(c18)
  87. out_feats = [c13, c16, c19] # [P3, P4, P5]
  88. # output proj layers
  89. if self.out_layers is not None:
  90. out_feats_proj = []
  91. for feat, layer in zip(out_feats, self.out_layers):
  92. out_feats_proj.append(layer(feat))
  93. return out_feats_proj
  94. return out_feats
  95. def build_fpn(cfg, in_dims, out_dim=None):
  96. model = cfg['fpn']
  97. # build neck
  98. if model == 'yolov7_pafpn':
  99. fpn_net = Yolov7PaFPN(in_dims=in_dims,
  100. out_dim=out_dim,
  101. act_type=cfg['fpn_act'],
  102. norm_type=cfg['fpn_norm'],
  103. depthwise=cfg['fpn_depthwise']
  104. )
  105. return fpn_net