926 / mind3d_trellis2 /code /make_arch_tikz.py
jamie33's picture
Restore mind3d_trellis2 (docs, code, results, renders, paper)
7c61ee6 verified
Raw History Blame Contribute Delete
7.97 kB
"""Overview figure in TikZ (vector, paper fonts) -> architecture.{pdf,svg,png}.
Assets: outputs/fig_assets (make_fig_assets.py), one real test object."""
import json
import os
import subprocess
import sys
import numpy as np
A = "/home/hubin/trellis_work/outputs/fig_assets"
OUT = sys.argv[1] if len(sys.argv) > 1 else "/home/hubin/trellis_work/outputs/fig_arch"
os.makedirs(OUT, exist_ok=True)
meta = json.load(open(f"{A}/meta.json"))
pred, gt = np.array(meta["coef_pred"]), np.array(meta["coef_gt"])
smax = np.abs(np.concatenate([pred, gt])).max()
def bars(x0, y0, vals, color):
"""16 signed bars centred on y0, signed-sqrt scaled."""
out, w = [], 0.085
for j, v in enumerate(vals):
h = float(np.sign(v) * np.sqrt(abs(v) / smax)) * 0.48
x = x0 + j * (w + 0.02)
out.append(rf"\fill[{color}] ({x:.3f},{y0:.3f}) rectangle ({x + w:.3f},{y0 + h:.3f});")
out.append(rf"\draw[black!40, line width=0.3pt] ({x0 - 0.03:.3f},{y0:.3f}) -- ({x0 + 16 * (w + 0.02):.3f},{y0:.3f});")
return "\n".join(out)
TEX = r"""\documentclass[tikz,border=2pt]{standalone}
\usepackage{amsmath,amssymb}
\usepackage{times}
\usetikzlibrary{arrows.meta,positioning,calc}
\definecolor{frz}{HTML}{E8EEF7}\definecolor{frzd}{HTML}{4A6FA5}
\definecolor{trn}{HTML}{FCE9D9}\definecolor{trnd}{HTML}{D9762B}
\definecolor{lane}{HTML}{F5F5F5}\definecolor{pred}{HTML}{D9762B}\definecolor{gtc}{HTML}{4A6FA5}
\definecolor{ok}{HTML}{2E8B57}\definecolor{bad}{HTML}{C0392B}
\tikzset{
box/.style={draw=#1d, fill=#1, rounded corners=3pt, line width=0.6pt, align=center,
minimum height=1.1cm, minimum width=1.9cm, inner sep=2pt, font=\footnotesize},
arr/.style={-{Stealth[length=4pt,width=4pt]}, line width=0.7pt, black!70},
lbl/.style={font=\scriptsize, align=center, text=black!80},
sub/.style={draw=frzd, fill=white, rounded corners=2pt, font=\scriptsize, minimum width=1.0cm,
minimum height=0.48cm, inner sep=1pt},
}
\newcommand{\snow}[1]{\begin{scope}[shift={(#1)}, frzd, line width=0.45pt]
\foreach \a in {0,60,120} {\draw (\a:0.085) -- (\a+180:0.085);}\end{scope}}
\newcommand{\flame}[1]{\begin{scope}[shift={(#1)}]
\fill[trnd] (0,-0.075) .. controls (0.085,-0.045) and (0.065,0.04) .. (0,0.105)
.. controls (-0.065,0.04) and (-0.085,-0.045) .. (0,-0.075);\end{scope}}
\begin{document}
\begin{tikzpicture}
% ---------------- lanes
\fill[lane, rounded corners=4pt] (0,3.0) rectangle (17.7,5.65);
\fill[lane, rounded corners=4pt] (0,0) rectangle (9.75,2.4);
\fill[lane, rounded corners=4pt] (9.95,0) rectangle (17.7,2.4);
\node[anchor=north west, font=\footnotesize] at (0.05,5.62) {\textbf{(a) Reconstruction} (inference: fMRI only)};
\node[anchor=north west, font=\footnotesize] at (0.05,2.37) {\textbf{(b) Regression target} (training objects only)};
\node[anchor=north west, font=\footnotesize] at (10.0,2.37) {\textbf{(c) Example} (test object, unseen in training)};
% legend
\begin{scope}[shift={(11.1,5.42)}]
\fill[frz, draw=frzd, rounded corners=1pt] (0,-0.1) rectangle (0.3,0.1); \snow{0.15,0}
\node[anchor=west, font=\scriptsize] at (0.33,0) {frozen};
\fill[trn, draw=trnd, rounded corners=1pt] (1.35,-0.1) rectangle (1.65,0.1); \flame{1.5,0}
\node[anchor=west, font=\scriptsize] at (1.68,0) {trained (18M)};
\draw[bad, line width=0.7pt, -{Stealth[length=4pt]}] (3.35,0) -- (3.75,0);
\node[anchor=west, font=\scriptsize] at (3.78,0) {loss};
\draw[frzd, dashed, line width=0.7pt, -{Stealth[length=4pt]}] (4.55,0) -- (4.95,0);
\node[anchor=west, font=\scriptsize] at (4.98,0) {shared};
\end{scope}
% ---------------- (a) inference
\node at (0.6,4.3) {\includegraphics[width=1.0cm]{fmri_t2.png}};
\node at (0.8,4.15) {\includegraphics[width=1.0cm]{fmri_t4.png}};
\node at (1.0,4.0) {\includegraphics[width=1.0cm]{fmri_t6.png}};
\node[lbl] at (0.8,3.27) {fMRI, frames 2--7};
\node[box=frz] (enc) at (2.95,4.15) {fMRI encoder\\[-1pt]{\scriptsize ViT-L (MinD-3D)}};
\snow{$(enc.north east)+(-0.17,-0.17)$}
\node[box=trn] (head) at (5.25,4.15) {Mapping head\\[-1pt]{\scriptsize Perceiver + linear}};
\flame{$(head.north east)+(-0.17,-0.19)$}
\draw[arr] (1.55,4.15) -- (enc.west);
\draw[arr] (enc.east) -- (head.west);
BARS_PRED
\node[lbl] at (7.39,4.88) {predicted $\hat c\in\mathbb{R}^{16}$};
\draw[arr] (head.east) -- (6.5,4.15);
\node[box=frz, minimum width=2.15cm] (dec) at (9.75,4.15) {Token decoder\\[-1pt]{\scriptsize $\hat T=\mathrm{LN}(\mu+\hat c B)$}};
\snow{$(dec.north east)+(-0.17,-0.17)$}
\draw[arr] (8.3,4.15) -- (dec.west);
\node (tok) at (11.55,4.15) {\includegraphics[width=0.95cm]{tokens_pred.png}};
\node[lbl] at (11.55,3.35) {$\hat T$: $1029{\times}1024$};
\draw[arr] (dec.east) -- (11.0,4.15);
\node[box=frz, minimum width=3.75cm, minimum height=1.75cm] (tr) at (14.35,4.15) {};
\node[font=\footnotesize, anchor=north] at ($(tr.north)+(0,-0.03)$) {TRELLIS.2-4B};
\snow{$(tr.north east)+(-0.17,-0.17)$}
\node[sub] (s1) at (13.17,4.12) {SS flow};
\node[sub] (s2) at (14.35,4.12) {SLat flow};
\node[sub] (s3) at (15.53,4.12) {decoder};
\draw[arr, line width=0.5pt] (s1) -- (s2); \draw[arr, line width=0.5pt] (s2) -- (s3);
\node[font=\scriptsize, text=black!75, anchor=south] at ($(tr.south)+(0,0.04)$) {CFG $w{=}50$, negative condition $\mathbf{0}$};
\draw[arr] (12.1,4.15) -- (tr.west);
\node at (17.0,4.25) {\includegraphics[width=1.2cm]{mesh_ours.png}};
\node[lbl] at (17.0,3.35) {3D mesh};
\draw[arr] (tr.east) -- (16.4,4.15);
% ---------------- (b) target
\node at (0.8,1.25) {\includegraphics[width=1.05cm]{stimulus_prep.png}};
\node[lbl] at (0.8,0.3) {stimulus, frame 24};
\node[box=frz] (dino) at (2.95,1.2) {DINOv3 ViT-L/16\\[-1pt]{\scriptsize GT tokens $T$}};
\snow{$(dino.north east)+(-0.17,-0.17)$}
\draw[arr] (1.38,1.2) -- (dino.west);
\node[box=frz] (pca) at (5.25,1.2) {PCA projection\\[-1pt]{\scriptsize $c=(T-\mu)B^{\top}$}};
\snow{$(pca.north east)+(-0.17,-0.17)$}
\draw[arr] (dino.east) -- (pca.west);
BARS_GT
\node[lbl] at (7.39,0.3) {target $c\in\mathbb{R}^{16}$};
\draw[arr] (pca.east) -- (6.5,1.2);
\node[font=\scriptsize, text=bad, align=left, anchor=west] at (8.32,1.3) {$\|\hat c-c\|^2$\\$+\,\lambda\,\mathrm{InfoNCE}$\\(no class labels)};
% loss and shared basis
\draw[bad, line width=0.75pt, {Stealth[length=4pt]}-{Stealth[length=4pt]}] (7.39,1.75) -- (7.39,3.6);
\draw[bad, line width=0.75pt, -{Stealth[length=4pt]}] (7.39,2.85) -- (4.8,2.85) -- (4.8,3.58);
\draw[frzd, dashed, line width=0.7pt, -{Stealth[length=4pt]}] (5.9,1.77) -- (5.9,2.58) -- (9.75,2.58) -- (9.75,3.58);
\node[font=\scriptsize, text=frzd, fill=white, inner sep=1pt, anchor=west] at (10.0,2.7) {$\mu, B$ fit on 1200 training objects};
% ---------------- (c) example
\node at (11.0,1.3) {\includegraphics[width=1.35cm]{gt_points.png}};
\node at (12.9,1.3) {\includegraphics[width=1.35cm]{mesh_mind3d.png}};
\node at (14.8,1.3) {\includegraphics[width=1.35cm]{mesh_ours.png}};
\node at (16.7,1.3) {\includegraphics[width=1.35cm]{mesh_oracle.png}};
\node[lbl] at (11.0,0.3) {GT points\\[-2pt]\phantom{C}};
\node[lbl] at (12.9,0.3) {MinD-3D\\[-2pt]\textcolor{bad}{CD 21.78}};
\node[lbl] at (14.8,0.3) {\textbf{Ours}\\[-2pt]\textcolor{ok}{CD 3.49}};
\node[lbl] at (16.7,0.3) {GT image (bound)\\[-2pt]CD 0.33};
\end{tikzpicture}
\end{document}
"""
tex = TEX.replace("BARS_PRED", bars(6.55, 4.15, pred, "pred")).replace("BARS_GT", bars(6.55, 1.2, gt, "gtc"))
open(f"{OUT}/architecture.tex", "w").write(tex)
for f in os.listdir(A):
if f.endswith(".png") and not os.path.exists(f"{OUT}/{f}"):
os.symlink(f"{A}/{f}", f"{OUT}/{f}")
r = subprocess.run(["pdflatex", "-interaction=nonstopmode", "architecture.tex"], cwd=OUT, capture_output=True, text=True)
print("\n".join(l for l in r.stdout.splitlines() if l.startswith("!") or "Overfull" in l) or "compiled")
import pymupdf
d = pymupdf.open(f"{OUT}/architecture.pdf")
open(f"{OUT}/architecture.svg", "w").write(d[0].get_svg_image(text_as_path=True))
d[0].get_pixmap(dpi=300).save(f"{OUT}/architecture.png")
print(d[0].rect)