File size: 5,612 Bytes
590304f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
"""Evaluate one SAM3 LoRA checkpoint across equivalent lesion prompts."""

from __future__ import annotations

import argparse
import itertools
import json
import os
import re
import subprocess
import sys
from pathlib import Path

import numpy as np
import pandas as pd
from PIL import Image

PROMPTS = ["breast lesion", "breast tumor", "breast mass", "breast nodule", "ultrasound breast lesion", "breast ultrasound mass", "hypoechoic breast lesion"]
SCRIPT_DIR = Path(__file__).resolve().parent
BUNDLE_ROOT = SCRIPT_DIR.parent


def slug(text):
    return re.sub(r"[^A-Za-z0-9]+", "_", text).strip("_").lower()


def args_parser():
    p = argparse.ArgumentParser()
    p.add_argument("--index_csv", default="data/sam3_multiprompt_lora_qc/index/test_index.csv")
    p.add_argument("--sam3_base_model_path", default=str(Path(os.environ.get("SAM3_CHECKPOINT", BUNDLE_ROOT / "model" / "sam3_base.pt"))))
    p.add_argument("--sam3_lora_path", default=str(BUNDLE_ROOT / "model" / "best_model.pt"))
    p.add_argument("--output_dir", default="outputs/sam3_multiprompt_lora_qc/single_prompt_lora_multiprompt_eval")
    p.add_argument("--prompts", nargs="+", default=PROMPTS)
    p.add_argument("--max_samples", type=int)
    p.add_argument("--overwrite", action="store_true")
    p.add_argument("--resume", action="store_true")
    return p.parse_args()


def mask(path):
    return np.asarray(Image.open(path).convert("L")) > 0


def dice(a, b):
    if a.shape != b.shape:
        b = np.asarray(Image.fromarray(b.astype(np.uint8) * 255).resize((a.shape[1], a.shape[0]), Image.Resampling.NEAREST)) > 0
    den = int(a.sum() + b.sum())
    return 2 * int(np.logical_and(a, b).sum()) / den if den else 1.0


def main():
    args = args_parser()
    out = Path(args.output_dir)
    out.mkdir(parents=True, exist_ok=True)
    lora = Path(args.sam3_lora_path)
    checkpoint = lora / "best_model.pt" if lora.is_dir() else lora
    if not checkpoint.exists():
        raise FileNotFoundError(checkpoint)
    command = [
        sys.executable, "-u", str(SCRIPT_DIR / "evaluate_sam3_test_text_prompt_segmentation.py"),
        "--index_csv", args.index_csv, "--sam3_model_path", args.sam3_base_model_path,
        "--checkpoint_path", str(checkpoint), "--output_dir", str(out),
        "--encoder_trainable", "lora", "--lora_rank", "8", "--lora_alpha", "16",
        "--prompts", *args.prompts,
    ]
    if args.max_samples:
        command += ["--max_samples", str(args.max_samples)]
    if args.overwrite:
        command.append("--overwrite")
    if args.resume:
        command.append("--resume")
    subprocess.run(command, check=True)

    frames = []
    for prompt in args.prompts:
        frame = pd.read_csv(out / f"predictions_{slug(prompt)}.csv")
        frame["prompt"] = prompt
        frames.append(frame)
    predictions = pd.concat(frames, ignore_index=True)
    predictions.to_csv(out / "all_prompt_predictions.csv", index=False)

    per_sample = []
    for sid, group in predictions.groupby("sample_id"):
        valid = group[group["valid_mask"].astype(bool)]
        masks = [(row["prompt"], mask(row["pred_mask_path"])) for _, row in valid.iterrows()]
        pairs = [dice(a, b) for (_, a), (_, b) in itertools.combinations(masks, 2)]
        areas = valid["pred_area"].astype(float).to_numpy()
        per_sample.append({
            "sample_id": sid,
            "prompt_dice_std": float(valid["dice"].std(ddof=0)),
            "mask_area_cv": float(areas.std() / areas.mean()) if len(areas) and areas.mean() else np.nan,
            "pairwise_mask_dice_mean": float(np.mean(pairs)) if pairs else np.nan,
            "pairwise_mask_dice_min": float(np.min(pairs)) if pairs else np.nan,
            "valid_prompt_count": len(valid),
        })
    sample_summary = pd.DataFrame(per_sample)
    sample_summary.to_csv(out / "per_sample_prompt_sensitivity.csv", index=False)
    metrics = pd.read_csv(out / "all_prompt_metrics.csv")
    best = metrics.loc[metrics["mean Dice"].idxmax()]
    worst = metrics.loc[metrics["mean Dice"].idxmin()]
    training = metrics[metrics["prompt"] == "breast lesion"].iloc[0]
    robustness = {
        "checkpoint_path": str(checkpoint.resolve()),
        "best_prompt_by_mean_dice": best["prompt"],
        "best_mean_dice": best["mean Dice"],
        "worst_prompt_by_mean_dice": worst["prompt"],
        "worst_mean_dice": worst["mean Dice"],
        "mean_dice_range": best["mean Dice"] - worst["mean Dice"],
        "std_mean_dice_across_prompts": metrics["mean Dice"].std(ddof=0),
        "mean_per_sample_prompt_dice_std": sample_summary["prompt_dice_std"].mean(),
        "mean_per_sample_area_cv": sample_summary["mask_area_cv"].mean(),
        "mean_pairwise_mask_dice": sample_summary["pairwise_mask_dice_mean"].mean(),
        "fraction_pairwise_mean_below_0p8": (sample_summary["pairwise_mask_dice_mean"] < 0.8).mean(),
        "max_nontraining_prompt_drop_vs_breast_lesion": training["mean Dice"] - metrics[metrics["prompt"] != "breast lesion"]["mean Dice"].min(),
    }
    robustness["large_prompt_sensitivity"] = bool(
        robustness["mean_dice_range"] >= 0.05
        or robustness["fraction_pairwise_mean_below_0p8"] >= 0.1
        or robustness["max_nontraining_prompt_drop_vs_breast_lesion"] > 0.05
    )
    pd.DataFrame([robustness]).to_csv(out / "prompt_sensitivity_summary.csv", index=False)
    with open(out / "prompt_sensitivity_summary.json", "w") as handle:
        json.dump(robustness, handle, indent=2)
    print(pd.DataFrame([robustness]).to_string(index=False))


if __name__ == "__main__":
    main()