| """Top-level evaluation script — runs Protocols 1/2/3/4 on a checkpoint.""" |
| from __future__ import annotations |
|
|
| import argparse |
| import json |
| import sys |
| from pathlib import Path |
|
|
| import torch |
| from omegaconf import OmegaConf |
|
|
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) |
|
|
| from src.data import FairTalkingDataModule |
| from src.methods import build_method |
| from src.eval import run_protocol_1, run_protocol_2, run_protocol_3, run_protocol_4 |
|
|
|
|
| def main(): |
| ap = argparse.ArgumentParser() |
| ap.add_argument("--config", required=True, help="path to the Hydra-composed config saved during training") |
| ap.add_argument("--ckpt", required=True) |
| ap.add_argument("--out_dir", required=True) |
| ap.add_argument("--protocols", default="1,2,3,4", |
| help="comma-separated list of protocol IDs to run") |
| args = ap.parse_args() |
|
|
| cfg = OmegaConf.load(args.config) |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
| dm = FairTalkingDataModule(data_cfg=cfg.data, return_paired=False) |
|
|
| model = build_method( |
| method_name=cfg.method.name, |
| method_cfg=cfg.method, |
| backbone_cfg=cfg.backbone, |
| data_cfg=cfg.data, |
| ) |
| state = torch.load(args.ckpt, map_location="cpu") |
| model.load_state_dict(state.get("state_dict", state), strict=False) |
|
|
| out = Path(args.out_dir); out.mkdir(parents=True, exist_ok=True) |
|
|
| prots = [p.strip() for p in args.protocols.split(",") if p.strip()] |
| all_metrics = {} |
| if "1" in prots: |
| all_metrics["protocol1"] = run_protocol_1(model, dm, device, out) |
| if "2" in prots: |
| all_metrics["protocol2"] = run_protocol_2(model, dm, device, out) |
| if "3" in prots: |
| all_metrics["protocol3"] = run_protocol_3(model, dm, device, out) |
| if "4" in prots: |
| all_metrics["protocol4"] = run_protocol_4(model, dm, device, out) |
|
|
| (out / "summary.json").write_text(json.dumps(all_metrics, indent=2)) |
| print(json.dumps(all_metrics, indent=2)) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|