Download eval/score.py from facebook/personalized-hearing: direct link, hf CLI and curl.
- Browser
- Download file 3.21 kB
-
https://huggingface.co/datasets/facebook/personalized-hearing/resolve/main/eval/score.py
- Command line
-
hf download hf://datasets/facebook/personalized-hearing/eval/score.py
-
curl -L -o score.py https://huggingface.co/datasets/facebook/personalized-hearing/resolve/main/eval/score.py
3.21 kB
| # Copyright (c) Meta Platforms, Inc. and affiliates. | |
| # All rights reserved. | |
| # | |
| # This source code is licensed under the license found in the | |
| # LICENSE file in the root directory of this source tree. | |
| """Accuracy per question type, per category, and overall, from eval/run_eval.py output. | |
| Accuracy is correct / (correct + wrong). Replies that named no single choice | |
| (parse_failure) and requests that failed (error) are counted and reported but | |
| left out of the accuracy. | |
| Usage: | |
| python eval/score.py results/<model> | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from collections import Counter, defaultdict | |
| from pathlib import Path | |
| # Type value in the data -> (name used in the paper, category) | |
| TYPES = { | |
| "audio_presence": ("Sound Presence", "Audio"), | |
| "audio_only_recall": ("Audio Scene Recall", "Audio"), | |
| "audio_occasion": ("Audio Occasion", "Audio"), | |
| "visual_detail_recall": ("Visual Detail Recall", "Visual"), | |
| "preference_state_at_time": ("Past Preference Recall", "Personalization"), | |
| "preference_change_count": ("Preference Change Count", "Personalization"), | |
| "preference_replaced": ("Preference Replacement", "Personalization"), | |
| "preference_never_asked": ("Preference Hallucination", "Personalization"), | |
| "exception_recall": ("Exception Recall", "Personalization"), | |
| "exception_expiry": ("Exception Expiry", "Personalization"), | |
| "hearing_adjustment": ("Adjustment Suggestion", "Hearing Adjustment"), | |
| } | |
| CATEGORIES = ["Audio", "Visual", "Personalization", "Hearing Adjustment"] | |
| def accuracy(counts: Counter) -> str: | |
| scored = counts["correct"] + counts["wrong"] | |
| return f"{100 * counts['correct'] / scored:6.2f}" if scored else " -" | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
| parser.add_argument("results_dir", type=Path) | |
| args = parser.parse_args() | |
| by_type: dict[str, Counter] = defaultdict(Counter) | |
| by_category: dict[str, Counter] = defaultdict(Counter) | |
| overall: Counter = Counter() | |
| for path in sorted(args.results_dir.glob("*.jsonl")): | |
| latest: dict[str, dict] = {} | |
| for line in path.read_text().splitlines(): | |
| record = json.loads(line) | |
| latest[record["candidate_id"]] = record # a resumed run's retry replaces the error | |
| for record in latest.values(): | |
| name, category = TYPES[record["type"]] | |
| by_type[name][record["outcome"]] += 1 | |
| by_category[category][record["outcome"]] += 1 | |
| overall[record["outcome"]] += 1 | |
| print(f"{'type':28s} {'acc':>6s} {'n':>5s}") | |
| for key, (name, _) in TYPES.items(): | |
| if name in by_type: | |
| print(f"{name:28s} {accuracy(by_type[name])} {sum(by_type[name].values()):5d}") | |
| print() | |
| for category in CATEGORIES: | |
| if category in by_category: | |
| print(f"{category:28s} {accuracy(by_category[category])} {sum(by_category[category].values()):5d}") | |
| print(f"{'Overall':28s} {accuracy(overall)} {sum(overall.values()):5d}") | |
| print(f"\nparse failures: {overall['parse_failure']}, errors: {overall['error']}") | |
| if __name__ == "__main__": | |
| main() | |