lodet_pafpn.py 3.2 KB

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