yolov5_plus_pafpn.py 3.3 KB

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