HoldTrack replay data and the SAM 3 against SAM 3.1 comparison on DanceTrack (part 13)
124e919 verified Download sam31_multiplex/code/common_loop.py from TheophaneBourdon/HoldTrack-data: direct link, hf CLI and curl.
- Browser
- Download file 6.98 kB
-
https://huggingface.co/datasets/TheophaneBourdon/HoldTrack-data/resolve/main/sam31_multiplex/code/common_loop.py
- Command line
-
hf download hf://datasets/TheophaneBourdon/HoldTrack-data/sam31_multiplex/code/common_loop.py
-
curl -L -o common_loop.py https://huggingface.co/datasets/TheophaneBourdon/HoldTrack-data/resolve/main/sam31_multiplex/code/common_loop.py
6.98 kB
| """One CTI/Mo/lost-object decision loop; only the model engine differs. | |
| No annotations are imported. ACM reference promotion is intentionally absent. | |
| All frame indices here are zero based; external MOT files are one based. | |
| """ | |
| import json | |
| import time | |
| from pathlib import Path | |
| import numpy as np | |
| from holdtrack.sam import liam, backend | |
| from holdtrack.sam.transactions import _advance_mo_confusion_counter | |
| def run(engine, detections, frames, output): | |
| output = Path(output); output.mkdir(parents=True, exist_ok=False) | |
| tracks = {}; next_id = 1; rows = [] | |
| started = time.monotonic() | |
| with (output/'frames.jsonl').open('x') as log: | |
| for frame in range(frames): | |
| logits, scores = engine.propagate(frame) | |
| assert set(logits) == set(tracks), (frame, sorted(logits), sorted(tracks)) | |
| observed = dict(scores) | |
| masks = {i: x > liam.MASK_LOGIT_THRESHOLD for i, x in logits.items()} | |
| boxes = {i: backend.mask_box(x) for i, x in logits.items()} | |
| boxes = {i: b for i, b in boxes.items() if b is not None} | |
| det = sorted([d for d in detections.get(frame, ()) if d.confidence >= liam.DETECTION_FLOOR], | |
| key=lambda d: (int(d.index), -d.confidence)) | |
| ids = sorted(boxes) | |
| ious = np.zeros((len(ids), len(det))) | |
| for a, ident in enumerate(ids): | |
| for b, d in enumerate(det): | |
| ious[a, b] = liam.box_iou(boxes[ident], d.box) | |
| matched = {b for a, b in liam.gated_hungarian_matches(ious)} | |
| existing = {i: x for i, x in masks.items() if x.any()} | |
| births = {}; events = [] | |
| unmatched = sorted([(j, d) for j, d in enumerate(det) if j not in matched], | |
| key=lambda v: (-v[1].confidence, v[1].index)) | |
| for _, detection in unmatched: | |
| coverage = liam.tracked_coverage_fraction(detection.box, existing) | |
| eligible, contrastive = liam.cti_precheck(detection.confidence, coverage) | |
| if not eligible: | |
| continue | |
| negatives = liam.contrastive_negative_points(detection.box, existing) if contrastive else () | |
| ident = next_id; next_id += 1 | |
| mask_logits, score = engine.prompt(frame, ident, detection.box, negatives) | |
| mask = mask_logits > 0 | |
| duplicate = liam.duplicate_mask_fraction(mask, existing) | |
| if duplicate > liam.DUPLICATE_MASK_COVERAGE: | |
| engine.reject(ident) | |
| events.append(dict(id=ident, detection=int(detection.index), accepted=False, | |
| duplicate_fraction=duplicate)) | |
| continue | |
| tracks[ident] = liam.LiAMTrackState(obj_id=ident, birth_frame=frame) | |
| # The birth is the sole conditioning reference; no ACM additions. | |
| tracks[ident].append_acm_reference(frame) | |
| births[ident] = mask_logits | |
| logits[ident] = mask_logits; masks[ident] = mask; existing[ident] = mask | |
| observed[ident] = float(score) | |
| box = backend.mask_box(mask_logits) | |
| if box is not None: boxes[ident] = box | |
| events.append(dict(id=ident, detection=int(detection.index), accepted=True, | |
| score=float(score), coverage=coverage, negative_points=list(negatives), | |
| duplicate_fraction=duplicate)) | |
| engine.consolidate_births(frame) | |
| for ident, track in tracks.items(): | |
| track.update_motion(frame, boxes.get(ident)) | |
| suppressed = set(); retired = set(); partners = {}; mo = [] | |
| for first, second, overlap in liam.mo_overlap_pairs({i: m for i, m in masks.items() if m.any()}): | |
| if first in suppressed or second in suppressed or first in retired or second in retired: | |
| continue | |
| distance, _ = liam.trajectory_window_squared_mahalanobis(tracks[first], tracks[second]) | |
| loser, reason = liam.choose_mgg_loser(tracks[first], tracks[second], | |
| mahalanobis2=distance, first_current_score=observed[first], | |
| second_current_score=observed[second]) | |
| partner = second if loser == first else first | |
| count = _advance_mo_confusion_counter(tracks[loser], frame, partner) | |
| suppressed.add(loser); partners[loser] = partner | |
| if tracks[loser].birth_frame == frame or count >= liam.MGG_RETIRE_AFTER: | |
| retired.add(loser) | |
| mo.append(dict(loser=loser, partner=partner, consecutive=count, | |
| overlap=float(overlap), rule=reason)) | |
| for ident, track in tracks.items(): | |
| if ident not in partners: | |
| track.last_confuser_obj_id = None; track.last_confusion_frame = None | |
| track.consecutive_confusions = 0 | |
| if track.last_seen_frame is not None and frame-track.last_seen_frame >= liam.MAX_UNSEEN_FRAMES: | |
| retired.add(ident) | |
| memory = engine.commit(frame, births, suppressed, retired, tracks) | |
| for ident in sorted(retired): | |
| del tracks[ident] | |
| current = [] | |
| for ident, track in tracks.items(): | |
| if ident in observed: track.push_score(observed[ident]) | |
| if ident in boxes and ident not in suppressed: | |
| current.append((frame, ident, *boxes[ident])) | |
| rows.extend(current) | |
| if frame==59 and frames>60: | |
| (output/'prefix60_raw_MASK_BOX_IDS.txt').write_bytes(backend.mot_bytes(rows)) | |
| engine.prune(frame) | |
| if hasattr(engine,'cache_summary'): | |
| cache=engine.cache_summary() | |
| assert cache['max_noncond_frames']<=cache['retention_window']+1,cache | |
| if cache.get('singleton_bucket_occupancies') is not None: | |
| assert all(n<=1 for state in cache['singleton_bucket_occupancies'] for n in state),cache | |
| memory.update(cache) | |
| record = dict(frame=frame, active=len(tracks), births=events, mo=mo, | |
| retired=sorted(retired), rows=current, memory=memory, | |
| allocated_bytes=engine.torch.cuda.memory_allocated(), | |
| reserved_bytes=engine.torch.cuda.memory_reserved(), | |
| elapsed_seconds=time.monotonic()-started) | |
| log.write(json.dumps(record)+'\n'); log.flush() | |
| if frame % 10 == 0: | |
| (output/'progress.json').write_text(json.dumps(record, indent=2)) | |
| raw = backend.mot_bytes(rows) | |
| (output/'raw_MASK_BOX_IDS.txt').write_bytes(raw) | |
| return dict(frames=frames, rows=len(rows), seconds=time.monotonic()-started, | |
| raw=str(output/'raw_MASK_BOX_IDS.txt'), engines=engine.report()) | |