yolov5_pafpn.py 3.2 KB

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