Skip to content

Commit

Permalink
added Q Scaling for DeepOCSort with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edblu1 committed Jul 2, 2024
1 parent 9eb8bb1 commit 3820896
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
2 changes: 2 additions & 0 deletions boxmot/configs/deepocsort.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ iou_thresh: 0.3
max_age: 30
min_hits: 1
w_association_emb: 0.75
Q_xy_scaling: 0.01
Q_s_scaling: 0.0001
2 changes: 2 additions & 0 deletions boxmot/tracker_zoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def create_tracker(tracker_type, tracker_config, reid_weights, device, half, per
delta_t=cfg.delta_t,
asso_func=cfg.asso_func,
inertia=cfg.inertia,
Q_xy_scaling=cfg.Q_xy_scaling,
Q_s_scaling=cfg.Q_s_scaling
)
return deepocsort
elif tracker_type == 'hybridsort':
Expand Down
16 changes: 13 additions & 3 deletions boxmot/trackers/deepocsort/deep_ocsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class KalmanBoxTracker(object):

count = 0

def __init__(self, det, delta_t=3, emb=None, alpha=0, max_obs=50):
def __init__(self, det, delta_t=3, emb=None, alpha=0, max_obs=50, Q_xy_scaling = 0.01, Q_s_scaling = 0.0001):
"""
Initialises a tracker using initial bounding box.
Expand All @@ -65,6 +65,9 @@ def __init__(self, det, delta_t=3, emb=None, alpha=0, max_obs=50):
self.cls = det[5]
self.det_ind = det[6]

self.Q_xy_scaling = Q_xy_scaling
self.Q_s_scaling = Q_s_scaling

self.kf = KalmanFilterXYSR(dim_x=7, dim_z=4)
self.kf.F = np.array(
[
Expand All @@ -89,8 +92,9 @@ def __init__(self, det, delta_t=3, emb=None, alpha=0, max_obs=50):
self.kf.R[2:, 2:] *= 10.0
self.kf.P[4:, 4:] *= 1000.0 # give high uncertainty to the unobservable initial velocities
self.kf.P *= 10.0
self.kf.Q[-1, -1] *= 0.01
self.kf.Q[4:, 4:] *= 0.01
self.kf.Q[4:6, 4:6] *= self.Q_xy_scaling
self.kf.Q[-1, -1] *= self.Q_s_scaling

self.bbox_to_z_func = xyxy2xysr
self.x_to_bbox_func = convert_x_to_bbox

Expand Down Expand Up @@ -238,6 +242,8 @@ def __init__(
embedding_off=False,
cmc_off=False,
aw_off=False,
Q_xy_scaling=0.01,
Q_s_scaling=0.0001,
**kwargs
):
super().__init__(max_age=max_age)
Expand All @@ -255,6 +261,8 @@ def __init__(
self.alpha_fixed_emb = alpha_fixed_emb
self.aw_param = aw_param
self.per_class = per_class
self.Q_xy_scaling = Q_xy_scaling
self.Q_s_scaling = Q_s_scaling
KalmanBoxTracker.count = 1

rab = ReidAutoBackend(
Expand Down Expand Up @@ -411,6 +419,8 @@ def update(self, dets: np.ndarray, img: np.ndarray, embs: np.ndarray = None) ->
delta_t=self.delta_t,
emb=dets_embs[i],
alpha=dets_alpha[i],
Q_xy_scaling=self.Q_xy_scaling,
Q_s_scaling=self.Q_s_scaling,
max_obs=self.max_obs
)
self.active_tracks.append(trk)
Expand Down
63 changes: 48 additions & 15 deletions tests/unit/test_trackers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
from boxmot import (
StrongSORT, BoTSORT, DeepOCSORT, OCSORT, BYTETracker, get_tracker_config, create_tracker,
)
from boxmot.trackers.ocsort.ocsort import KalmanBoxTracker

from boxmot.trackers.ocsort.ocsort import KalmanBoxTracker as OCSortKalmanBoxTracker
from boxmot.trackers.deepocsort.deep_ocsort import KalmanBoxTracker as DeepOCSortKalmanBoxTracker



MOTION_ONLY_TRACKING_METHODS=[OCSORT, BYTETracker]
Expand Down Expand Up @@ -60,29 +63,59 @@ def test_dynamic_max_obs_based_on_max_age():
assert ocsort.max_obs == (max_age + 5)


def test_Q_matrix_scaling():
bbox = [0, 0, 100, 100, 0.9]
def create_kalman_box_tracker_ocsort(bbox, cls, det_ind, tracker):
return OCSortKalmanBoxTracker(
bbox,
cls,
det_ind,
Q_xy_scaling=tracker.Q_xy_scaling,
Q_s_scaling=tracker.Q_s_scaling
)


def create_kalman_box_tracker_deepocsort(bbox, cls, det_ind, tracker):
# DeepOCSort KalmanBoxTracker expects input in different format than OCSort
det = np.concatenate([bbox, [cls, det_ind]])
return DeepOCSortKalmanBoxTracker(
det,
Q_xy_scaling=tracker.Q_xy_scaling,
Q_s_scaling=tracker.Q_s_scaling
)


TRACKER_CREATORS = {
OCSORT: create_kalman_box_tracker_ocsort,
DeepOCSORT: create_kalman_box_tracker_deepocsort,
}


@pytest.mark.parametrize("Tracker, init_args", [
(OCSORT, {}),
(DeepOCSORT, {
'model_weights': Path(WEIGHTS / 'osnet_x0_25_msmt17.pt'),
'device': 'cpu',
'fp16': True
}),
])
def test_Q_matrix_scaling(Tracker, init_args):
bbox = np.array([0, 0, 100, 100, 0.9])
cls = 1
det_ind = 0
Q_xy_scaling = 0.05
Q_s_scaling = 0.0005

ocsort = OCSORT(
tracker = Tracker(
Q_xy_scaling=Q_xy_scaling,
Q_s_scaling=Q_s_scaling
Q_s_scaling=Q_s_scaling,
**init_args
)

kalman_box_tracker = KalmanBoxTracker(
bbox,
cls,
det_ind,
Q_xy_scaling=ocsort.Q_xy_scaling,
Q_s_scaling=ocsort.Q_s_scaling
)
create_kalman_box_tracker = TRACKER_CREATORS[Tracker]
kalman_box_tracker = create_kalman_box_tracker(bbox, cls, det_ind, tracker)

assert kalman_box_tracker.kf.Q[4, 4] == Q_xy_scaling
assert kalman_box_tracker.kf.Q[5, 5] == Q_xy_scaling
assert kalman_box_tracker.kf.Q[6, 6] == Q_s_scaling
assert kalman_box_tracker.kf.Q[4, 4] == Q_xy_scaling, "Q_xy scaling incorrect for x' velocity"
assert kalman_box_tracker.kf.Q[5, 5] == Q_xy_scaling, "Q_xy scaling incorrect for y' velocity"
assert kalman_box_tracker.kf.Q[6, 6] == Q_s_scaling, "Q_s scaling incorrect for s' (scale) velocity"


@pytest.mark.parametrize("tracker_type", PER_CLASS_TRACKERS)
Expand Down

0 comments on commit 3820896

Please sign in to comment.