yolov7_pafpn.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. try:
  5. from .modules import ConvModule, ELANBlockFPN, DownSample
  6. except:
  7. from modules import ConvModule, ELANBlockFPN, DownSample
  8. # PaFPN-ELAN (YOLOv7's)
  9. class Yolov7PaFPN(nn.Module):
  10. def __init__(self,
  11. in_dims = [512, 1024, 512],
  12. head_dim = 256,
  13. ):
  14. super(Yolov7PaFPN, self).__init__()
  15. # ----------------------------- Basic parameters -----------------------------
  16. self.in_dims = in_dims
  17. self.head_dim = head_dim
  18. self.fpn_out_dims = [head_dim] * 3
  19. self.branch_width = 4
  20. self.branch_depth = 1
  21. c3, c4, c5 = self.in_dims
  22. # ----------------------------- Top-down FPN -----------------------------
  23. ## P5 -> P4
  24. self.reduce_layer_1 = ConvModule(c5, 256, kernel_size=1)
  25. self.reduce_layer_2 = ConvModule(c4, 256, kernel_size=1)
  26. self.top_down_layer_1 = ELANBlockFPN(in_dim = 256 + 256,
  27. out_dim = 256,
  28. expansion = 0.5,
  29. branch_width = self.branch_width,
  30. branch_depth = self.branch_depth,
  31. )
  32. ## P4 -> P3
  33. self.reduce_layer_3 = ConvModule(256, 128, kernel_size=1)
  34. self.reduce_layer_4 = ConvModule(c3, 128, kernel_size=1)
  35. self.top_down_layer_2 = ELANBlockFPN(in_dim = 128 + 128,
  36. out_dim = 128,
  37. expansion = 0.5,
  38. branch_width = self.branch_width,
  39. branch_depth = self.branch_depth,
  40. )
  41. # ----------------------------- Bottom-up FPN -----------------------------
  42. ## P3 -> P4
  43. self.downsample_layer_1 = DownSample(128, 256)
  44. self.bottom_up_layer_1 = ELANBlockFPN(in_dim = 256 + 256,
  45. out_dim = 256,
  46. expansion = 0.5,
  47. branch_width = self.branch_width,
  48. branch_depth = self.branch_depth,
  49. )
  50. ## P4 -> P5
  51. self.downsample_layer_2 = DownSample(256, 512)
  52. self.bottom_up_layer_2 = ELANBlockFPN(in_dim = 512 + c5,
  53. out_dim = 512,
  54. expansion = 0.5,
  55. branch_width = self.branch_width,
  56. branch_depth = self.branch_depth,
  57. )
  58. ## Head convs
  59. self.head_conv_1 = ConvModule(128, 256, kernel_size=3, stride=1)
  60. self.head_conv_2 = ConvModule(256, 512, kernel_size=3, stride=1)
  61. self.head_conv_3 = ConvModule(512, 1024, kernel_size=3, stride=1)
  62. ## Output projs
  63. self.out_layers = nn.ModuleList([ConvModule(in_dim, head_dim, kernel_size=1)
  64. for in_dim in [256, 512, 1024]
  65. ])
  66. def forward(self, features):
  67. c3, c4, c5 = features
  68. # Top down
  69. ## P5 -> P4
  70. c6 = self.reduce_layer_1(c5)
  71. c7 = F.interpolate(c6, scale_factor=2.0)
  72. c8 = torch.cat([c7, self.reduce_layer_2(c4)], dim=1)
  73. c9 = self.top_down_layer_1(c8)
  74. ## P4 -> P3
  75. c10 = self.reduce_layer_3(c9)
  76. c11 = F.interpolate(c10, scale_factor=2.0)
  77. c12 = torch.cat([c11, self.reduce_layer_4(c3)], dim=1)
  78. c13 = self.top_down_layer_2(c12)
  79. # Bottom up
  80. ## p3 -> P4
  81. c14 = self.downsample_layer_1(c13)
  82. c15 = torch.cat([c14, c9], dim=1)
  83. c16 = self.bottom_up_layer_1(c15)
  84. ## P4 -> P5
  85. c17 = self.downsample_layer_2(c16)
  86. c18 = torch.cat([c17, c5], dim=1)
  87. c19 = self.bottom_up_layer_2(c18)
  88. c20 = self.head_conv_1(c13)
  89. c21 = self.head_conv_2(c16)
  90. c22 = self.head_conv_3(c19)
  91. out_feats = [c20, c21, c22] # [P3, P4, P5]
  92. # output proj layers
  93. out_feats_proj = []
  94. for feat, layer in zip(out_feats, self.out_layers):
  95. out_feats_proj.append(layer(feat))
  96. return out_feats_proj
  97. if __name__=='__main__':
  98. import time
  99. from thop import profile
  100. # Model config
  101. # Build a head
  102. in_dims = [128, 256, 512]
  103. fpn = Yolov7PaFPN(in_dims, head_dim=256)
  104. # Randomly generate a input data
  105. x = [torch.randn(1, in_dims[0], 80, 80),
  106. torch.randn(1, in_dims[1], 40, 40),
  107. torch.randn(1, in_dims[2], 20, 20)]
  108. # Inference
  109. t0 = time.time()
  110. output = fpn(x)
  111. t1 = time.time()
  112. print('Time: ', t1 - t0)
  113. print('====== FPN output ====== ')
  114. for level, feat in enumerate(output):
  115. print("- Level-{} : ".format(level), feat.shape)
  116. flops, params = profile(fpn, inputs=(x, ), verbose=False)
  117. print('==============================')
  118. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  119. print('Params : {:.2f} M'.format(params / 1e6))