fairmot.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 ppdet.core.workspace import register, create
  19. from .meta_arch import BaseArch
  20. __all__ = ['FairMOT']
  21. @register
  22. class FairMOT(BaseArch):
  23. """
  24. FairMOT network, see http://arxiv.org/abs/2004.01888
  25. Args:
  26. detector (object): 'CenterNet' instance
  27. reid (object): 'FairMOTEmbeddingHead' instance
  28. tracker (object): 'JDETracker' instance
  29. loss (object): 'FairMOTLoss' instance
  30. """
  31. __category__ = 'architecture'
  32. __inject__ = ['loss']
  33. def __init__(self,
  34. detector='CenterNet',
  35. reid='FairMOTEmbeddingHead',
  36. tracker='JDETracker',
  37. loss='FairMOTLoss'):
  38. super(FairMOT, self).__init__()
  39. self.detector = detector
  40. self.reid = reid
  41. self.tracker = tracker
  42. self.loss = loss
  43. @classmethod
  44. def from_config(cls, cfg, *args, **kwargs):
  45. detector = create(cfg['detector'])
  46. detector_out_shape = detector.neck and detector.neck.out_shape or detector.backbone.out_shape
  47. kwargs = {'input_shape': detector_out_shape}
  48. reid = create(cfg['reid'], **kwargs)
  49. loss = create(cfg['loss'])
  50. tracker = create(cfg['tracker'])
  51. return {
  52. 'detector': detector,
  53. 'reid': reid,
  54. 'loss': loss,
  55. 'tracker': tracker
  56. }
  57. def _forward(self):
  58. loss = dict()
  59. # det_outs keys:
  60. # train: neck_feat, det_loss, heatmap_loss, size_loss, offset_loss (optional: iou_loss)
  61. # eval/infer: neck_feat, bbox, bbox_inds
  62. det_outs = self.detector(self.inputs)
  63. neck_feat = det_outs['neck_feat']
  64. if self.training:
  65. reid_loss = self.reid(neck_feat, self.inputs)
  66. det_loss = det_outs['det_loss']
  67. loss = self.loss(det_loss, reid_loss)
  68. for k, v in det_outs.items():
  69. if 'loss' not in k:
  70. continue
  71. loss.update({k: v})
  72. loss.update({'reid_loss': reid_loss})
  73. return loss
  74. else:
  75. pred_dets, pred_embs = self.reid(
  76. neck_feat, self.inputs, det_outs['bbox'], det_outs['bbox_inds'],
  77. det_outs['topk_clses'])
  78. return pred_dets, pred_embs
  79. def get_pred(self):
  80. output = self._forward()
  81. return output
  82. def get_loss(self):
  83. loss = self._forward()
  84. return loss