fairmot.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. __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: det_loss, heatmap_loss, size_loss, offset_loss, neck_feat
  61. # eval/infer: bbox, bbox_inds, neck_feat
  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. loss.update({
  69. 'heatmap_loss': det_outs['heatmap_loss'],
  70. 'size_loss': det_outs['size_loss'],
  71. 'offset_loss': det_outs['offset_loss'],
  72. 'reid_loss': reid_loss
  73. })
  74. return loss
  75. else:
  76. embedding = self.reid(neck_feat, self.inputs)
  77. bbox_inds = det_outs['bbox_inds']
  78. embedding = paddle.transpose(embedding, [0, 2, 3, 1])
  79. embedding = paddle.reshape(embedding,
  80. [-1, paddle.shape(embedding)[-1]])
  81. pred_embs = paddle.gather(embedding, bbox_inds)
  82. pred_dets = det_outs['bbox']
  83. return pred_dets, pred_embs
  84. def get_pred(self):
  85. output = self._forward()
  86. return output
  87. def get_loss(self):
  88. loss = self._forward()
  89. return loss