yolov5_plus_pafpn.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from .yolov5_plus_basic import (Conv, 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.top_down_layer_1 = build_fpn_block(cfg, c4 + c5, round(512*width))
  18. ### P4 -> P3
  19. self.top_down_layer_2 = build_fpn_block(cfg, c3 + round(512*width), round(256*width))
  20. ## bottom up
  21. ### P3 -> P4
  22. self.downsample_layer_1 = build_downsample_layer(cfg, round(256*width), round(256*width))
  23. self.bottom_up_layer_1 = build_fpn_block(cfg, round(256*width) + round(512*width), round(512*width))
  24. ### P4 -> P5
  25. self.downsample_layer_2 = build_downsample_layer(cfg, round(512*width), round(512*width))
  26. self.bottom_up_layer_2 = build_fpn_block(cfg, c5 + round(512*width), round(512*width*ratio))
  27. ## output proj layers
  28. if out_dim is not None:
  29. self.out_layers = nn.ModuleList([
  30. Conv(in_dim, out_dim, k=1,
  31. act_type=cfg['fpn_act'], norm_type=cfg['fpn_norm'])
  32. for in_dim in [round(256*width), round(512*width), round(512*width*ratio)]
  33. ])
  34. self.out_dim = [out_dim] * 3
  35. else:
  36. self.out_layers = None
  37. self.out_dim = [round(256*width), round(512*width), round(512*width*ratio)]
  38. def forward(self, features):
  39. c3, c4, c5 = features
  40. # Top down
  41. ## P5 -> P4
  42. c6 = F.interpolate(c5, scale_factor=2.0)
  43. c7 = torch.cat([c6, c4], dim=1)
  44. c8 = self.top_down_layer_1(c7)
  45. ## P4 -> P3
  46. c9 = F.interpolate(c8, scale_factor=2.0)
  47. c10 = torch.cat([c9, c3], dim=1)
  48. c11 = self.top_down_layer_2(c10)
  49. # Bottom up
  50. ## p3 -> P4
  51. c12 = self.downsample_layer_1(c11)
  52. c13 = torch.cat([c12, c8], dim=1)
  53. c14 = self.bottom_up_layer_1(c13)
  54. ## P4 -> P5
  55. c15 = self.downsample_layer_2(c14)
  56. c16 = torch.cat([c15, c5], dim=1)
  57. c17 = self.bottom_up_layer_2(c16)
  58. out_feats = [c11, c14, c17] # [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 == 'yolov5_plus_pafpn':
  70. fpn_net = Yolov5PlusPaFPN(cfg, in_dims, out_dim)
  71. return fpn_net