yolo.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. from ppdet.core.workspace import register, create
  18. from .meta_arch import BaseArch
  19. from ..post_process import JDEBBoxPostProcess
  20. __all__ = ['YOLOv3']
  21. # YOLOv3,PP-YOLO,PP-YOLOv2,PP-YOLOE,PP-YOLOE+ use the same architecture as YOLOv3
  22. # PP-YOLOE and PP-YOLOE+ are recommended to use PPYOLOE architecture in ppyoloe.py
  23. @register
  24. class YOLOv3(BaseArch):
  25. __category__ = 'architecture'
  26. __shared__ = ['data_format']
  27. __inject__ = ['post_process']
  28. def __init__(self,
  29. backbone='DarkNet',
  30. neck='YOLOv3FPN',
  31. yolo_head='YOLOv3Head',
  32. post_process='BBoxPostProcess',
  33. data_format='NCHW',
  34. for_mot=False):
  35. """
  36. YOLOv3 network, see https://arxiv.org/abs/1804.02767
  37. Args:
  38. backbone (nn.Layer): backbone instance
  39. neck (nn.Layer): neck instance
  40. yolo_head (nn.Layer): anchor_head instance
  41. bbox_post_process (object): `BBoxPostProcess` instance
  42. data_format (str): data format, NCHW or NHWC
  43. for_mot (bool): whether return other features for multi-object tracking
  44. models, default False in pure object detection models.
  45. """
  46. super(YOLOv3, self).__init__(data_format=data_format)
  47. self.backbone = backbone
  48. self.neck = neck
  49. self.yolo_head = yolo_head
  50. self.post_process = post_process
  51. self.for_mot = for_mot
  52. self.return_idx = isinstance(post_process, JDEBBoxPostProcess)
  53. @classmethod
  54. def from_config(cls, cfg, *args, **kwargs):
  55. # backbone
  56. backbone = create(cfg['backbone'])
  57. # fpn
  58. kwargs = {'input_shape': backbone.out_shape}
  59. neck = create(cfg['neck'], **kwargs)
  60. # head
  61. kwargs = {'input_shape': neck.out_shape}
  62. yolo_head = create(cfg['yolo_head'], **kwargs)
  63. return {
  64. 'backbone': backbone,
  65. 'neck': neck,
  66. "yolo_head": yolo_head,
  67. }
  68. def _forward(self):
  69. body_feats = self.backbone(self.inputs)
  70. if self.for_mot:
  71. neck_feats = self.neck(body_feats, self.for_mot)
  72. else:
  73. neck_feats = self.neck(body_feats)
  74. if isinstance(neck_feats, dict):
  75. assert self.for_mot == True
  76. emb_feats = neck_feats['emb_feats']
  77. neck_feats = neck_feats['yolo_feats']
  78. if self.training:
  79. yolo_losses = self.yolo_head(neck_feats, self.inputs)
  80. if self.for_mot:
  81. return {'det_losses': yolo_losses, 'emb_feats': emb_feats}
  82. else:
  83. return yolo_losses
  84. else:
  85. yolo_head_outs = self.yolo_head(neck_feats)
  86. if self.for_mot:
  87. # the detection part of JDE MOT model
  88. boxes_idx, bbox, bbox_num, nms_keep_idx = self.post_process(
  89. yolo_head_outs, self.yolo_head.mask_anchors)
  90. output = {
  91. 'bbox': bbox,
  92. 'bbox_num': bbox_num,
  93. 'boxes_idx': boxes_idx,
  94. 'nms_keep_idx': nms_keep_idx,
  95. 'emb_feats': emb_feats,
  96. }
  97. else:
  98. if self.return_idx:
  99. # the detection part of JDE MOT model
  100. _, bbox, bbox_num, _ = self.post_process(
  101. yolo_head_outs, self.yolo_head.mask_anchors)
  102. elif self.post_process is not None:
  103. # anchor based YOLOs: YOLOv3,PP-YOLO,PP-YOLOv2 use mask_anchors
  104. bbox, bbox_num = self.post_process(
  105. yolo_head_outs, self.yolo_head.mask_anchors,
  106. self.inputs['im_shape'], self.inputs['scale_factor'])
  107. else:
  108. # anchor free YOLOs: PP-YOLOE, PP-YOLOE+
  109. bbox, bbox_num = self.yolo_head.post_process(
  110. yolo_head_outs, self.inputs['scale_factor'])
  111. output = {'bbox': bbox, 'bbox_num': bbox_num}
  112. return output
  113. def get_loss(self):
  114. return self._forward()
  115. def get_pred(self):
  116. return self._forward()