yolov7_fpn.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. # head conv
  55. self.head_conv_1 = Conv(128, 256, k=3, p=1,
  56. act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  57. self.head_conv_2 = Conv(256, 512, k=3, p=1,
  58. act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  59. self.head_conv_3 = Conv(512, 1024, k=3, p=1,
  60. act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  61. # output proj layers
  62. if out_dim is not None:
  63. self.out_layers = nn.ModuleList([
  64. Conv(in_dim, out_dim, k=1,
  65. norm_type=norm_type, act_type=act_type)
  66. for in_dim in [256, 512, 1024]
  67. ])
  68. self.out_dim = [out_dim] * 3
  69. else:
  70. self.out_layers = None
  71. self.out_dim = [256, 512, 1024]
  72. def forward(self, features):
  73. c3, c4, c5 = features
  74. # Top down
  75. ## P5 -> P4
  76. c6 = self.reduce_layer_1(c5)
  77. c7 = F.interpolate(c6, scale_factor=2.0)
  78. c8 = torch.cat([c7, self.reduce_layer_2(c4)], dim=1)
  79. c9 = self.top_down_layer_1(c8)
  80. ## P4 -> P3
  81. c10 = self.reduce_layer_3(c9)
  82. c11 = F.interpolate(c10, scale_factor=2.0)
  83. c12 = torch.cat([c11, self.reduce_layer_4(c3)], dim=1)
  84. c13 = self.top_down_layer_2(c12)
  85. # Bottom up
  86. # p3 -> P4
  87. c14 = self.downsample_layer_1(c13)
  88. c15 = torch.cat([c14, c9], dim=1)
  89. c16 = self.bottom_up_layer_1(c15)
  90. # P4 -> P5
  91. c17 = self.downsample_layer_2(c16)
  92. c18 = torch.cat([c17, c5], dim=1)
  93. c19 = self.bottom_up_layer_2(c18)
  94. c20 = self.head_conv_1(c13)
  95. c21 = self.head_conv_2(c16)
  96. c22 = self.head_conv_3(c19)
  97. out_feats = [c20, c21, c22] # [P3, P4, P5]
  98. # output proj layers
  99. if self.out_layers is not None:
  100. out_feats_proj = []
  101. for feat, layer in zip(out_feats, self.out_layers):
  102. out_feats_proj.append(layer(feat))
  103. return out_feats_proj
  104. return out_feats
  105. def build_fpn(cfg, in_dims, out_dim=None):
  106. model = cfg['fpn']
  107. # build neck
  108. if model == 'yolov7_pafpn':
  109. fpn_net = Yolov7PaFPN(in_dims=in_dims,
  110. out_dim=out_dim,
  111. act_type=cfg['fpn_act'],
  112. norm_type=cfg['fpn_norm'],
  113. depthwise=cfg['fpn_depthwise']
  114. )
  115. return fpn_net