yolo.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from __future__ import absolute_import
  2. from __future__ import division
  3. from __future__ import print_function
  4. from paddlex.ppdet.core.workspace import register, create
  5. from .meta_arch import BaseArch
  6. __all__ = ['YOLOv3']
  7. @register
  8. class YOLOv3(BaseArch):
  9. __category__ = 'architecture'
  10. __shared__ = ['data_format']
  11. __inject__ = ['post_process']
  12. def __init__(self,
  13. backbone='DarkNet',
  14. neck='YOLOv3FPN',
  15. yolo_head='YOLOv3Head',
  16. post_process='BBoxPostProcess',
  17. data_format='NCHW',
  18. for_mot=False):
  19. """
  20. YOLOv3 network, see https://arxiv.org/abs/1804.02767
  21. Args:
  22. backbone (nn.Layer): backbone instance
  23. neck (nn.Layer): neck instance
  24. yolo_head (nn.Layer): anchor_head instance
  25. bbox_post_process (object): `BBoxPostProcess` instance
  26. data_format (str): data format, NCHW or NHWC
  27. for_mot (bool): whether return other features for multi-object tracking
  28. models, default False in pure object detection models.
  29. """
  30. super(YOLOv3, self).__init__(data_format=data_format)
  31. self.backbone = backbone
  32. self.neck = neck
  33. self.yolo_head = yolo_head
  34. self.post_process = post_process
  35. self.for_mot = for_mot
  36. @classmethod
  37. def from_config(cls, cfg, *args, **kwargs):
  38. # backbone
  39. backbone = create(cfg['backbone'])
  40. # fpn
  41. kwargs = {'input_shape': backbone.out_shape}
  42. neck = create(cfg['neck'], **kwargs)
  43. # head
  44. kwargs = {'input_shape': neck.out_shape}
  45. yolo_head = create(cfg['yolo_head'], **kwargs)
  46. return {
  47. 'backbone': backbone,
  48. 'neck': neck,
  49. "yolo_head": yolo_head,
  50. }
  51. def _forward(self):
  52. body_feats = self.backbone(self.inputs)
  53. neck_feats = self.neck(body_feats, self.for_mot)
  54. if isinstance(neck_feats, dict):
  55. assert self.for_mot == True
  56. emb_feats = neck_feats['emb_feats']
  57. neck_feats = neck_feats['yolo_feats']
  58. if self.training:
  59. yolo_losses = self.yolo_head(neck_feats, self.inputs)
  60. if self.for_mot:
  61. return {'det_losses': yolo_losses, 'emb_feats': emb_feats}
  62. else:
  63. return yolo_losses
  64. else:
  65. yolo_head_outs = self.yolo_head(neck_feats)
  66. if self.for_mot:
  67. boxes_idx, bbox, bbox_num, nms_keep_idx = self.post_process(
  68. yolo_head_outs, self.yolo_head.mask_anchors)
  69. output = {
  70. 'bbox': bbox,
  71. 'bbox_num': bbox_num,
  72. 'boxes_idx': boxes_idx,
  73. 'nms_keep_idx': nms_keep_idx,
  74. 'emb_feats': emb_feats,
  75. }
  76. else:
  77. bbox, bbox_num = self.post_process(
  78. yolo_head_outs, self.yolo_head.mask_anchors,
  79. self.inputs['im_shape'], self.inputs['scale_factor'])
  80. output = {'bbox': bbox, 'bbox_num': bbox_num}
  81. return output
  82. def get_loss(self):
  83. return self._forward()
  84. def get_pred(self):
  85. return self._forward()