Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starwit q scaling #1506

Merged
merged 12 commits into from
Jul 2, 2024
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/configs/ocsort.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ iou_thresh: 0.3
max_age: 30
min_hits: 1
use_byte: false
Q_xy_scaling: 0.01
Q_s_scaling: 0.0001
4 changes: 4 additions & 0 deletions boxmot/tracker_zoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def create_tracker(tracker_type, tracker_config, reid_weights, device, half, per
asso_func=cfg.asso_func,
inertia=cfg.inertia,
use_byte=cfg.use_byte,
Q_xy_scaling=cfg.Q_xy_scaling,
Q_s_scaling=cfg.Q_s_scaling
)
return ocsort

Expand Down Expand Up @@ -97,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
19 changes: 14 additions & 5 deletions boxmot/trackers/ocsort/ocsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ class KalmanBoxTracker(object):

count = 0

def __init__(self, bbox, cls, det_ind, delta_t=3, max_obs=50):
def __init__(self, bbox, cls, det_ind, delta_t=3, max_obs=50, Q_xy_scaling = 0.01, Q_s_scaling = 0.0001):
"""
Initialises a tracker using initial bounding box.

"""
# define constant velocity model
self.det_ind = det_ind

self.Q_xy_scaling = Q_xy_scaling
self.Q_s_scaling = Q_s_scaling

self.kf = KalmanFilterXYSR(dim_x=7, dim_z=4, max_obs=max_obs)
self.kf.F = np.array(
[
Expand All @@ -92,8 +96,9 @@ def __init__(self, bbox, cls, det_ind, delta_t=3, max_obs=50):
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.kf.x[:4] = xyxy2xysr(bbox)
self.time_since_update = 0
Expand Down Expand Up @@ -189,6 +194,8 @@ def __init__(
asso_func="iou",
inertia=0.2,
use_byte=False,
Q_xy_scaling=0.01,
Q_s_scaling=0.0001
):
super().__init__(max_age=max_age)
"""
Expand All @@ -204,6 +211,8 @@ def __init__(
self.asso_func = get_asso_func(asso_func)
self.inertia = inertia
self.use_byte = use_byte
self.Q_xy_scaling = Q_xy_scaling
self.Q_s_scaling = Q_s_scaling
KalmanBoxTracker.count = 0

@PerClassDecorator
Expand Down Expand Up @@ -341,7 +350,7 @@ def update(self, dets: np.ndarray, img: np.ndarray, embs: np.ndarray = None) ->

# create and initialise new trackers for unmatched detections
for i in unmatched_dets:
trk = KalmanBoxTracker(dets[i, :5], dets[i, 5], dets[i, 6], delta_t=self.delta_t, max_obs=self.max_obs)
trk = KalmanBoxTracker(dets[i, :5], dets[i, 5], dets[i, 6], delta_t=self.delta_t, Q_xy_scaling=self.Q_xy_scaling, Q_s_scaling=self.Q_s_scaling, max_obs=self.max_obs)
self.active_tracks.append(trk)
i = len(self.active_tracks)
for trk in reversed(self.active_tracks):
Expand All @@ -368,4 +377,4 @@ def update(self, dets: np.ndarray, img: np.ndarray, embs: np.ndarray = None) ->
self.active_tracks.pop(i)
if len(ret) > 0:
return np.concatenate(ret)
return np.array([])
return np.array([])
59 changes: 59 additions & 0 deletions tests/unit/test_trackers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
StrongSORT, BoTSORT, DeepOCSORT, OCSORT, BYTETracker, get_tracker_config, create_tracker,
)

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]
MOTION_N_APPEARANCE_TRACKING_METHODS=[StrongSORT, BoTSORT, DeepOCSORT]
Expand Down Expand Up @@ -59,6 +63,61 @@ def test_dynamic_max_obs_based_on_max_age():
assert ocsort.max_obs == (max_age + 5)


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

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

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, "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)
def test_per_class_tracker_output_size(tracker_type):

Expand Down
11 changes: 9 additions & 2 deletions tracking/evolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ def get_new_config(self, trial):
delta_t = trial.suggest_int("delta_t", 1, 5, step=1)
asso_func = trial.suggest_categorical("asso_func", ['iou', 'giou', 'centroid'])
inertia = trial.suggest_float("inertia", 0.1, 0.4)
use_byte = trial.suggest_categorical("use_byte", [True, False])
Q_xy_scaling = trial.suggest_float("Q_xy_scaling", 0.01, 1)
Q_s_scaling = trial.suggest_float("Q_s_scaling", 0.0001, 1)

d = {
'det_thresh': det_thresh,
Expand All @@ -165,7 +166,9 @@ def get_new_config(self, trial):
'delta_t': delta_t,
'asso_func': asso_func,
'inertia': inertia,
'use_byte': use_byte
'use_byte': use_byte,
'Q_xy_scaling': Q_xy_scaling,
'Q_s_scaling': Q_s_scaling
}

elif self.opt.tracking_method == 'deepocsort':
Expand All @@ -183,6 +186,8 @@ def get_new_config(self, trial):
embedding_off = trial.suggest_categorical("embedding_off", [True, False])
cmc_off = trial.suggest_categorical("cmc_off", [True, False])
aw_off = trial.suggest_categorical("aw_off", [True, False])
Q_xy_scaling = trial.suggest_float("Q_xy_scaling", 0.01, 1)
Q_s_scaling = trial.suggest_float("Q_s_scaling", 0.0001, 1)

d = {
'det_thresh': det_thresh,
Expand All @@ -198,6 +203,8 @@ def get_new_config(self, trial):
'embedding_off': embedding_off,
'cmc_off': cmc_off,
'aw_off': aw_off,
'Q_xy_scaling': Q_xy_scaling,
'Q_s_scaling': Q_s_scaling
}

# overwrite existing config for tracker
Expand Down
Loading