Datasets:
Download examples/score_predictions.py from govtech/municipalbench-full: direct link, hf CLI and curl.
- Browser
- Download file 2.42 kB
-
https://huggingface.co/datasets/govtech/municipalbench-full/resolve/main/examples/score_predictions.py
- Command line
-
hf download hf://datasets/govtech/municipalbench-full/examples/score_predictions.py
-
curl -L -o score_predictions.py https://huggingface.co/datasets/govtech/municipalbench-full/resolve/main/examples/score_predictions.py
2.42 kB
| """Reproduce primary scores from retained predictions; assert released parity.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import sys | |
| def score(root): | |
| sys.path.insert(0, str(root / "code")) | |
| from schemas.ground_truth import GoldDataset | |
| from schemas.inference import InferenceResponse | |
| from metrics.classification import compute_classification_metrics | |
| from metrics.localisation import compute_localisation_metrics | |
| folder = root / "evaluation/models/synthetic11534" | |
| saved = json.loads((folder / "results.json").read_text()) | |
| gold = GoldDataset.from_file(root / "annotations/ground_truth.json").records | |
| predictions = {m["id"]: [InferenceResponse(**json.loads(line)) for line in | |
| (folder / (m["id"] + "_predictions.jsonl")).read_text().splitlines()] for m in saved["models"]} | |
| valid_ids = {name: {r.image_id for r in rows if r.parsed_cleanly} for name, rows in predictions.items()} | |
| common = set.intersection(*valid_ids.values()) | |
| assert len(common) == saved["experiment"]["common_images"] | |
| output = {} | |
| for model in saved["models"]: | |
| name = model["id"] | |
| assert len(predictions[name]) == model["coverage"]["expected"] | |
| assert len(valid_ids[name]) == model["coverage"]["valid"] | |
| output[name] = {} | |
| for scope, ids in (("valid", valid_ids[name]), ("common", common)): | |
| rows = [r for r in predictions[name] if r.image_id in ids] | |
| output[name][scope] = { | |
| "classification": compute_classification_metrics(rows, gold), | |
| "localisation": compute_localisation_metrics(rows, gold, iou_threshold=0.5), | |
| } | |
| for task, result in output[name][scope].items(): | |
| expected = model["scopes"][scope][task] | |
| for key in ("summary", "per_violation"): | |
| assert result[key] == expected[key], f"Mismatch: {name}/{scope}/{task}/{key}" | |
| return output | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parents[1]) | |
| parser.add_argument("--output", type=Path, default=Path("/tmp/model-scores.json")) | |
| args = parser.parse_args() | |
| result = score(args.root.resolve()) | |
| args.output.write_text(json.dumps(result, indent=2) + "\n") | |
| print(f"All model valid/common primary scores match: {args.output}") | |