yolox2_pafpn.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. try:
  5. from .yolox2_basic import Conv, Yolox2StageBlock
  6. except:
  7. from yolox2_basic import Conv, Yolox2StageBlock
  8. # PaFPN-ELAN
  9. class Yolox2PaFPN(nn.Module):
  10. def __init__(self,
  11. in_dims = [256, 512, 1024],
  12. out_dim = None,
  13. width = 1.0,
  14. depth = 1.0,
  15. act_type = 'silu',
  16. norm_type = 'BN',
  17. depthwise = False):
  18. super(Yolox2PaFPN, self).__init__()
  19. print('==============================')
  20. print('FPN: {}'.format("Yolox2 PaFPN"))
  21. # ---------------- Basic parameters ----------------
  22. self.in_dims = in_dims
  23. self.width = width
  24. self.depth = depth
  25. c3, c4, c5 = in_dims
  26. # ---------------- Top dwon ----------------
  27. ## P5 -> P4
  28. self.reduce_layer_1 = Conv(c5, round(512*width), k=1, act_type=act_type, norm_type=norm_type)
  29. self.top_down_layer_1 = Yolox2StageBlock(in_dim = round(512*width) + c4,
  30. out_dim = round(512*width),
  31. num_blocks = round(3*depth),
  32. shortcut = False,
  33. act_type = act_type,
  34. norm_type = norm_type,
  35. depthwise = depthwise,
  36. )
  37. ## P4 -> P3
  38. self.reduce_layer_2 = Conv(round(512*width), round(256*width), k=1, act_type=act_type, norm_type=norm_type)
  39. self.top_down_layer_2 = Yolox2StageBlock(in_dim = round(256*width) + c3,
  40. out_dim = round(256*width),
  41. num_blocks = round(3*depth),
  42. shortcut = False,
  43. act_type = act_type,
  44. norm_type = norm_type,
  45. depthwise = depthwise,
  46. )
  47. # ---------------- Bottom up ----------------
  48. ## P3 -> P4
  49. self.downsample_layer_1 = Conv(round(256*width), round(256*width), k=3, p=1, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  50. self.bottom_up_layer_1 = Yolox2StageBlock(in_dim = round(256*width) + round(256*width),
  51. out_dim = round(512*width),
  52. num_blocks = round(3*depth),
  53. shortcut = False,
  54. act_type = act_type,
  55. norm_type = norm_type,
  56. depthwise = depthwise,
  57. )
  58. ## P4 -> P5
  59. self.downsample_layer_2 = Conv(round(512*width), round(512*width), k=3, p=1, s=2, act_type=act_type, norm_type=norm_type, depthwise=depthwise)
  60. self.bottom_up_layer_2 = Yolox2StageBlock(in_dim = round(512 * width) + round(512 * width),
  61. out_dim = round(1024 * width),
  62. num_blocks = round(3*depth),
  63. shortcut = False,
  64. act_type = act_type,
  65. norm_type = norm_type,
  66. depthwise = depthwise,
  67. )
  68. ## output proj layers
  69. if out_dim is not None:
  70. self.out_layers = nn.ModuleList([
  71. Conv(in_dim, out_dim, k=1, act_type=act_type, norm_type=norm_type)
  72. for in_dim in [round(256*width), round(512*width), round(1024*width)]
  73. ])
  74. self.out_dim = [out_dim] * 3
  75. else:
  76. self.out_layers = None
  77. self.out_dim = [round(256*width), round(512*width), round(1024*width)]
  78. self.init_weights()
  79. def init_weights(self):
  80. """Initialize the parameters."""
  81. for m in self.modules():
  82. if isinstance(m, torch.nn.Conv2d):
  83. # In order to be consistent with the source code,
  84. # reset the Conv2d initialization parameters
  85. m.reset_parameters()
  86. def forward(self, features):
  87. c3, c4, c5 = features
  88. # Top down
  89. ## P5 -> P4
  90. c6 = self.reduce_layer_1(c5)
  91. c7 = F.interpolate(c6, scale_factor=2.0)
  92. c8 = torch.cat([c7, c4], dim=1)
  93. c9 = self.top_down_layer_1(c8)
  94. ## P4 -> P3
  95. c10 = self.reduce_layer_2(c9)
  96. c11 = F.interpolate(c10, scale_factor=2.0)
  97. c12 = torch.cat([c11, c3], dim=1)
  98. c13 = self.top_down_layer_2(c12)
  99. # Bottom up
  100. ## p3 -> P4
  101. c14 = self.downsample_layer_1(c13)
  102. c15 = torch.cat([c14, c10], dim=1)
  103. c16 = self.bottom_up_layer_1(c15)
  104. ## P4 -> P5
  105. c17 = self.downsample_layer_2(c16)
  106. c18 = torch.cat([c17, c6], dim=1)
  107. c19 = self.bottom_up_layer_2(c18)
  108. out_feats = [c13, c16, c19] # [P3, P4, P5]
  109. # output proj layers
  110. if self.out_layers is not None:
  111. out_feats_proj = []
  112. for feat, layer in zip(out_feats, self.out_layers):
  113. out_feats_proj.append(layer(feat))
  114. return out_feats_proj
  115. return out_feats
  116. def build_fpn(cfg, in_dims, out_dim=None):
  117. model = cfg['fpn']
  118. # build neck
  119. if model == 'yolox2_pafpn':
  120. fpn_net = Yolox2PaFPN(in_dims = in_dims,
  121. out_dim = out_dim,
  122. width = cfg['width'],
  123. depth = cfg['depth'],
  124. act_type = cfg['fpn_act'],
  125. norm_type = cfg['fpn_norm'],
  126. depthwise = cfg['fpn_depthwise']
  127. )
  128. return fpn_net
  129. if __name__ == '__main__':
  130. import time
  131. from thop import profile
  132. cfg = {
  133. 'fpn': 'yolox2_pafpn',
  134. 'fpn_act': 'silu',
  135. 'fpn_norm': 'BN',
  136. 'fpn_depthwise': False,
  137. 'width': 1.0,
  138. 'depth': 1.0,
  139. }
  140. fpn_dims = [256, 512, 1024]
  141. out_dim=256
  142. model = build_fpn(cfg, fpn_dims, out_dim)
  143. pyramid_feats = [torch.randn(1, fpn_dims[0], 80, 80), torch.randn(1, fpn_dims[1], 40, 40), torch.randn(1, fpn_dims[2], 20, 20)]
  144. t0 = time.time()
  145. outputs = model(pyramid_feats)
  146. t1 = time.time()
  147. print('Time: ', t1 - t0)
  148. for out in outputs:
  149. print(out.shape)
  150. print('==============================')
  151. flops, params = profile(model, inputs=(pyramid_feats, ), verbose=False)
  152. print('==============================')
  153. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  154. print('Params : {:.2f} M'.format(params / 1e6))