"""Qualitative figure (vector text): test objects at fixed percentiles of our CD, no hand-picking. Rows: percentiles; columns: stimulus | GT points | MinD-3D | ours | GT category mean | GT image.""" import glob import json import sys import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt import numpy as np from PIL import Image from render_compare import CAT_NAMES, GT_ROOT, O, STIM_DIR, render_points OUT = sys.argv[1] picks = json.load(open(f"{O}/renders/paper/qualitative_percentiles.json")) COLS = [("MinD-3D", f"{O}/mind3d_sub01", "mind3d_official"), ("Ours", f"{O}/ft/frozen_0001/sub0001/test_coef_rescaled_cfg50", "ft_frozen_sub01_k16_cfg50"), ("Cat. mean$^\\dagger$", f"{O}/diag/category_mean", "diag_category_mean"), ("GT image$^\\dagger$", f"{O}/upper_bound_512_f24", "oracle_gt_image")] rows = {k: {r["id"]: r for r in json.load(open(f"{d}/metrics.json"))["rows"]} for _, d, k in COLS} gt_paths = {p.split("/")[-1][:-4]: p for p in glob.glob(f"{GT_ROOT}/*/_/*/*.npy")} def crop(img, pad=0.06): """Square crop around non-background content so that objects fill their cell.""" a = np.asarray(img.convert("RGB")).astype(int) m = np.abs(a - a[0, 0]).sum(-1) > 30 if not m.any(): return img ys, xs = np.where(m) c, h = ((ys.min() + ys.max()) / 2, (xs.min() + xs.max()) / 2), max(np.ptp(ys), np.ptp(xs)) / 2 * (1 + pad) h = min(h, a.shape[0] / 2) return img.crop((int(c[1] - h), int(c[0] - h), int(c[1] + h), int(c[0] + h))) plt.rcParams.update({"font.family": "STIXGeneral", "mathtext.fontset": "stix", "font.size": 7}) n, m = len(picks["ids"]), 2 + len(COLS) fig, ax = plt.subplots(n, m, figsize=(3.32, 0.62 * n + 0.2), gridspec_kw=dict(wspace=0.04, hspace=0.32)) heads = ["Stimulus", "GT points"] + [c[0] for c in COLS] for i, (p, obj) in enumerate(zip(picks["percentiles"], picks["ids"])): cat, uid = obj.split("/") name = f"{cat}_{uid}" imgs = [crop(Image.open(f"{STIM_DIR}/{name}.png"), 0.15), crop(render_points(np.load(gt_paths[uid])))] cds = [None, None] for _, _, key in COLS: imgs.append(crop(Image.open(f"{O}/renders/cells/{key}/{name}.png"))) cds.append(rows[key][obj]["cd"] * 1e3) for j, (im, cd) in enumerate(zip(imgs, cds)): a = ax[i, j] a.imshow(im) a.set_xticks([]), a.set_yticks([]) for s in a.spines.values(): s.set_visible(j == 3) s.set_color("#D9762B"), s.set_linewidth(0.8) if i == 0: a.set_title(heads[j], fontsize=6.2, pad=2, fontweight="bold" if j == 3 else "normal") if cd is not None: col = "black" if j == 3: col = "#2E8B57" if cd < cds[2] else "#C0392B" a.text(0.5, -0.03, f"{cd:.1f}", transform=a.transAxes, ha="center", va="top", fontsize=6, color=col) ax[i, 0].set_ylabel(f"{CAT_NAMES[cat]}\n{p}th pct.", fontsize=6, labelpad=2) fig.savefig(OUT, bbox_inches="tight", pad_inches=0.01, dpi=400)