perf(movienet): vectorise eval matching; count frames missing from Image.zip

movienet_eval: replace the per-element dot() with numpy — actor references are
loaded once as an ndarray and scored with a single matmul, keeping a
whole-library gallery fast.

movienet_prep: count and report frames referenced by annotations but absent
from Image.zip instead of skipping them silently.
This commit is contained in:
2026-07-04 20:41:54 +02:00
parent 1f5acc25df
commit 65fee74585
2 changed files with 24 additions and 16 deletions
+15 -11
View File
@@ -19,28 +19,32 @@ import json
import sys
from pathlib import Path
import numpy as np
sys.path.insert(0, str(Path(__file__).resolve().parent))
from sae_embed_loader import load_embedder
def load_gallery(path: str) -> dict[str, dict]:
"""Return {imdb_id: {"name": str, "embeddings": [[float]]}}."""
"""Return {imdb_id: {"name": str, "refs": np.ndarray[n_refs, dim]}}."""
with open(path) as f:
data = json.load(f)
return {a["imdb_id"]: {"name": a["name"], "embeddings": a["embeddings"]}
return {a["imdb_id"]: {"name": a["name"],
"refs": np.asarray(a["embeddings"], dtype=np.float32)}
for a in data["actors"]}
def dot(a: list[float], b: list[float]) -> float:
return sum(x * y for x, y in zip(a, b))
def match(embedding: list[float], gallery: dict[str, dict]) -> tuple[str, float, dict[str, float]]:
"""Return (best_imdb_id, best_similarity, {imdb_id: similarity})."""
scores: dict[str, float] = {}
for imdb_id, actor in gallery.items():
# max similarity across all reference embeddings for this actor
scores[imdb_id] = max(dot(embedding, ref) for ref in actor["embeddings"])
"""Return (best_imdb_id, best_similarity, {imdb_id: similarity}).
Similarity to an actor is the max dot product over that actor's reference
embeddings; vectorised with numpy so a whole-library gallery stays fast.
"""
vec = np.asarray(embedding, dtype=np.float32)
scores: dict[str, float] = {
imdb_id: float((actor["refs"] @ vec).max())
for imdb_id, actor in gallery.items()
}
best_id = max(scores, key=lambda k: scores[k])
return best_id, scores[best_id], scores