yolo_free_v2_pafpn.py 3.5 KB

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