foley.eval.prompt_match#

Calibrate the prompt-match threshold of a text-audio embedder on a labelled set (#111).

foley.prompt_match.prompt_match() flags a generated clip whose CLAP similarity to its own prompt is under a threshold. That threshold depends on the checkpoint, so it is measured, not guessed: given prompts and the clips generated for them, every prompt is scored against every clip. A clip generated for its prompt is a match; the same clip scored against another prompt is a mismatch (the failure the check exists for: a clean clip of the wrong thing). calibrate_threshold() picks the cut that best separates the two and reports precision and recall of the mismatch call; save_threshold() records it per checkpoint in foley/data/prompt_match/thresholds.json, which foley.prompt_match.threshold_for() (and so foley.index.embedders.ClapEmbedder) reads.

The labels are only as good as the clips: a “match” is a clip generated for the prompt, not one a person listened to, so a generation that missed its prompt counts as a match and pulls the threshold down. Put by-ear labels in the manifest ("match": false on a clip) to correct that. Pure numpy/stdlib apart from the embedder passed in.

Functions

calibrate_threshold(scores)

The threshold under which a clip is called a mismatch, maximising balanced accuracy.

load_labelled_dir(directory)

The score_pairs items of a directory holding manifest.json and its clips.

save_threshold(model_id, calibration, *, source)

Record calibration for model_id (source: what set it was measured on).

score_pairs(embedder, items)

Every prompt against every clip: [(cosine, is_match), ...].

Classes

Calibration(threshold, precision, recall, ...)

A measured threshold and what it achieves on the labelled set.

class foley.eval.prompt_match.Calibration(threshold, precision, recall, f1, match_kept, auc, n_match, n_mismatch, match_mean, mismatch_mean)[source]#

Bases: object

A measured threshold and what it achieves on the labelled set.

to_dict()[source]#

Return the fields as a dict, floats rounded to four decimals.

Return type:

dict

foley.eval.prompt_match.calibrate_threshold(scores)[source]#

The threshold under which a clip is called a mismatch, maximising balanced accuracy.

Balanced accuracy (the mean of the share of mismatches caught and the share of matches kept) because the set has far more mismatch pairs (n² - n) than matches (n): plain F1 would be won by a threshold that flags most good clips. A false alarm costs a listen, a miss costs a wrong sound in the film; this weighs them equally, and Calibration reports precision and recall so the trade can be re-struck.

Parameters:

scores (Sequence[tuple[float, bool]]) – [(similarity, is_match), ...] from score_pairs().

Raises:

ValueError – If either class is empty.

Return type:

Calibration

foley.eval.prompt_match.load_labelled_dir(directory)[source]#

The score_pairs items of a directory holding manifest.json and its clips.

The manifest is a list of {"prompt": ..., "file": ..., "match": true} (match defaults to true; set it false for a clip a listener rejected for its prompt).

Return type:

list[tuple[str, bytes, bool]]

foley.eval.prompt_match.save_threshold(model_id, calibration, *, source, path=PosixPath('/home/runner/work/foley/foley/foley/data/prompt_match/thresholds.json'))[source]#

Record calibration for model_id (source: what set it was measured on).

Return type:

None

foley.eval.prompt_match.score_pairs(embedder, items)[source]#

Every prompt against every clip: [(cosine, is_match), ...].

items is [(prompt, clip_bytes, clip_matches_prompt), ...]. Pair (i, i) is a match when its flag is true (a clip a listener rejected counts as a mismatch); pair (i, j) with i != j is a mismatch (clip j was made for another prompt). Embeddings are computed once per prompt and per clip.

Return type:

list[tuple[float, bool]]