ocsort.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. from typing import Dict, Optional
  3. from torch import Tensor
  4. from mmdet.registry import MODELS
  5. from mmdet.structures import TrackSampleList
  6. from mmdet.utils import OptConfigType, OptMultiConfig
  7. from .base import BaseMOTModel
  8. @MODELS.register_module()
  9. class OCSORT(BaseMOTModel):
  10. """OCOSRT: Observation-Centric SORT: Rethinking SORT for Robust
  11. Multi-Object Tracking
  12. This multi object tracker is the implementation of `OC-SORT
  13. <https://arxiv.org/abs/2203.14360>`_.
  14. Args:
  15. detector (dict): Configuration of detector. Defaults to None.
  16. tracker (dict): Configuration of tracker. Defaults to None.
  17. motion (dict): Configuration of motion. Defaults to None.
  18. init_cfg (dict): Configuration of initialization. Defaults to None.
  19. """
  20. def __init__(self,
  21. detector: Optional[dict] = None,
  22. tracker: Optional[dict] = None,
  23. data_preprocessor: OptConfigType = None,
  24. init_cfg: OptMultiConfig = None):
  25. super().__init__(data_preprocessor, init_cfg)
  26. if detector is not None:
  27. self.detector = MODELS.build(detector)
  28. if tracker is not None:
  29. self.tracker = MODELS.build(tracker)
  30. def loss(self, inputs: Tensor, data_samples: TrackSampleList,
  31. **kwargs) -> dict:
  32. """Calculate losses from a batch of inputs and data samples."""
  33. return self.detector.loss(inputs, data_samples, **kwargs)
  34. def predict(self, inputs: Dict[str, Tensor], data_samples: TrackSampleList,
  35. **kwargs) -> TrackSampleList:
  36. """Predict results from a video and data samples with post-processing.
  37. Args:
  38. inputs (Tensor): of shape (N, T, C, H, W) encoding
  39. input images. The N denotes batch size.
  40. The T denotes the number of frames in a video.
  41. data_samples (list[:obj:`TrackDataSample`]): The batch
  42. data samples. It usually includes information such
  43. as `video_data_samples`.
  44. Returns:
  45. TrackSampleList: Tracking results of the inputs.
  46. """
  47. assert inputs.dim() == 5, 'The img must be 5D Tensor (N, T, C, H, W).'
  48. assert inputs.size(0) == 1, \
  49. 'OCSORT inference only support ' \
  50. '1 batch size per gpu for now.'
  51. assert len(data_samples) == 1, \
  52. 'OCSORT inference only support 1 batch size per gpu for now.'
  53. track_data_sample = data_samples[0]
  54. video_len = len(track_data_sample)
  55. for frame_id in range(video_len):
  56. img_data_sample = track_data_sample[frame_id]
  57. single_img = inputs[:, frame_id].contiguous()
  58. # det_results List[DetDataSample]
  59. det_results = self.detector.predict(single_img, [img_data_sample])
  60. assert len(det_results) == 1, 'Batch inference is not supported.'
  61. pred_track_instances = self.tracker.track(
  62. data_sample=det_results[0], **kwargs)
  63. img_data_sample.pred_track_instances = pred_track_instances
  64. return [track_data_sample]