from pathlib import Path import numpy as np import pandas as pd from ultralytics import YOLO ROOT = Path("/media/rtx5090/Scripts/runs/detect/training_stats/sota_study/automotive/") DATA = "/media/z4/DATASETS/AUTOMOTIVE/yolo/dataset.yaml" PROJECT = ROOT / "evaluation" CSV_PATH = PROJECT / "evaluation_results.csv" METRICS = [ "mAP50", "mAP50_95", "precision", "recall", "f1", ] # Bootstrap settings N_BOOT = 10000 CI_LEVEL = 0.95 BOOT_SEED = 42 def summarize_group(group, metrics=METRICS, n_boot=N_BOOT, ci=CI_LEVEL, seed=BOOT_SEED): rng = np.random.default_rng(seed) out = {} for metric in metrics: data = group[metric].to_numpy(dtype=float) n = data.size out[(metric, "mean")] = data.mean() out[(metric, "std")] = data.std(ddof=1) if n > 1 else np.nan out[(metric, "min")] = data.min() out[(metric, "max")] = data.max() out[(metric, "n")] = n if n >= 2: idx = rng.integers(0, n, size=(n_boot, n)) boot_means = data[idx].mean(axis=1) lo, hi = np.percentile( boot_means, [(1 - ci) / 2 * 100, (1 - (1 - ci) / 2) * 100] ) else: lo, hi = np.nan, np.nan out[(metric, "ci_lo")] = lo out[(metric, "ci_hi")] = hi return pd.Series(out) def main(): PROJECT.mkdir(exist_ok=True) results = [] # Structure: # ROOT/ # ├── experiment/ # │ ├── computer/ # │ │ ├── run_1/ # │ │ ├── run_2/ for experiment_dir in sorted(ROOT.iterdir()): if not experiment_dir.is_dir(): continue if experiment_dir.name == "evaluation": continue experiment = experiment_dir.name # Computer/workstation level for computer_dir in sorted(experiment_dir.iterdir()): if not computer_dir.is_dir(): continue computer = computer_dir.name # Run level for run in sorted(computer_dir.glob("run_*")): if not run.is_dir(): continue weights = run / "weights" / "best.pt" if not weights.exists(): print( f"Skipping {experiment}/{computer}/{run.name}: " "best.pt not found" ) continue print( f"\nEvaluating " f"{experiment} / {computer} / {run.name}" ) try: model = YOLO(weights) metrics = model.val( data=DATA, split="test", imgsz=512, batch=28, device=0, workers=8, project=str(PROJECT), name=f"{experiment}_{computer}_{run.name}", exist_ok=True, save_json=True, plots=True, verbose=True, ) except Exception as e: print( f"Failed evaluating " f"{experiment}/{computer}/{run.name}" ) print(e) continue box = metrics.box results.append( { "experiment": experiment, "computer": computer, "run": run.name, "mAP50": float(box.map50), "mAP50_95": float(box.map), "precision": float(box.mp), "recall": float(box.mr), "f1": float(box.f1.mean()), } ) # Save per-run results df = pd.DataFrame(results) df.to_csv(CSV_PATH, index=False) print(f"\nSaved: {CSV_PATH}") if df.empty: print("No evaluation results found.") return # Statistics per experiment (mean, std, min, max, n, bootstrap CI) summary = df.groupby("experiment").apply(summarize_group) summary_path = PROJECT / "evaluation_summary.csv" summary.to_csv(summary_path) print(f"Saved: {summary_path}") print("\nSummary:") print(summary) # Statistics per experiment and computer (mean, std, min, max, n, bootstrap CI) computer_summary = ( df.groupby(["experiment", "computer"]) .apply(summarize_group) ) computer_summary_path = PROJECT / "evaluation_summary_by_computer.csv" computer_summary.to_csv(computer_summary_path) print(f"\nSaved: {computer_summary_path}") if __name__ == "__main__": main()