Datasets:
Download code/metrics/sensitivity.py from govtech/municipalbench-full: direct link, hf CLI and curl.
- Browser
- Download file 2.52 kB
-
https://huggingface.co/datasets/govtech/municipalbench-full/resolve/main/code/metrics/sensitivity.py
- Command line
-
hf download hf://datasets/govtech/municipalbench-full/code/metrics/sensitivity.py
-
curl -L -o sensitivity.py https://huggingface.co/datasets/govtech/municipalbench-full/resolve/main/code/metrics/sensitivity.py
2.52 kB
| """Rescore fixed detections across IoU and semantic acceptance thresholds.""" | |
| from metrics.classification import compute_classification_metrics | |
| from metrics.localisation import compute_localisation_metrics | |
| from metrics.semantic import SemanticPolicy, compute_semantic_classification_metrics, compute_semantic_localisation_metrics | |
| IOU_GRID = (0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90) | |
| SEMANTIC_GRID = (0.50, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95, 1.0) | |
| def policy_at_threshold(payload: dict, threshold: float) -> SemanticPolicy: | |
| if not 0 <= threshold <= 1: | |
| raise ValueError("Semantic threshold must be between zero and one") | |
| return SemanticPolicy.from_dict({**payload, "accepted_matrix": [ | |
| [int(i == j or (threshold < 1 and value > threshold)) for j, value in enumerate(row)] | |
| for i, row in enumerate(payload["cosine_matrix"]) | |
| ]}) | |
| def build_sensitivity(predictions, gold, policy_payload) -> dict: | |
| clean = [r for r in predictions if r.parsed_cleanly] | |
| exact_class = compute_classification_metrics(clean, gold)["summary"]["micro"] | |
| exact_iou = [{"iou": t, **compute_localisation_metrics(clean, gold, t)["summary"]["micro"]} for t in IOU_GRID] | |
| classifications, localisations = [], [] | |
| for threshold in SEMANTIC_GRID: | |
| policy = policy_at_threshold(policy_payload, threshold) | |
| classification = compute_semantic_classification_metrics(clean, gold, policy) | |
| classifications.append({"threshold": threshold, **classification["summary"]["micro"], "newly_credited": len(classification["credited_matches"])}) | |
| for iou in IOU_GRID: | |
| loc = compute_semantic_localisation_metrics(clean, gold, policy, iou) | |
| localisations.append({"threshold": threshold, "iou": iou, **loc["summary"]["micro"], "newly_credited": len(loc["credited_matches"])}) | |
| return { | |
| "n_images": len(clean), "iou_grid": IOU_GRID, "semantic_grid": SEMANTIC_GRID, | |
| "scoring_definition": "All retained detections are evaluated without confidence filtering, weighting, or ranking. Only the IoU and semantic acceptance rules change; exact class IDs at IoU >= 0.50 remain primary.", | |
| "retention_caveat": "Results are conditional on saved predictions. Inference-time detector confidence filtering and score-ranked output caps can affect which detections were retained.", | |
| "exact_classification": exact_class, "exact_iou": exact_iou, | |
| "classification": classifications, "localisation": localisations, | |
| } | |