matching.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. import numpy as np
  3. import scipy
  4. from scipy.spatial.distance import cdist
  5. from ultralytics.utils.metrics import bbox_ioa
  6. try:
  7. import lap # for linear_assignment
  8. assert lap.__version__ # verify package is not directory
  9. except (ImportError, AssertionError, AttributeError):
  10. from ultralytics.utils.checks import check_requirements
  11. check_requirements('lapx>=0.5.2') # update to lap package from https://github.com/rathaROG/lapx
  12. import lap
  13. def linear_assignment(cost_matrix, thresh, use_lap=True):
  14. """
  15. Perform linear assignment using scipy or lap.lapjv.
  16. Args:
  17. cost_matrix (np.ndarray): The matrix containing cost values for assignments.
  18. thresh (float): Threshold for considering an assignment valid.
  19. use_lap (bool, optional): Whether to use lap.lapjv. Defaults to True.
  20. Returns:
  21. (tuple): Tuple containing matched indices, unmatched indices from 'a', and unmatched indices from 'b'.
  22. """
  23. if cost_matrix.size == 0:
  24. return np.empty((0, 2), dtype=int), tuple(range(cost_matrix.shape[0])), tuple(range(cost_matrix.shape[1]))
  25. if use_lap:
  26. # https://github.com/gatagat/lap
  27. _, x, y = lap.lapjv(cost_matrix, extend_cost=True, cost_limit=thresh)
  28. matches = [[ix, mx] for ix, mx in enumerate(x) if mx >= 0]
  29. unmatched_a = np.where(x < 0)[0]
  30. unmatched_b = np.where(y < 0)[0]
  31. else:
  32. # https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.linear_sum_assignment.html
  33. x, y = scipy.optimize.linear_sum_assignment(cost_matrix) # row x, col y
  34. matches = np.asarray([[x[i], y[i]] for i in range(len(x)) if cost_matrix[x[i], y[i]] <= thresh])
  35. if len(matches) == 0:
  36. unmatched_a = list(np.arange(cost_matrix.shape[0]))
  37. unmatched_b = list(np.arange(cost_matrix.shape[1]))
  38. else:
  39. unmatched_a = list(set(np.arange(cost_matrix.shape[0])) - set(matches[:, 0]))
  40. unmatched_b = list(set(np.arange(cost_matrix.shape[1])) - set(matches[:, 1]))
  41. return matches, unmatched_a, unmatched_b
  42. def iou_distance(atracks, btracks):
  43. """
  44. Compute cost based on Intersection over Union (IoU) between tracks.
  45. Args:
  46. atracks (list[STrack] | list[np.ndarray]): List of tracks 'a' or bounding boxes.
  47. btracks (list[STrack] | list[np.ndarray]): List of tracks 'b' or bounding boxes.
  48. Returns:
  49. (np.ndarray): Cost matrix computed based on IoU.
  50. """
  51. if (len(atracks) > 0 and isinstance(atracks[0], np.ndarray)) \
  52. or (len(btracks) > 0 and isinstance(btracks[0], np.ndarray)):
  53. atlbrs = atracks
  54. btlbrs = btracks
  55. else:
  56. atlbrs = [track.tlbr for track in atracks]
  57. btlbrs = [track.tlbr for track in btracks]
  58. ious = np.zeros((len(atlbrs), len(btlbrs)), dtype=np.float32)
  59. if len(atlbrs) and len(btlbrs):
  60. ious = bbox_ioa(np.ascontiguousarray(atlbrs, dtype=np.float32),
  61. np.ascontiguousarray(btlbrs, dtype=np.float32),
  62. iou=True)
  63. return 1 - ious # cost matrix
  64. def embedding_distance(tracks, detections, metric='cosine'):
  65. """
  66. Compute distance between tracks and detections based on embeddings.
  67. Args:
  68. tracks (list[STrack]): List of tracks.
  69. detections (list[BaseTrack]): List of detections.
  70. metric (str, optional): Metric for distance computation. Defaults to 'cosine'.
  71. Returns:
  72. (np.ndarray): Cost matrix computed based on embeddings.
  73. """
  74. cost_matrix = np.zeros((len(tracks), len(detections)), dtype=np.float32)
  75. if cost_matrix.size == 0:
  76. return cost_matrix
  77. det_features = np.asarray([track.curr_feat for track in detections], dtype=np.float32)
  78. # for i, track in enumerate(tracks):
  79. # cost_matrix[i, :] = np.maximum(0.0, cdist(track.smooth_feat.reshape(1,-1), det_features, metric))
  80. track_features = np.asarray([track.smooth_feat for track in tracks], dtype=np.float32)
  81. cost_matrix = np.maximum(0.0, cdist(track_features, det_features, metric)) # Normalized features
  82. return cost_matrix
  83. def fuse_score(cost_matrix, detections):
  84. """
  85. Fuses cost matrix with detection scores to produce a single similarity matrix.
  86. Args:
  87. cost_matrix (np.ndarray): The matrix containing cost values for assignments.
  88. detections (list[BaseTrack]): List of detections with scores.
  89. Returns:
  90. (np.ndarray): Fused similarity matrix.
  91. """
  92. if cost_matrix.size == 0:
  93. return cost_matrix
  94. iou_sim = 1 - cost_matrix
  95. det_scores = np.array([det.score for det in detections])
  96. det_scores = np.expand_dims(det_scores, axis=0).repeat(cost_matrix.shape[0], axis=0)
  97. fuse_sim = iou_sim * det_scores
  98. return 1 - fuse_sim # fuse_cost