jde.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.modeling.mot.utils import scale_coords
  19. from paddlex.ppdet.core.workspace import register, create
  20. from .meta_arch import BaseArch
  21. __all__ = ['JDE']
  22. @register
  23. class JDE(BaseArch):
  24. __category__ = 'architecture'
  25. __shared__ = ['metric']
  26. """
  27. JDE network, see https://arxiv.org/abs/1909.12605v1
  28. Args:
  29. detector (object): detector model instance
  30. reid (object): reid model instance
  31. tracker (object): tracker instance
  32. metric (str): 'MOTDet' for training and detection evaluation, 'ReID'
  33. for ReID embedding evaluation, or 'MOT' for multi object tracking
  34. evaluation。
  35. """
  36. def __init__(self,
  37. detector='YOLOv3',
  38. reid='JDEEmbeddingHead',
  39. tracker='JDETracker',
  40. metric='MOT'):
  41. super(JDE, self).__init__()
  42. self.detector = detector
  43. self.reid = reid
  44. self.tracker = tracker
  45. self.metric = metric
  46. @classmethod
  47. def from_config(cls, cfg, *args, **kwargs):
  48. detector = create(cfg['detector'])
  49. kwargs = {'input_shape': detector.neck.out_shape}
  50. reid = create(cfg['reid'], **kwargs)
  51. tracker = create(cfg['tracker'])
  52. return {
  53. "detector": detector,
  54. "reid": reid,
  55. "tracker": tracker,
  56. }
  57. def _forward(self):
  58. det_outs = self.detector(self.inputs)
  59. if self.training:
  60. emb_feats = det_outs['emb_feats']
  61. loss_confs = det_outs['det_losses']['loss_confs']
  62. loss_boxes = det_outs['det_losses']['loss_boxes']
  63. jde_losses = self.reid(emb_feats, self.inputs, loss_confs,
  64. loss_boxes)
  65. return jde_losses
  66. else:
  67. if self.metric == 'MOTDet':
  68. det_results = {
  69. 'bbox': det_outs['bbox'],
  70. 'bbox_num': det_outs['bbox_num'],
  71. }
  72. return det_results
  73. elif self.metric == 'ReID':
  74. emb_feats = det_outs['emb_feats']
  75. embs_and_gts = self.reid(emb_feats, self.inputs, test_emb=True)
  76. return embs_and_gts
  77. elif self.metric == 'MOT':
  78. emb_feats = det_outs['emb_feats']
  79. emb_outs = self.reid(emb_feats, self.inputs)
  80. boxes_idx = det_outs['boxes_idx']
  81. bbox = det_outs['bbox']
  82. input_shape = self.inputs['image'].shape[2:]
  83. im_shape = self.inputs['im_shape']
  84. scale_factor = self.inputs['scale_factor']
  85. bbox[:, 2:] = scale_coords(bbox[:, 2:], input_shape, im_shape,
  86. scale_factor)
  87. nms_keep_idx = det_outs['nms_keep_idx']
  88. pred_dets = paddle.concat((bbox[:, 2:], bbox[:, 1:2]), axis=1)
  89. emb_valid = paddle.gather_nd(emb_outs, boxes_idx)
  90. pred_embs = paddle.gather_nd(emb_valid, nms_keep_idx)
  91. online_targets = self.tracker.update(pred_dets, pred_embs)
  92. return online_targets
  93. else:
  94. raise ValueError(
  95. "Unknown metric {} for multi object tracking.".format(
  96. self.metric))
  97. def get_loss(self):
  98. return self._forward()
  99. def get_pred(self):
  100. return self._forward()