Datasets:
Download scripts/plot_multimodal_aupro.py from moiaraya/i-badas: direct link, hf CLI and curl.
- Browser
- Download file 4.95 kB
-
https://huggingface.co/datasets/moiaraya/i-badas/resolve/main/scripts/plot_multimodal_aupro.py
- Command line
-
hf download hf://datasets/moiaraya/i-badas/scripts/plot_multimodal_aupro.py
-
curl -L -o plot_multimodal_aupro.py https://huggingface.co/datasets/moiaraya/i-badas/resolve/main/scripts/plot_multimodal_aupro.py
4.95 kB
| #!/usr/bin/env python3 | |
| """Plot per-class multimodal isolated AUPRO comparison across models.""" | |
| import argparse | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import matplotlib | |
| matplotlib.use("Agg") | |
| import matplotlib.pyplot as plt | |
| import pandas as pd | |
| import seaborn as sns | |
| def find_data_file(explicit_path=None): | |
| if explicit_path: | |
| p = Path(explicit_path) | |
| if p.is_file(): | |
| return p | |
| raise FileNotFoundError(f"Specified data file not found: {explicit_path}") | |
| candidates = [ | |
| Path("checkpoint_data/multimodal_isolated_metrics.json"), | |
| Path("../checkpoint_data/multimodal_isolated_metrics.json"), | |
| Path(__file__).resolve().parent.parent / "checkpoint_data" / "multimodal_isolated_metrics.json", | |
| Path(__file__).resolve().parent / "multimodal_isolated_metrics.json", | |
| Path("multimodal_isolated_metrics.json"), | |
| ] | |
| for c in candidates: | |
| if c.is_file(): | |
| return c | |
| raise FileNotFoundError( | |
| "Could not find 'multimodal_isolated_metrics.json'. Looked in: " | |
| + ", ".join(str(c) for c in candidates) | |
| ) | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Plot per-class multimodal isolated AUPRO") | |
| parser.add_argument( | |
| "--data", | |
| type=Path, | |
| default=None, | |
| help="Path to multimodal_isolated_metrics.json (auto-detected if omitted)", | |
| ) | |
| parser.add_argument( | |
| "--out-dir", | |
| type=Path, | |
| default=None, | |
| help="Output folder for figures (default: checkpoint_data/ or current dir)", | |
| ) | |
| parser.add_argument( | |
| "--stem", | |
| default="performance_aupro_multimodal", | |
| help="Output filename stem without extension", | |
| ) | |
| parser.add_argument( | |
| "--show", | |
| action="store_true", | |
| help="Show interactive plot window", | |
| ) | |
| args = parser.parse_args() | |
| data_file = find_data_file(args.data) | |
| print(f"Loading data from: {data_file}") | |
| with open(data_file, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| # 2. Flatten the data | |
| rows = [] | |
| for model, cameras in data.items(): | |
| if model == "CMDR": | |
| model = "CMDR-IAD" | |
| for camera, scenes in cameras.items(): | |
| for scene, classes in scenes.items(): | |
| for cls, metrics in classes.items(): | |
| rows.append({ | |
| "Model": model, | |
| "Class": cls, | |
| "AUPRO": metrics.get("Pixel_AUPRO", metrics.get("aupro_30", 0.0)), | |
| }) | |
| if not rows: | |
| raise SystemExit("No metric records found in input file.") | |
| df = pd.DataFrame(rows) | |
| # 3. Aggregate: Mean performance per Model and Class | |
| df_agg = df.groupby(["Model", "Class"])["AUPRO"].mean().reset_index() | |
| # 4. Calculate Mean per Class (across all models) | |
| class_means = df_agg.groupby("Class")["AUPRO"].mean() | |
| # 5. Visualization | |
| sns.set_theme(style="whitegrid", font_scale=1.1) | |
| fig = plt.figure(figsize=(14, 5)) | |
| # Create barplot | |
| chart = sns.barplot( | |
| data=df_agg, | |
| x="Class", | |
| y="AUPRO", | |
| hue="Model", | |
| hue_order=["M3DM", "CMDR-IAD", "CFM", "WinCLIP", "UniVAD"], | |
| palette="viridis", | |
| ) | |
| # 6. Add dotted line segments and text labels | |
| classes = df_agg["Class"].unique() | |
| for i, cls in enumerate(classes): | |
| mean_val = class_means[cls] | |
| plt.hlines( | |
| y=mean_val, | |
| xmin=i - 0.4, | |
| xmax=i + 0.4, | |
| colors="red", | |
| linestyles=":", | |
| linewidth=3, | |
| label="Class Mean" if i == 0 else "", | |
| ) | |
| plt.text( | |
| i + 0.3, | |
| mean_val + 0.018, | |
| f"{mean_val:.2f}", | |
| color="red", | |
| ha="left", | |
| va="center", | |
| fontsize=9, | |
| fontweight="bold", | |
| ) | |
| plt.xticks(rotation=15, ha="right") | |
| plt.xlabel("") | |
| plt.ylabel("Mean Pixel AUPRO Score") | |
| plt.legend( | |
| loc="upper left", | |
| ncol=3, | |
| frameon=True, | |
| framealpha=0.2, | |
| facecolor="white", | |
| edgecolor="black", | |
| ) | |
| plt.tight_layout() | |
| if args.out_dir is not None: | |
| out_dir = args.out_dir | |
| elif Path("checkpoint_data").is_dir(): | |
| out_dir = Path("checkpoint_data") | |
| elif (Path(__file__).resolve().parent.parent / "checkpoint_data").is_dir(): | |
| out_dir = Path(__file__).resolve().parent.parent / "checkpoint_data" | |
| else: | |
| out_dir = Path(".") | |
| out_dir.mkdir(parents=True, exist_ok=True) | |
| pdf_path = out_dir / f"{args.stem}.pdf" | |
| png_path = out_dir / f"{args.stem}.png" | |
| plt.savefig(pdf_path, bbox_inches="tight") | |
| plt.savefig(png_path, dpi=300, bbox_inches="tight") | |
| print(f"Saved: {pdf_path}") | |
| print(f"Saved: {png_path}") | |
| if args.show: | |
| plt.show() | |
| plt.close(fig) | |
| if __name__ == "__main__": | |
| main() | |