-
Notifications
You must be signed in to change notification settings - Fork 598
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support tracklet interpolation in ByteTrack (#385)
* support tracklet interpolation * update readme.md * update readme.md * update readme.md * update readme.md * update based on 1-st comments * fix a typo * update docstrings * update docstrings
- Loading branch information
Showing
8 changed files
with
177 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
from .correlation import depthwise_correlation | ||
from .interpolation import interpolate_tracks | ||
from .similarity import embed_similarity | ||
from .transforms import imrenormalize, outs2results, results2outs | ||
|
||
__all__ = [ | ||
'depthwise_correlation', 'outs2results', 'results2outs', | ||
'embed_similarity', 'imrenormalize' | ||
'embed_similarity', 'imrenormalize', 'interpolate_tracks' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import numpy as np | ||
|
||
|
||
def _interpolate_track(track, track_id, max_num_frames=20): | ||
"""Interpolate a track linearly to make the track more complete. | ||
Args: | ||
track (ndarray): With shape (N, 7). Each row denotes | ||
(frame_id, track_id, x1, y1, x2, y2, score). | ||
max_num_frames (int, optional): The maximum disconnected length in the | ||
track. Defaults to 20. | ||
Returns: | ||
ndarray: The interpolated track with shape (N, 7). Each row denotes | ||
(frame_id, track_id, x1, y1, x2, y2, score) | ||
""" | ||
assert (track[:, 1] == track_id).all(), \ | ||
'The track id should not changed when interpolate a track.' | ||
|
||
frame_ids = track[:, 0] | ||
interpolated_track = np.zeros((0, 7)) | ||
# perform interpolation for the disconnected frames in the track. | ||
for i in np.where(np.diff(frame_ids) > 1)[0]: | ||
left_frame_id = frame_ids[i] | ||
right_frame_id = frame_ids[i + 1] | ||
num_disconnected_frames = int(right_frame_id - left_frame_id) | ||
|
||
if 1 < num_disconnected_frames < max_num_frames: | ||
left_bbox = track[i, 2:6] | ||
right_bbox = track[i + 1, 2:6] | ||
|
||
# perform interpolation for two adjacent tracklets. | ||
for j in range(1, num_disconnected_frames): | ||
cur_bbox = j / (num_disconnected_frames) * ( | ||
right_bbox - left_bbox) + left_bbox | ||
cur_result = np.ones((7, )) | ||
cur_result[0] = j + left_frame_id | ||
cur_result[1] = track_id | ||
cur_result[2:6] = cur_bbox | ||
|
||
interpolated_track = np.concatenate( | ||
(interpolated_track, cur_result[None]), axis=0) | ||
|
||
interpolated_track = np.concatenate((track, interpolated_track), axis=0) | ||
return interpolated_track | ||
|
||
|
||
def interpolate_tracks(tracks, min_num_frames=5, max_num_frames=20): | ||
"""Interpolate tracks linearly to make tracks more complete. | ||
This function is proposed in | ||
"ByteTrack: Multi-Object Tracking by Associating Every Detection Box." | ||
`ByteTrack<https://arxiv.org/abs/2110.06864>`_. | ||
Args: | ||
tracks (ndarray): With shape (N, 7). Each row denotes | ||
(frame_id, track_id, x1, y1, x2, y2, score). | ||
min_num_frames (int, optional): The minimum length of a track that will | ||
be interpolated. Defaults to 5. | ||
max_num_frames (int, optional): The maximum disconnected length in | ||
a track. Defaults to 20. | ||
Returns: | ||
ndarray: The interpolated tracks with shape (N, 7). Each row denotes | ||
(frame_id, track_id, x1, y1, x2, y2, score) | ||
""" | ||
max_track_id = int(np.max(tracks[:, 1])) | ||
min_track_id = int(np.min(tracks[:, 1])) | ||
|
||
# perform interpolation for each track | ||
interpolated_tracks = [] | ||
for track_id in range(min_track_id, max_track_id + 1): | ||
inds = tracks[:, 1] == track_id | ||
track = tracks[inds] | ||
num_frames = len(track) | ||
if num_frames <= 2: | ||
continue | ||
|
||
if num_frames > min_num_frames: | ||
interpolated_track = _interpolate_track(track, track_id, | ||
max_num_frames) | ||
else: | ||
interpolated_track = track | ||
interpolated_tracks.append(interpolated_track) | ||
|
||
interpolated_tracks = np.concatenate(interpolated_tracks) | ||
return interpolated_tracks[interpolated_tracks[:, 0].argsort()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
import numpy as np | ||
|
||
|
||
def test_interpolate_tracks(): | ||
from mmtrack.core import interpolate_tracks | ||
frame_id = np.arange(100) // 10 | ||
tracklet_id = np.random.randint(low=1, high=5, size=(100)) | ||
bboxes = np.random.random((100, 4)) * 100 | ||
scores = np.random.random((100)) * 100 | ||
in_results = np.concatenate( | ||
(frame_id[:, None], tracklet_id[:, None], bboxes, scores[:, None]), | ||
axis=1) | ||
out_results = interpolate_tracks(in_results) | ||
assert out_results.shape[1] == in_results.shape[1] | ||
# the range of frame ids should not change | ||
assert min(out_results[:, 0]) == min(in_results[:, 0]) | ||
assert max(out_results[:, 0]) == max(in_results[:, 0]) | ||
# the range of track ids should not change | ||
assert min(out_results[:, 1]) == min(in_results[:, 1]) | ||
assert max(out_results[:, 1]) == max(in_results[:, 1]) |