forked from andrew/feedmill-recounter
fix: reset per-track counting state at batch start (FCP predict.py:1020 parity)
This commit is contained in:
1 parent
f54521c1d8
commit
5e4add2238
4 files changed
+163
No files matched your search
@@ -244,6 +244,26 @@ class LineCrossCounter:
|
||||
self._dedup_circles.clear()
|
||||
self._loaded_tracks.clear()
|
||||
|
||||
def reset_state(self) -> None:
|
||||
"""Clear the per-track state only — the totals survive.
|
||||
|
||||
FCP parity: predict.py:1020-1032 / :1148-1150 call reset() when a
|
||||
batch starts, so every batch starts with an empty track state. FCP
|
||||
writes one row per batch, so dropping its totals is harmless there.
|
||||
The recounter reports ONE cumulative count per clip, so the totals
|
||||
must stay.
|
||||
|
||||
ponytail: one cumulative count per clip. Upgrade path: per-batch
|
||||
result rows, then a full reset() per batch.
|
||||
"""
|
||||
self._state.clear()
|
||||
self._has_been_above.clear()
|
||||
self._has_been_below.clear()
|
||||
self._counted.clear()
|
||||
self._entry_points.clear()
|
||||
self._dedup_circles.clear()
|
||||
self._loaded_tracks.clear()
|
||||
|
||||
|
||||
class MultiClassLineCounter:
|
||||
"""Sack + box counting on one shared line (Option 2: dual counters).
|
||||
@@ -345,3 +365,8 @@ class MultiClassLineCounter:
|
||||
def reset(self) -> None:
|
||||
self._sack.reset()
|
||||
self._box.reset()
|
||||
|
||||
def reset_state(self) -> None:
|
||||
"""Clear the per-track state of both counters; keep the totals."""
|
||||
self._sack.reset_state()
|
||||
self._box.reset_state()
|
||||
@@ -386,6 +386,13 @@ def run_pipeline(
|
||||
nonlocal completed_batches
|
||||
completed_batches += 1
|
||||
|
||||
def on_batch_start(batch_id, timestamp):
|
||||
# FCP parity: predict.py:1020-1032 — every batch starts with an empty
|
||||
# track state. The recounter keeps its clip-level totals.
|
||||
counter.reset_state()
|
||||
stabilizer.reset()
|
||||
|
||||
batch_mgr.on_batch_start(on_batch_start)
|
||||
batch_mgr.on_batch_end(on_batch_end)
|
||||
|
||||
try:
|
||||
@@ -799,6 +806,14 @@ def run_merged_pipeline(
|
||||
def on_batch_end(record):
|
||||
nonlocal completed_batches
|
||||
completed_batches += 1
|
||||
|
||||
def on_batch_start(batch_id, timestamp):
|
||||
# FCP parity: predict.py:1020-1032 — every batch starts with an empty
|
||||
# track state. The recounter keeps its clip-level totals.
|
||||
counter.reset_state()
|
||||
stabilizer.reset()
|
||||
|
||||
batch_mgr.on_batch_start(on_batch_start)
|
||||
batch_mgr.on_batch_end(on_batch_end)
|
||||
|
||||
try:
|
||||
|
||||
@@ -41,3 +41,18 @@ def test_reset_clears_loaded_tracks():
|
||||
c.update([_det(1, 200)])
|
||||
assert c.update([_det(1, 10)]) == []
|
||||
assert c.unloading_count == 0
|
||||
|
||||
|
||||
def test_reset_state_keeps_totals_but_clears_track_state():
|
||||
"""reset_state() clears dedup state but keeps the clip totals."""
|
||||
c = LineCrossCounter(line_y=100, line_x_start=0, line_x_end=1000)
|
||||
c.update([_det(4, 10)])
|
||||
c.update([_det(4, 200)])
|
||||
assert c.loading_count == 1
|
||||
c.reset_state()
|
||||
assert c.loading_count == 1 # totals survive
|
||||
assert c.unloading_count == 0
|
||||
assert c._loaded_tracks == set() # per-track state cleared
|
||||
# the same track may cross again in the next batch
|
||||
assert [e["direction"] for e in c.update([_det(4, 10)])] == []
|
||||
assert [e["direction"] for e in c.update([_det(4, 200)])] == ["loading"]
|
||||
@@ -256,6 +256,9 @@ class _FakeCounter:
|
||||
def update(self, detections):
|
||||
return []
|
||||
|
||||
def reset_state(self):
|
||||
pass
|
||||
|
||||
|
||||
class _FakeYOLO:
|
||||
def __init__(self, path="fake.pt"):
|
||||
@@ -443,6 +446,7 @@ class _RecordingCounter:
|
||||
self.events = list(events or [])
|
||||
self.emit_every = emit_every
|
||||
self.calls = 0
|
||||
self.reset_state_calls = 0
|
||||
|
||||
def update(self, detections):
|
||||
self.seen.extend(detections)
|
||||
@@ -454,6 +458,9 @@ class _RecordingCounter:
|
||||
return []
|
||||
return list(self.events)
|
||||
|
||||
def reset_state(self):
|
||||
self.reset_state_calls += 1
|
||||
|
||||
|
||||
class _AlwaysCountingBatch:
|
||||
"""Batch manager stub: the batch is open, so the counter is always fed."""
|
||||
@@ -468,6 +475,10 @@ class _AlwaysCountingBatch:
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.sacks_calls: list[dict] = []
|
||||
self.start_callbacks: list = []
|
||||
|
||||
def on_batch_start(self, callback):
|
||||
self.start_callbacks.append(callback)
|
||||
|
||||
def on_batch_end(self, callback):
|
||||
pass
|
||||
@@ -963,6 +974,103 @@ def test_async_writer_preserves_order_and_finishes():
|
||||
assert fake.finish_calls == 1
|
||||
|
||||
|
||||
# ── Batch start resets the per-track counting state (Task 7) ─────────────
|
||||
|
||||
|
||||
class _BatchStartFiringBatch(_AlwaysCountingBatch):
|
||||
"""Batch manager stub: fires the batch-start callback when the batch opens."""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fired = False
|
||||
|
||||
def update_sacks(self, has_crossing_event, sacks_in_area_count, timestamp,
|
||||
loading_count=0, unloading_count=0):
|
||||
super().update_sacks(has_crossing_event, sacks_in_area_count, timestamp,
|
||||
loading_count, unloading_count)
|
||||
if not self.fired:
|
||||
self.fired = True
|
||||
for cb in self.start_callbacks:
|
||||
cb(1, timestamp)
|
||||
|
||||
|
||||
class _RecordingStabilizer:
|
||||
"""Stabilizer stub that records reset() calls."""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.reset_calls = 0
|
||||
|
||||
def update(self, detections):
|
||||
return list(detections)
|
||||
|
||||
def reset(self):
|
||||
self.reset_calls += 1
|
||||
|
||||
|
||||
def _patch_batch_start_fakes(monkeypatch):
|
||||
"""Stub tracker/batch/counter/stabilizer so the pipeline runs offline."""
|
||||
from src import pipeline as pipe_mod
|
||||
|
||||
_patch_pipeline_fakes(monkeypatch)
|
||||
|
||||
class _FakeTracker:
|
||||
def __init__(self, model, conf=0.0, **kwargs):
|
||||
pass
|
||||
|
||||
def update(self, frame, dets):
|
||||
return [INSIDE_SACK]
|
||||
|
||||
counter = _RecordingCounter()
|
||||
batch = _BatchStartFiringBatch()
|
||||
stabilizer = _RecordingStabilizer()
|
||||
monkeypatch.setattr(pipe_mod, "ByteTrackTracker", _FakeTracker)
|
||||
monkeypatch.setattr(pipe_mod, "BatchLifecycleManager", lambda *a, **kw: batch)
|
||||
monkeypatch.setattr(pipe_mod, "MultiClassLineCounter", lambda **kw: counter)
|
||||
monkeypatch.setattr(pipe_mod, "BboxStabilizer", lambda *a, **kw: stabilizer)
|
||||
return counter, batch, stabilizer
|
||||
|
||||
|
||||
@pytest.mark.parametrize("fn_name", ["run_pipeline", "run_merged_pipeline"])
|
||||
def test_batch_start_resets_per_track_state(tmp_path, monkeypatch, fn_name):
|
||||
"""The batch-start hook reaches the counter and the stabilizer."""
|
||||
from src import pipeline as pipe_mod
|
||||
|
||||
counter, batch, stabilizer = _patch_batch_start_fakes(monkeypatch)
|
||||
video_path = str(tmp_path / "t.mp4")
|
||||
_write_video(video_path, frames=3)
|
||||
|
||||
if fn_name == "run_pipeline":
|
||||
pipe_mod.run_pipeline(
|
||||
video_path=video_path,
|
||||
model_config=ModelConfig(
|
||||
filename="test.pt",
|
||||
path=str(tmp_path / "test.pt"),
|
||||
stem="test",
|
||||
known_classes=["sack"],
|
||||
),
|
||||
output_path=str(tmp_path / "out.mp4"),
|
||||
preview_enabled=False,
|
||||
)
|
||||
else:
|
||||
pipe_mod.run_merged_pipeline(
|
||||
video_path=video_path,
|
||||
model_configs=[
|
||||
ModelConfig(
|
||||
filename="a.pt",
|
||||
path=str(tmp_path / "a.pt"),
|
||||
stem="a",
|
||||
known_classes=["sack"],
|
||||
)
|
||||
],
|
||||
output_path=str(tmp_path / "merged.mp4"),
|
||||
preview_enabled=False,
|
||||
)
|
||||
|
||||
assert batch.start_callbacks, "no batch-start callback registered"
|
||||
assert counter.reset_state_calls == 1
|
||||
assert stabilizer.reset_calls == 1
|
||||
|
||||
|
||||
def test_async_writer_surfaces_first_write_error():
|
||||
"""Writer failing on frame 3 raises that error from close(); queue still drains."""
|
||||
from src.pipeline import _AsyncWriter
|
||||
|
||||
Reference in new issue
Block a user