rtcdet_v2_pafpn.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from .rtcdet_v2_basic import (Conv, build_reduce_layer, build_downsample_layer, build_fpn_block)
  5. # RTCDet-Style PaFPN
  6. class RTCDetPaFPN(nn.Module):
  7. def __init__(self, cfg, in_dims=[256, 512, 1024], out_dim=None):
  8. super(RTCDetPaFPN, self).__init__()
  9. # --------------------------- Basic Parameters ---------------------------
  10. self.in_dims = in_dims
  11. self.fpn_dims = in_dims
  12. # --------------------------- Top-down FPN ---------------------------
  13. ## P5 -> P4
  14. self.reduce_layer_1 = build_reduce_layer(cfg, self.fpn_dims[2], self.fpn_dims[2]//2)
  15. self.top_down_layer_1 = build_fpn_block(cfg, self.fpn_dims[1] + self.fpn_dims[2]//2, self.fpn_dims[1])
  16. ## P4 -> P3
  17. self.reduce_layer_2 = build_reduce_layer(cfg, self.fpn_dims[1], self.fpn_dims[1]//2)
  18. self.top_down_layer_2 = build_fpn_block(cfg, self.fpn_dims[0] + self.fpn_dims[1]//2, self.fpn_dims[0])
  19. # --------------------------- Bottom-up FPN ---------------------------
  20. ## P3 -> P4
  21. self.downsample_layer_1 = build_downsample_layer(cfg, self.fpn_dims[0], self.fpn_dims[0])
  22. self.bottom_up_layer_1 = build_fpn_block(cfg, self.fpn_dims[0] + self.fpn_dims[1]//2, self.fpn_dims[1])
  23. ## P4 -> P5
  24. self.downsample_layer_2 = build_downsample_layer(cfg, self.fpn_dims[1], self.fpn_dims[1])
  25. self.bottom_up_layer_2 = build_fpn_block(cfg, self.fpn_dims[1] + self.fpn_dims[2]//2, self.fpn_dims[2])
  26. # --------------------------- Output proj ---------------------------
  27. if out_dim is not None:
  28. self.out_layers = nn.ModuleList([
  29. Conv(in_dim, out_dim, k=1,
  30. act_type=cfg['fpn_act'], norm_type=cfg['fpn_norm'])
  31. for in_dim in self.fpn_dims
  32. ])
  33. self.out_dim = [out_dim] * 3
  34. else:
  35. self.out_layers = None
  36. self.out_dim = self.fpn_dims
  37. def forward(self, fpn_feats):
  38. c3, c4, c5 = fpn_feats
  39. # Top down
  40. ## P5 -> P4
  41. c6 = self.reduce_layer_1(c5)
  42. c7 = F.interpolate(c6, scale_factor=2.0)
  43. c8 = torch.cat([c7, c4], dim=1)
  44. c9 = self.top_down_layer_1(c8)
  45. ## P4 -> P3
  46. c10 = self.reduce_layer_2(c9)
  47. c11 = F.interpolate(c10, scale_factor=2.0)
  48. c12 = torch.cat([c11, c3], dim=1)
  49. c13 = self.top_down_layer_2(c12)
  50. # Bottom up
  51. ## p3 -> P4
  52. c14 = self.downsample_layer_1(c13)
  53. c15 = torch.cat([c14, c10], dim=1)
  54. c16 = self.bottom_up_layer_1(c15)
  55. ## P4 -> P5
  56. c17 = self.downsample_layer_2(c16)
  57. c18 = torch.cat([c17, c6], dim=1)
  58. c19 = self.bottom_up_layer_2(c18)
  59. out_feats = [c13, c16, c19] # [P3, P4, P5]
  60. # output proj layers
  61. if self.out_layers is not None:
  62. out_feats_proj = []
  63. for feat, layer in zip(out_feats, self.out_layers):
  64. out_feats_proj.append(layer(feat))
  65. return out_feats_proj
  66. return out_feats
  67. def build_fpn(cfg, in_dims, out_dim=None):
  68. model = cfg['fpn']
  69. # build pafpn
  70. if model == 'rtcdet_pafpn':
  71. fpn_net = RTCDetPaFPN(cfg, in_dims, out_dim)
  72. return fpn_net