DefectRAG / backend /calibration.py
Wizzas's picture
One q95 rule for every product: Macaroni 1 and 2 no longer use the more sensitive limit
e3d580c verified
Raw History Blame Contribute Delete
6.4 kB
"""Versioned decision calibration; encoder, memory and old records stay unchanged."""
import json
import math
import os
POLICY = 'normal-q95-v1'
FILENAME = 'sensitive_deployment_evaluation.json'
MACARONI_POLICY = 'macaroni-recall-v1'
MACARONI_FILENAME = 'macaroni_recall_deployment_evaluation.json'
def active_evaluation(root, baseline):
policy = os.getenv('DEFECTRAG_CALIBRATION', MACARONI_POLICY)
if policy == 'normal-max-v1':
return baseline
if policy not in (POLICY, MACARONI_POLICY):
raise ValueError('Unknown decision calibration policy')
path = root / 'results' / FILENAME
if not path.exists():
return baseline
candidate = json.loads(path.read_text())
if candidate.get('decision_policy') != POLICY or set(candidate['categories']) != set(baseline['categories']):
raise ValueError('Invalid decision calibration manifest')
for category, original in baseline['categories'].items():
card = candidate['categories'][category]
for key in ('category', 'profile', 'encoder_sha256', 'bank_sha256', 'heatmap_scale', 'train_normal_count'):
if card[key] != original[key]:
raise ValueError('Calibration manifest does not match the original model: ' + category)
if (card['baseline_threshold'] != original['threshold'] or
card['model_id'] != original['model_id'] + '-q95-v1' or
card['calibration'].get('policy') != POLICY or
card['calibration']['normal'] != original['calibration']['normal'] or
card['calibration']['anomalous'] != 0 or
not math.isfinite(card['threshold']) or not 0 < card['threshold'] <= original['threshold'] or
not math.isclose(card['uncertainty_margin'], card['threshold'] * .05, abs_tol=1e-12)):
raise ValueError('Invalid calibrated threshold: ' + category)
if policy == POLICY or not (root / 'results' / MACARONI_FILENAME).exists():
return candidate
targeted = json.loads((root / 'results' / MACARONI_FILENAME).read_text())
if targeted.get('decision_policy') != MACARONI_POLICY or set(targeted['categories']) != set(candidate['categories']):
raise ValueError('Invalid Macaroni calibration manifest')
for category, previous in candidate['categories'].items():
card = targeted['categories'][category]
if category not in ('macaroni1','macaroni2'):
if card != previous:
raise ValueError('Macaroni-only policy changed another category')
continue
mutable = {'threshold','uncertainty_margin','model_id','calibration','metrics','decision_policy','previous_threshold','previous_model_id'}
if ({k:v for k,v in card.items() if k not in mutable} !=
{k:v for k,v in previous.items() if k not in mutable} or
card['previous_threshold'] != previous['threshold'] or
card['previous_model_id'] != previous['model_id'] or
card['model_id'] != baseline['categories'][category]['model_id'] + '-q80-v1' or
card['decision_policy'] != 'normal-q80-v1' or
card['calibration'].get('policy') != 'normal-q80-v1' or
card['calibration'].get('quantile') != .8 or
card['calibration']['normal'] != previous['calibration']['normal'] or
card['calibration']['anomalous'] != 0 or
not math.isfinite(card['threshold']) or not 0 < card['threshold'] <= previous['threshold'] or
not math.isclose(card['uncertainty_margin'], card['threshold']*.05, abs_tol=1e-12)):
raise ValueError('Invalid Macaroni-only threshold: ' + category)
for key in ('image_auroc','average_precision','pixel_auroc_224','pixel_average_precision_224','n_images'):
if card['metrics'][key] != previous['metrics'][key]:
raise ValueError('Decision calibration changed ranking metrics')
return targeted
def calibrated_card(root, original):
# Both local and ZeroGPU inference use this exact manifest and guard logic.
baseline_path = root / 'results/highres_deployment_evaluation.json'
if not baseline_path.exists():
return original
baseline = json.loads(baseline_path.read_text())
for key in ('threshold', 'model_id', 'encoder_sha256', 'bank_sha256'):
if baseline['categories'][original['category']][key] != original[key]:
raise ValueError('Original model card and evaluation disagree')
return active_evaluation(root, baseline)['categories'][original['category']]
TUNED_POLICY = "tuned-normal-quantile-v1"
TUNED_FILENAME = "tuned_deployment_evaluation.json"
# One rule for every product (the 95th percentile of the held-out normal scores). Per-product exceptions would go here.
TUNED_QUANTILES: dict[str, float] = {}
TUNED_DEFAULT_QUANTILE = 0.95
def tuned_card(root, original):
"""The decision card of the fine-tuned profile, checked against its model card and the whole manifest.
The card in models/highres_tuned already holds the final threshold (a fixed linear quantile of held-out normal
scores, no anomalous image involved). This confirms that the manifest the dashboard shows says the same.
"""
path = root / "results" / TUNED_FILENAME
if not path.exists():
raise ValueError("Missing tuned deployment manifest")
manifest = json.loads(path.read_text())
if manifest.get("decision_policy") != TUNED_POLICY:
raise ValueError("Invalid tuned calibration manifest")
category = original["category"]
card = manifest["categories"][category]
for key in ("model_id", "profile", "encoder_sha256", "bank_sha256", "threshold", "heatmap_scale", "calibration"):
if card[key] != original[key]:
raise ValueError("Tuned model card and manifest disagree: " + category)
quantile = TUNED_QUANTILES.get(category, TUNED_DEFAULT_QUANTILE)
if (
card["calibration"].get("policy") != TUNED_POLICY
or card["calibration"].get("quantile") != quantile
or card["calibration"]["anomalous"] != 0
or not math.isfinite(card["threshold"])
or card["threshold"] <= 0
or not math.isclose(card["uncertainty_margin"], card["threshold"] * 0.05, abs_tol=1e-12)
):
raise ValueError("Invalid tuned threshold: " + category)
return card