"""Parameter-conditioned evaluation. Generates an image for each test conditioning vector, runs the trained ImageRegressor on it, and reports how well the recovered parameters match the ones the sample was conditioned on (per-parameter MSE / MAE / RMSE / R2). python eval_regression.py --data DIR --flow-ckpt X.pth --regressor-ckpt Y.pt """ import argparse, os, sys import numpy as np import torch from tqdm import tqdm from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "code")) sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from common import load_flow_model, load_regressor, sample_cfg, pick_device, minmax_to_unit PARAM_NAMES = ["Einstein radius", "x", "y", "slope beta"] def main(): ap = argparse.ArgumentParser() ap.add_argument("--data", required=True, help="dir of .npy samples (image + 4 params)") ap.add_argument("--flow-ckpt", required=True) ap.add_argument("--regressor-ckpt", required=True) ap.add_argument("--n", type=int, default=1000) ap.add_argument("--batch-size", type=int, default=50) ap.add_argument("--ode-steps", type=int, default=100) ap.add_argument("--cfg-scale", type=float, default=2.0) ap.add_argument("--device", default="auto") ap.add_argument("--seed", type=int, default=0) args = ap.parse_args() torch.manual_seed(args.seed) device = pick_device(args.device) print(f"device: {device}") flow = load_flow_model(args.flow_ckpt, "param", device) reg = load_regressor(args.regressor_ckpt, device) files = sorted(f for f in os.listdir(args.data) if f.endswith(".npy"))[: args.n] if not files: raise SystemExit(f"no .npy files in {args.data}") print(f"evaluating {len(files)} samples") conds = [] for f in files: d = np.load(os.path.join(args.data, f), allow_pickle=True) conds.append(np.asarray(d[1:5], dtype=np.float32)) conds = torch.from_numpy(np.stack(conds)) preds = [] for i in tqdm(range(0, len(conds), args.batch_size), desc="sampling"): batch = conds[i : i + args.batch_size] imgs = sample_cfg(flow, batch, args.ode_steps, args.cfg_scale, device) imgs = torch.stack([minmax_to_unit(im) for im in imgs]) with torch.no_grad(): preds.append(reg(imgs).cpu()) preds = torch.cat(preds).numpy() actual = conds.numpy() print("\nPer-Parameter Metrics (conditioned value vs. value recovered from the generated image)") print("-" * 78) for i, name in enumerate(PARAM_NAMES): a, p = actual[:, i], preds[:, i] rng = a.max() - a.min() mae = mean_absolute_error(a, p) print(f"{name}:") print(f" Range: [{a.min():.3f}, {a.max():.3f}]") print(f" MSE: {mean_squared_error(a, p):.6f}") print(f" MAE: {mae:.6f}" + (f" ({mae / rng * 100:.1f}% of range)" if rng > 0 else " (constant parameter)")) print(f" RMSE: {np.sqrt(mean_squared_error(a, p)):.6f}") print(f" R2: {r2_score(a, p):.6f}" if rng > 0 else " R2: n/a (constant parameter)") print() if __name__ == "__main__": main()