Spaces:
Paused
Paused
Zhen Ye commited on
Commit ·
7e7e04e
1
Parent(s): e7dfc36
Refactor ByteTracker: add min_hits confirmation, split thresholds, associate unconfirmed tracks
Browse files222 unmodified lines22322422522622722822923023123223323423523623723823942 unmodified lines2822832842852862872882892902912922932942952968 unmodified lines30530630730830931031131231331431531631731831932032133 unmodified lines35535635735835936036136236336436536636733 unmodified lines40140240340440540640740840941041141241341420 unmodified lines4354364374384394404414424434444454467 unmodified lines4544554564574584594604614624634644652 unmodified lines468469470471472473474475476477478479222 unmodified lines# wait, input is xyxy usually in our pipeline# ByteTrack usually uses tlwh internally.# Let's standardize to input xyxy.self._tlwh = np.asarray(self._tlwh_from_xyxy(tlwh), dtype=np.float32)self.is_activated = Falseself.track_id = 0self.state = 1 # 1: New, 2: Tracked, 3: Lost, 4: Removedself.score = scoreself.label = labelself.start_frame = 0self.frame_id = 0self.time_since_update = 0# Multi-frame historyself.history = []42 unmodified linesreturn retdef activate(self, kalman_filter, frame_id):"""Start a new track."""self.kalman_filter = kalman_filterself.track_id = self.next_id()self.mean, self.covariance = self.kalman_filter.initiate(self.tlbr) # Initiate needs xyxyself.state = 2 # Trackedself.frame_id = frame_idself.start_frame = frame_idself.is_activated = Truedef re_activate(self, new_track, frame_id, new_id=False):"""Reactivate a lost track with a new detection."""8 unmodified linesif new_id:self.track_id = self.next_id()def update(self, new_track, frame_id):"""Update a tracked object with a new detection."""self.frame_id = frame_idself.time_since_update = 0self.score = new_track.scoreself.mean, self.covariance = self.kalman_filter.update(self.mean, self.covariance, self._xyah_from_xyxy(new_track.tlbr))self.state = 2 # Trackedself.is_activated = Truedef predict(self):"""Propagate tracking state distribution one time step forward."""33 unmodified linesclass ByteTracker:def __init__(self, track_thresh=0.3, track_buffer=60, match_thresh=0.8, frame_rate=30):STrack.reset_count()self.track_thresh = track_threshself.track_buffer = track_bufferself.match_thresh = match_threshself.frame_id = 0self.tracked_stracks = [] # Type: List[STrack]self.lost_stracks = [] # Type: List[STrack]self.removed_stracks = [] # Type: List[STrack]33 unmodified lines# We wrap original dict in STrackfor d in detections_list:bbox = d['bbox']score = d['score']label = d['label']t = STrack(bbox, score, label)t.original_data = d # Link backif score >= self.track_thresh:detections.append(t)else:detections_second.append(t)20 unmodified linestrack = strack_pool[itracked]det = detections[idet]if track.state == 2:track.update(det, self.frame_id)activated_stracks.append(track)else:track.re_activate(det, self.frame_id, new_id=False)refind_stracks.append(track)# Persist dataself._sync_data(track, det)7 unmodified linestrack = r_tracked_stracks[itracked]det = detections_second[idet]if track.state == 2:track.update(det, self.frame_id)activated_stracks.append(track)else:track.re_activate(det, self.frame_id, new_id=False)refind_stracks.append(track)self._sync_data(track, det)for it in u_track:2 unmodified linestrack.state = 3 # Lostlost_stracks.append(track)# 4. Init new tracks from unmatched high score detections# Note: Unmatched low score detections are ignored (noise)unmatched_dets = [detections[i] for i in u_detection]for track in unmatched_dets:if track.score < self.track_thresh:continuetrack.activate(self.kalman_filter, self.frame_id)activated_stracks.append(track)222 unmodified lines22322422522622722822923023123223323423523623723823924042 unmodified lines2832842852862872882892902912922932942952962972988 unmodified lines30730830931031131231331431531631731831932032132232332432533 unmodified lines35936036136236336436536636736836937037137237337437537633 unmodified lines41041141241341441541641741841942042142242320 unmodified lines4444454464474484494504514524534544557 unmodified lines4634644654664674684694704714724734742 unmodified lines477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517222 unmodified lines# wait, input is xyxy usually in our pipeline# ByteTrack usually uses tlwh internally.# Let's standardize to input xyxy.self._tlwh = np.asarray(self._tlwh_from_xyxy(tlwh), dtype=np.float32)self.is_activated = Falseself.track_id = 0self.state = 1 # 1: New, 2: Tracked, 3: Lost, 4: Removedself.score = scoreself.label = labelself.start_frame = 0self.frame_id = 0self.time_since_update = 0self.hits = 0# Multi-frame historyself.history = []42 unmodified linesreturn retdef activate(self, kalman_filter, frame_id):"""Start a new track (tentative until min_hits reached)."""self.kalman_filter = kalman_filterself.track_id = self.next_id()self.mean, self.covariance = self.kalman_filter.initiate(self.tlbr) # Initiate needs xyxyself.state = 2 # Trackedself.frame_id = frame_idself.start_frame = frame_idself.is_activated = False # Tentative until min_hits reachedself.hits = 1def re_activate(self, new_track, frame_id, new_id=False):"""Reactivate a lost track with a new detection."""8 unmodified linesif new_id:self.track_id = self.next_id()def update(self, new_track, frame_id, min_hits=3):"""Update a tracked object with a new detection."""self.frame_id = frame_idself.time_since_update = 0self.score = new_track.scoreself.hits += 1self.mean, self.covariance = self.kalman_filter.update(self.mean, self.covariance, self._xyah_from_xyxy(new_track.tlbr))self.state = 2 # Trackedif self.hits >= min_hits:self.is_activated = True # Confirmed after N consecutive hitsdef predict(self):"""Propagate tracking state distribution one time step forward."""33 unmodified linesclass ByteTracker:def __init__(self, track_high_thresh=0.3, track_low_thresh=0.1,new_track_thresh=0.4, track_buffer=60, match_thresh=0.8,frame_rate=30, min_hits=3):STrack.reset_count()self.track_high_thresh = track_high_threshself.track_low_thresh = track_low_threshself.new_track_thresh = new_track_threshself.track_buffer = track_bufferself.match_thresh = match_threshself.min_hits = min_hitsself.frame_id = 0self.tracked_stracks = [] # Type: List[STrack]self.lost_stracks = [] # Type: List[STrack]self.removed_stracks = [] # Type: List[STrack]33 unmodified lines# We wrap original dict in STrackfor d in detections_list:score = d['score']if score < self.track_low_thresh:continue # Background noise — discard entirelyt = STrack(d['bbox'], score, d['label'])t.original_data = d # Link backif score >= self.track_high_thresh:detections.append(t)else:detections_second.append(t)20 unmodified linestrack = strack_pool[itracked]det = detections[idet]if track.state == 2:track.update(det, self.frame_id, min_hits=self.min_hits)activated_stracks.append(track)else:track.re_activate(det, self.frame_id, new_id=False)refind_stracks.append(track)# Persist dataself._sync_data(track, det)7 unmodified linestrack = r_tracked_stracks[itracked]det = detections_second[idet]if track.state == 2:track.update(det, self.frame_id, min_hits=self.min_hits)activated_stracks.append(track)else:track.re_activate(det, self.frame_id, new_id=False)refind_stracks.append(track)self._sync_data(track, det)for it in u_track:2 unmodified linestrack.state = 3 # Lostlost_stracks.append(track)# 3.5 Associate unconfirmed tracks with remaining unmatched detectionsif unconfirmed and u_detection:remaining_dets = [detections[i] for i in u_detection]dists = iou_distance(unconfirmed, remaining_dets)matches_unc, u_unconfirmed, u_det_remaining = linear_assignment(dists, thresh=0.7)for itracked, idet in matches_unc:track = unconfirmed[itracked]det = remaining_dets[idet]track.update(det, self.frame_id, min_hits=self.min_hits)activated_stracks.append(track)self._sync_data(track, det)# Update u_detection to only contain indices not matched to unconfirmedmatched_det_indices = set(u_detection[idet] for _, idet in matches_unc) if len(matches_unc) > 0 else set()u_detection = [i for i in u_detection if i not in matched_det_indices]# Unconfirmed tracks that didn't match → remove (too noisy to keep)for it in u_unconfirmed:track = unconfirmed[it]track.state = 4 # Removedremoved_stracks.append(track)elif unconfirmed:# No detections left to match — remove all unconfirmedfor track in unconfirmed:track.state = 4 # Removedremoved_stracks.append(track)# 4. Init new tracks from unmatched high score detections# Note: Unmatched low score detections are ignored (noise)unmatched_dets = [detections[i] for i in u_detection]for track in unmatched_dets:if track.score < self.new_track_thresh:continue # Not confident enough to start a new tracktrack.activate(self.kalman_filter, self.frame_id)activated_stracks.append(track)
utils/tracker.py+62-24