Download mind3d_trellis2/code/paper_plots.py from jamie33/926: direct link, hf CLI and curl.
- Browser
- Download file 3.75 kB
-
https://huggingface.co/datasets/jamie33/926/resolve/main/mind3d_trellis2/code/paper_plots.py
- Command line
-
hf download hf://datasets/jamie33/926/mind3d_trellis2/code/paper_plots.py
-
curl -L -o paper_plots.py https://huggingface.co/datasets/jamie33/926/resolve/main/mind3d_trellis2/code/paper_plots.py
3.75 kB
| """Analysis figure for the paper: (a) CFG sweep, (b) per-dimension decodability, (c) error decomposition.""" | |
| import json | |
| import sys | |
| import matplotlib | |
| matplotlib.use("Agg") | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import torch | |
| sys.path.insert(0, "/home/hubin/trellis_work/mapper") | |
| from pca_tools import get_pca, load_tokens, project, split_ids | |
| O = "/home/hubin/trellis_work/outputs" | |
| OUT = sys.argv[1] | |
| train_ids, _, fit_idx, val_idx = split_ids() | |
| pca = get_pca() | |
| gt = project(torch.from_numpy(load_tokens([train_ids[i] for i in val_idx])).cuda(), pca, 16).cpu().numpy() | |
| pred = np.load(f"{O}/ft/frozen_0001/sub0001_coefs.npz")["val"][1] | |
| r = np.array([np.corrcoef(pred[:, j], gt[:, j])[0, 1] for j in range(16)]) | |
| ev = (pca["evals"][:16] / pca["evals"].sum()).cpu().numpy() | |
| json.dump({"per_dim_r_val": r.tolist(), "explained_var_ratio": ev.tolist()}, | |
| open(f"{O}/supp/per_dim_r.json", "w"), indent=1) | |
| b = json.load(open(f"{O}/analysis/bottleneck.json"))["methods"] | |
| sup = json.load(open(f"{O}/supp/supp_results.json")) | |
| plt.rcParams.update({"font.size": 8, "font.family": "STIXGeneral", "mathtext.fontset": "stix", | |
| "axes.spines.top": False}) | |
| bits = json.load(open(f"{O}/supp/info_bits.json")) | |
| fig, ax = plt.subplots(1, 3, figsize=(7.0, 2.1)) | |
| ridge_test = {3: 12.66, 7.5: 10.62, 12: 9.06, 20: 8.25, 30: 8.12, 50: 8.07} | |
| ours_val = {float(k): v for k, v in sup["val_cfg_sweep"].items()} | |
| ours_val[7.5] = float(np.mean([r["cd"] for r in json.load(open(f"{O}/supp/val_cfg7.5/metrics.json"))["rows"]]) * 1e3) | |
| ours_val = dict(sorted(ours_val.items())) | |
| ax[0].plot(list(ridge_test), list(ridge_test.values()), "o-", ms=3, label="ridge, 64-d (test)") | |
| ax[0].plot(list(ours_val), list(ours_val.values()), "s-", ms=3, label="ours, 16-d (val)") | |
| ax[0].axvline(7.5, color="gray", ls=":", lw=0.8) | |
| ax[0].text(8.0, 6.9, "default", color="gray", fontsize=7) | |
| ax[0].set_xscale("log") | |
| ax[0].set_xticks([3, 7.5, 20, 50, 100]) | |
| ax[0].set_xticklabels(["3", "7.5", "20", "50", "100"]) | |
| ax[0].set_xlabel("CFG weight $w$") | |
| ax[0].set_ylabel(r"CD ($\times 10^{-3}$)") | |
| ax[0].legend(frameon=False, fontsize=7, loc="upper right") | |
| ax[0].set_ylim(5.5, 13.5) | |
| ax[0].set_title("(a) guidance strength", fontsize=8) | |
| ax[1].bar(np.arange(16), r, color="#3b7dd8") | |
| ax2 = ax[1].twinx() | |
| ax2.plot(np.arange(16), ev * 100, "k.-", ms=3, lw=0.8) | |
| ax2.set_ylabel("explained var. (%)", fontsize=7, labelpad=1) | |
| ax2.tick_params(labelsize=7) | |
| ax[1].set_xlabel("PCA dimension") | |
| ax[1].set_ylabel("val. correlation $r$") | |
| ax[1].set_ylim(0, 1) | |
| ax[1].text(15.4, 0.93, f"$\\Sigma$ info $\\approx$ {bits['val']['total']:.1f} bits (val)\n{bits['test']['total']:.1f} bits (test)", | |
| ha="right", va="top", fontsize=7) | |
| ax[1].set_title("(b) decodability per dimension", fontsize=8) | |
| names = [("mind3d", "MinD-3D"), ("ours", "Ours"), ("gt_pca16", "GT\nPCA-16"), ("gt_category_mean", "GT cat.\nmean"), | |
| ("oracle", "GT\nimage")] | |
| x = np.arange(len(names)) | |
| for off, key, lab, col in [(-0.2, "category_correct", "class decodable", "#4caf50"), | |
| (0.2, "category_wrong", "not decodable", "#e57373")]: | |
| v = [b[k][key] for k, _ in names] | |
| ax[2].bar(x + off, v, 0.4, label=lab, color=col) | |
| for xi, vi in zip(x + off, v): | |
| ax[2].text(xi, vi + 0.3, f"{vi:.1f}", ha="center", va="bottom", fontsize=5.5) | |
| ax[2].set_xticks(x) | |
| ax[2].set_xticklabels([n for _, n in names], fontsize=6.5) | |
| ax[2].set_ylabel(r"CD ($\times 10^{-3}$)") | |
| ax[2].set_ylim(0, 18.5) | |
| ax[2].legend(frameon=False, fontsize=6.5, loc="upper right", ncol=1) | |
| ax[2].set_title("(c) error decomposition (test)", fontsize=8) | |
| ax[1].spines["top"].set_visible(False) | |
| ax2.spines["top"].set_visible(False) | |
| plt.tight_layout(pad=0.3, w_pad=1.2) | |
| plt.savefig(OUT) | |
| print("r", np.round(r, 2)) | |