deepsort.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright (c) 2021 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. import paddle
  18. from paddlex.ppdet.core.workspace import register, create
  19. from .meta_arch import BaseArch
  20. from paddlex.ppdet.modeling.mot.utils import Detection, get_crops, scale_coords, clip_box
  21. __all__ = ['DeepSORT']
  22. @register
  23. class DeepSORT(BaseArch):
  24. """
  25. DeepSORT network, see https://arxiv.org/abs/1703.07402
  26. Args:
  27. detector (object): detector model instance
  28. reid (object): reid model instance
  29. tracker (object): tracker instance
  30. """
  31. __category__ = 'architecture'
  32. def __init__(self,
  33. detector='YOLOv3',
  34. reid='PCBPyramid',
  35. tracker='DeepSORTTracker'):
  36. super(DeepSORT, self).__init__()
  37. self.detector = detector
  38. self.reid = reid
  39. self.tracker = tracker
  40. @classmethod
  41. def from_config(cls, cfg, *args, **kwargs):
  42. if cfg['detector'] != 'None':
  43. detector = create(cfg['detector'])
  44. else:
  45. detector = None
  46. reid = create(cfg['reid'])
  47. tracker = create(cfg['tracker'])
  48. return {
  49. "detector": detector,
  50. "reid": reid,
  51. "tracker": tracker,
  52. }
  53. def _forward(self):
  54. assert 'ori_image' in self.inputs
  55. load_dets = 'pred_bboxes' in self.inputs and 'pred_scores' in self.inputs
  56. ori_image = self.inputs['ori_image']
  57. input_shape = self.inputs['image'].shape[2:]
  58. im_shape = self.inputs['im_shape']
  59. scale_factor = self.inputs['scale_factor']
  60. if self.detector and not load_dets:
  61. outs = self.detector(self.inputs)
  62. if outs['bbox_num'] > 0:
  63. pred_bboxes = scale_coords(outs['bbox'][:, 2:], input_shape,
  64. im_shape, scale_factor)
  65. pred_scores = outs['bbox'][:, 1:2]
  66. else:
  67. pred_bboxes = []
  68. pred_scores = []
  69. else:
  70. pred_bboxes = self.inputs['pred_bboxes']
  71. pred_scores = self.inputs['pred_scores']
  72. if len(pred_bboxes) > 0:
  73. pred_bboxes = clip_box(pred_bboxes, input_shape, im_shape,
  74. scale_factor)
  75. bbox_tlwh = paddle.concat(
  76. (pred_bboxes[:, 0:2],
  77. pred_bboxes[:, 2:4] - pred_bboxes[:, 0:2] + 1),
  78. axis=1)
  79. crops, pred_scores = get_crops(
  80. pred_bboxes, ori_image, pred_scores, w=64, h=192)
  81. if len(crops) > 0:
  82. features = self.reid(paddle.to_tensor(crops))
  83. detections = [Detection(bbox_tlwh[i], conf, features[i])\
  84. for i, conf in enumerate(pred_scores)]
  85. else:
  86. detections = []
  87. else:
  88. detections = []
  89. self.tracker.predict()
  90. online_targets = self.tracker.update(detections)
  91. return online_targets
  92. def get_pred(self):
  93. return self._forward()