Reference for ultralytics/trackers/utils/matching.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/trackers/utils/matching.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.trackers.utils.matching.linear_assignment
Perform linear assignment using either the scipy or lap.lapjv method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cost_matrix
|
ndarray
|
The matrix containing cost values for assignments, with shape (N, M). |
required |
thresh
|
float
|
Threshold for considering an assignment valid. |
required |
use_lap
|
bool
|
Use lap.lapjv for the assignment. If False, scipy.optimize.linear_sum_assignment is used. |
True
|
Returns:
Type | Description |
---|---|
tuple
|
A tuple containing: - matched_indices (np.ndarray): Array of matched indices of shape (K, 2), where K is the number of matches. - unmatched_a (np.ndarray): Array of unmatched indices from the first set, with shape (L,). - unmatched_b (np.ndarray): Array of unmatched indices from the second set, with shape (M,). |
Examples:
>>> cost_matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> thresh = 5.0
>>> matched_indices, unmatched_a, unmatched_b = linear_assignment(cost_matrix, thresh, use_lap=True)
Source code in ultralytics/trackers/utils/matching.py
ultralytics.trackers.utils.matching.iou_distance
Compute cost based on Intersection over Union (IoU) between tracks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
atracks
|
list[STrack] | list[ndarray]
|
List of tracks 'a' or bounding boxes. |
required |
btracks
|
list[STrack] | list[ndarray]
|
List of tracks 'b' or bounding boxes. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Cost matrix computed based on IoU. |
Examples:
Compute IoU distance between two sets of tracks
>>> atracks = [np.array([0, 0, 10, 10]), np.array([20, 20, 30, 30])]
>>> btracks = [np.array([5, 5, 15, 15]), np.array([25, 25, 35, 35])]
>>> cost_matrix = iou_distance(atracks, btracks)
Source code in ultralytics/trackers/utils/matching.py
ultralytics.trackers.utils.matching.embedding_distance
Compute distance between tracks and detections based on embeddings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tracks
|
list[STrack]
|
List of tracks, where each track contains embedding features. |
required |
detections
|
list[BaseTrack]
|
List of detections, where each detection contains embedding features. |
required |
metric
|
str
|
Metric for distance computation. Supported metrics include 'cosine', 'euclidean', etc. |
'cosine'
|
Returns:
Type | Description |
---|---|
ndarray
|
Cost matrix computed based on embeddings with shape (N, M), where N is the number of tracks and M is the number of detections. |
Examples:
Compute the embedding distance between tracks and detections using cosine metric
>>> tracks = [STrack(...), STrack(...)] # List of track objects with embedding features
>>> detections = [BaseTrack(...), BaseTrack(...)] # List of detection objects with embedding features
>>> cost_matrix = embedding_distance(tracks, detections, metric="cosine")
Source code in ultralytics/trackers/utils/matching.py
ultralytics.trackers.utils.matching.fuse_score
Fuses cost matrix with detection scores to produce a single similarity matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cost_matrix
|
ndarray
|
The matrix containing cost values for assignments, with shape (N, M). |
required |
detections
|
list[BaseTrack]
|
List of detections, each containing a score attribute. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Fused similarity matrix with shape (N, M). |
Examples:
Fuse a cost matrix with detection scores
>>> cost_matrix = np.random.rand(5, 10) # 5 tracks and 10 detections
>>> detections = [BaseTrack(score=np.random.rand()) for _ in range(10)]
>>> fused_matrix = fuse_score(cost_matrix, detections)