matching.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import cv2
  2. import numpy as np
  3. import scipy
  4. import lap
  5. from scipy.spatial.distance import cdist
  6. from .kalman_filter import chi2inv95
  7. import time
  8. def merge_matches(m1, m2, shape):
  9. O,P,Q = shape
  10. m1 = np.asarray(m1)
  11. m2 = np.asarray(m2)
  12. M1 = scipy.sparse.coo_matrix((np.ones(len(m1)), (m1[:, 0], m1[:, 1])), shape=(O, P))
  13. M2 = scipy.sparse.coo_matrix((np.ones(len(m2)), (m2[:, 0], m2[:, 1])), shape=(P, Q))
  14. mask = M1*M2
  15. match = mask.nonzero()
  16. match = list(zip(match[0], match[1]))
  17. unmatched_O = tuple(set(range(O)) - set([i for i, j in match]))
  18. unmatched_Q = tuple(set(range(Q)) - set([j for i, j in match]))
  19. return match, unmatched_O, unmatched_Q
  20. def _indices_to_matches(cost_matrix, indices, thresh):
  21. matched_cost = cost_matrix[tuple(zip(*indices))]
  22. matched_mask = (matched_cost <= thresh)
  23. matches = indices[matched_mask]
  24. unmatched_a = tuple(set(range(cost_matrix.shape[0])) - set(matches[:, 0]))
  25. unmatched_b = tuple(set(range(cost_matrix.shape[1])) - set(matches[:, 1]))
  26. return matches, unmatched_a, unmatched_b
  27. def linear_assignment(cost_matrix, thresh):
  28. if cost_matrix.size == 0:
  29. return np.empty((0, 2), dtype=int), tuple(range(cost_matrix.shape[0])), tuple(range(cost_matrix.shape[1]))
  30. matches, unmatched_a, unmatched_b = [], [], []
  31. cost, x, y = lap.lapjv(cost_matrix, extend_cost=True, cost_limit=thresh)
  32. for ix, mx in enumerate(x):
  33. if mx >= 0:
  34. matches.append([ix, mx])
  35. unmatched_a = np.where(x < 0)[0]
  36. unmatched_b = np.where(y < 0)[0]
  37. matches = np.asarray(matches)
  38. return matches, unmatched_a, unmatched_b
  39. def ious(axyxys, bxyxys):
  40. """
  41. Compute cost based on IoU
  42. :type axyxys: list[xyxy] | np.ndarray
  43. :type axyxys: list[xyxy] | np.ndarray
  44. :rtype ious np.ndarray
  45. """
  46. ious = np.zeros((len(axyxys), len(bxyxys)), dtype=np.float)
  47. if ious.size == 0:
  48. return ious
  49. axyxys = np.ascontiguousarray(axyxys, dtype=np.float)
  50. bxyxys = np.ascontiguousarray(bxyxys, dtype=np.float)
  51. area1 = (axyxys[:, 2] - axyxys[:, 0]) * (axyxys[:, 3] - axyxys[:, 1])
  52. area2 = (bxyxys[:, 2] - bxyxys[:, 0]) * (bxyxys[:, 3] - bxyxys[:, 1])
  53. lt = np.maximum(axyxys[:, None, :2], bxyxys[:, :2]) # [N,M,2]
  54. rb = np.minimum(axyxys[:, None, 2:], bxyxys[:, 2:]) # [N,M,2]
  55. wh = np.clip(rb - lt, a_min=0, a_max=1e4) # [N,M,2]
  56. inter = wh[:, :, 0] * wh[:, :, 1] # [N,M]
  57. union = area1[:, None] + area2 - inter
  58. iou = inter / union
  59. return iou
  60. def iou_distance(atracks, btracks):
  61. """
  62. Compute cost based on IoU
  63. :type atracks: list[STrack]
  64. :type btracks: list[STrack]
  65. :rtype cost_matrix np.ndarray
  66. """
  67. if (len(atracks)>0 and isinstance(atracks[0], np.ndarray)) or (len(btracks) > 0 and isinstance(btracks[0], np.ndarray)):
  68. axyxys = atracks
  69. bxyxys = btracks
  70. else:
  71. axyxys = [track.xyxy for track in atracks]
  72. bxyxys = [track.xyxy for track in btracks]
  73. _ious = ious(axyxys, bxyxys)
  74. cost_matrix = 1 - _ious
  75. return cost_matrix
  76. def v_iou_distance(atracks, btracks):
  77. """
  78. Compute cost based on IoU
  79. :type atracks: list[STrack]
  80. :type btracks: list[STrack]
  81. :rtype cost_matrix np.ndarray
  82. """
  83. if (len(atracks)>0 and isinstance(atracks[0], np.ndarray)) or (len(btracks) > 0 and isinstance(btracks[0], np.ndarray)):
  84. axyxys = atracks
  85. bxyxys = btracks
  86. else:
  87. axyxys = [track.xywh_to_xyxy(track.pred_bbox) for track in atracks]
  88. bxyxys = [track.xywh_to_xyxy(track.pred_bbox) for track in btracks]
  89. _ious = ious(axyxys, bxyxys)
  90. cost_matrix = 1 - _ious
  91. return cost_matrix
  92. def embedding_distance(tracks, detections, metric='cosine'):
  93. """
  94. :param tracks: list[STrack]
  95. :param detections: list[BaseTrack]
  96. :param metric:
  97. :return: cost_matrix np.ndarray
  98. """
  99. cost_matrix = np.zeros((len(tracks), len(detections)), dtype=np.float)
  100. if cost_matrix.size == 0:
  101. return cost_matrix
  102. det_features = np.asarray([track.curr_feat for track in detections], dtype=np.float)
  103. #for i, track in enumerate(tracks):
  104. #cost_matrix[i, :] = np.maximum(0.0, cdist(track.smooth_feat.reshape(1,-1), det_features, metric))
  105. track_features = np.asarray([track.smooth_feat for track in tracks], dtype=np.float)
  106. cost_matrix = np.maximum(0.0, cdist(track_features, det_features, metric)) # Nomalized features
  107. return cost_matrix
  108. def gate_cost_matrix(kf, cost_matrix, tracks, detections, only_position=False):
  109. if cost_matrix.size == 0:
  110. return cost_matrix
  111. gating_dim = 2 if only_position else 4
  112. gating_threshold = chi2inv95[gating_dim]
  113. measurements = np.asarray([det.to_xyah() for det in detections])
  114. for row, track in enumerate(tracks):
  115. gating_distance = kf.gating_distance(
  116. track.mean, track.covariance, measurements, only_position)
  117. cost_matrix[row, gating_distance > gating_threshold] = np.inf
  118. return cost_matrix
  119. def fuse_motion(kf, cost_matrix, tracks, detections, only_position=False, lambda_=0.98):
  120. if cost_matrix.size == 0:
  121. return cost_matrix
  122. gating_dim = 2 if only_position else 4
  123. gating_threshold = chi2inv95[gating_dim]
  124. measurements = np.asarray([det.to_xyah() for det in detections])
  125. for row, track in enumerate(tracks):
  126. gating_distance = kf.gating_distance(
  127. track.mean, track.covariance, measurements, only_position, metric='maha')
  128. cost_matrix[row, gating_distance > gating_threshold] = np.inf
  129. cost_matrix[row] = lambda_ * cost_matrix[row] + (1 - lambda_) * gating_distance
  130. return cost_matrix
  131. def fuse_iou(cost_matrix, tracks, detections):
  132. if cost_matrix.size == 0:
  133. return cost_matrix
  134. reid_sim = 1 - cost_matrix
  135. iou_dist = iou_distance(tracks, detections)
  136. iou_sim = 1 - iou_dist
  137. fuse_sim = reid_sim * (1 + iou_sim) / 2
  138. det_scores = np.array([det.score for det in detections])
  139. det_scores = np.expand_dims(det_scores, axis=0).repeat(cost_matrix.shape[0], axis=0)
  140. #fuse_sim = fuse_sim * (1 + det_scores) / 2
  141. fuse_cost = 1 - fuse_sim
  142. return fuse_cost
  143. def fuse_score(cost_matrix, detections):
  144. if cost_matrix.size == 0:
  145. return cost_matrix
  146. iou_sim = 1 - cost_matrix
  147. det_scores = np.array([det.score for det in detections])
  148. det_scores = np.expand_dims(det_scores, axis=0).repeat(cost_matrix.shape[0], axis=0)
  149. fuse_sim = iou_sim * det_scores
  150. fuse_cost = 1 - fuse_sim
  151. return fuse_cost