"""Render the CTA framework figure (paper-style). Style is inspired by images/{1,2,3}.jpg: * rounded rectangles with pastel fills (pink / lavender / mint / cream) * black arrows, dashed for detach / auxiliary paths * snowflake glyph for frozen(ish) / stop-grad boundaries * math annotations for l_av, l_va, asym Output: images/cta_framework.png (and .pdf) """ from __future__ import annotations import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt from matplotlib.patches import FancyBboxPatch, FancyArrowPatch, Rectangle from matplotlib.lines import Line2D # ---------- palette (matched to images/*.jpg) -------------------------------- COL_PINK = "#F7C6C7" # main blocks COL_LAVEND = "#D9C7EA" # cross-modal predictor blocks COL_MINT = "#CFE9D7" # loss / metric blocks COL_CREAM = "#F6E7C1" # classifier / feature vector COL_BLUE = "#CBDDF2" # backbone COL_BG = "#FFFFFF" COL_TEXT = "#111111" COL_MUTED = "#666666" COL_ACCENT = "#B22222" # asym highlight (dark red) FIG_W, FIG_H = 17.0, 11.5 def rbox(ax, x, y, w, h, facecolor, text, fontsize=11, edgecolor="black", lw=1.4, boxstyle="round,pad=0.02,rounding_size=0.10", weight="normal", zorder=3): """Rounded-rectangle labelled block.""" patch = FancyBboxPatch( (x, y), w, h, boxstyle=boxstyle, facecolor=facecolor, edgecolor=edgecolor, linewidth=lw, zorder=zorder, ) ax.add_patch(patch) ax.text(x + w / 2, y + h / 2, text, ha="center", va="center", fontsize=fontsize, color=COL_TEXT, weight=weight, zorder=zorder + 1) return patch def arrow(ax, xy_from, xy_to, color="black", lw=1.6, style="-", mutation_scale=14, zorder=4, curved=False, rad=0.0): connector = f"arc3,rad={rad}" if curved else "arc3,rad=0" a = FancyArrowPatch( xy_from, xy_to, arrowstyle="-|>", mutation_scale=mutation_scale, color=color, lw=lw, linestyle=style, connectionstyle=connector, zorder=zorder, ) ax.add_patch(a) return a def snowflake(ax, x, y, size=0.13, color="#2F6DB5"): """Simple 6-arm snowflake glyph -- marks stop-grad / detach.""" import numpy as np for k in range(6): ang = np.deg2rad(60 * k) dx, dy = size * np.cos(ang), size * np.sin(ang) ax.plot([x - dx, x + dx], [y - dy, y + dy], color=color, lw=1.2, zorder=6) ax.plot([x], [y], "o", ms=3.2, color=color, zorder=7) def circle_op(ax, x, y, r=0.11, glyph="+", color="black"): from matplotlib.patches import Circle ax.add_patch(Circle((x, y), r, facecolor="white", edgecolor=color, lw=1.4, zorder=6)) ax.text(x, y, glyph, ha="center", va="center", fontsize=10, weight="bold", zorder=7) # ============================================================================= fig, ax = plt.subplots(figsize=(FIG_W, FIG_H)) ax.set_xlim(0, 17) ax.set_ylim(0, 11.5) ax.set_aspect("equal") ax.set_axis_off() fig.patch.set_facecolor(COL_BG) # ------ Title --------------------------------------------------------------- ax.text(8.5, 11.10, "CTA — Cross-modal Asymmetry for Talking-Face Deepfake Detection", ha="center", va="center", fontsize=17, weight="bold") ax.text(8.5, 10.65, r"Core signal: $\mathrm{asym} \;=\; \ell_{V\!A}-\ell_{AV}$" r" (direction-signed reconstruction gap)", ha="center", va="center", fontsize=12.5, color=COL_ACCENT, style="italic") # ------ Inputs (left column) ------------------------------------------------- rbox(ax, 0.35, 8.15, 1.55, 1.05, COL_BG, "Video clip\n" r"$X_v \in \mathbb{R}^{T\times3\times H\times W}$", fontsize=10, edgecolor="#333") rbox(ax, 0.35, 6.15, 1.55, 1.05, COL_BG, "Audio clip\n" r"$X_a$", fontsize=10, edgecolor="#333") # ------ Backbones ------------------------------------------------------------ rbox(ax, 2.35, 8.10, 2.05, 1.15, COL_BLUE, "Video Backbone\n(TimeSformer)", fontsize=10.5) rbox(ax, 2.35, 6.10, 2.05, 1.15, COL_BLUE, "Audio Backbone\n(HF encoder)", fontsize=10.5) # arrows: X -> backbone arrow(ax, (1.90, 8.67), (2.35, 8.67)) arrow(ax, (1.90, 6.67), (2.35, 6.67)) # backbone output labels: tokens / pooled ax.text(4.55, 9.10, r"$v_\text{tok}$", fontsize=11, color=COL_TEXT) ax.text(4.55, 8.65, r"$v_\text{pool}$", fontsize=11, color=COL_TEXT) ax.text(4.55, 7.10, r"$a_\text{tok}$", fontsize=11, color=COL_TEXT) ax.text(4.55, 6.65, r"$a_\text{pool}$", fontsize=11, color=COL_TEXT) # ------ Cross-modal predictors (center) ------------------------------------- rbox(ax, 5.60, 8.10, 2.55, 1.15, COL_LAVEND, r"$\mathbf{A\!\to\!V}$ predictor" "\n(cross-attention)", fontsize=10.5) rbox(ax, 5.60, 6.10, 2.55, 1.15, COL_LAVEND, r"$\mathbf{V\!\to\!A}$ predictor" "\n(cross-attention)", fontsize=10.5) arrow(ax, (4.40, 8.65), (5.60, 8.65)) arrow(ax, (4.40, 6.90), (5.60, 8.35), curved=True, rad=-0.28) arrow(ax, (4.40, 6.65), (5.60, 6.65)) arrow(ax, (4.40, 8.40), (5.60, 7.00), curved=True, rad=0.28) ax.text(5.05, 8.78, "query", fontsize=8, color=COL_MUTED) ax.text(5.05, 8.12, "src (a)", fontsize=8, color=COL_MUTED) ax.text(5.05, 6.78, "query", fontsize=8, color=COL_MUTED) ax.text(5.05, 7.10, "src (v)", fontsize=8, color=COL_MUTED) # ------ Per-sample losses --------------------------------------------------- rbox(ax, 8.55, 8.35, 2.10, 0.80, COL_MINT, r"$\ell_{AV}=\mathrm{MSE}(\hat v_\text{tok},v_\text{tok})$", fontsize=10) rbox(ax, 8.55, 6.35, 2.10, 0.80, COL_MINT, r"$\ell_{VA}=\mathrm{MSE}(\hat a_\text{tok},a_\text{tok})$", fontsize=10) arrow(ax, (8.15, 8.67), (8.55, 8.75)) arrow(ax, (8.15, 6.67), (8.55, 6.75)) ax.text(9.60, 8.15, "(per-sample, keeps batch dim)", fontsize=8, color=COL_MUTED, ha="center", style="italic") ax.text(9.60, 6.15, "(per-sample, keeps batch dim)", fontsize=8, color=COL_MUTED, ha="center", style="italic") # ------ Asymmetry node ------------------------------------------------------ rbox(ax, 11.35, 7.15, 2.75, 1.05, COL_PINK, r"$\mathrm{asym} = \ell_{VA}-\ell_{AV}$" "\n(direction-signed gap)", fontsize=11.5, weight="bold", edgecolor=COL_ACCENT, lw=2.0) arrow(ax, (10.65, 8.75), (11.60, 8.20), curved=True, rad=-0.20) arrow(ax, (10.65, 6.75), (11.60, 7.20), curved=True, rad=0.20) ax.text(11.00, 7.98, r"$\ell_{AV}$", fontsize=10, color=COL_TEXT) ax.text(11.00, 6.95, r"$\ell_{VA}$", fontsize=10, color=COL_TEXT) # ------ Feature vector for classifier --------------------------------------- rbox(ax, 11.35, 4.60, 2.75, 1.10, COL_CREAM, r"$[\,v_\text{pool},\,a_\text{pool},\,\mathrm{asym},\,\ell_{AV}\!+\!\ell_{VA}\,]$", fontsize=10) arrow(ax, (4.40, 8.40), (11.35, 5.55), curved=True, rad=-0.30, color=COL_MUTED) arrow(ax, (4.40, 6.40), (11.35, 5.30), curved=True, rad=0.30, color=COL_MUTED) arrow(ax, (12.72, 7.15), (12.72, 5.70)) # l_av / l_va (detached) via dashed arrow(ax, (9.60, 8.35), (12.00, 5.70), curved=True, rad=-0.22, style=(0, (5, 4)), color="#333") arrow(ax, (9.60, 6.35), (12.00, 5.70), curved=True, rad=0.22, style=(0, (5, 4)), color="#333") snowflake(ax, 11.05, 7.55) snowflake(ax, 11.05, 5.85) ax.text(10.55, 7.80, ".detach()", fontsize=8, color="#2F6DB5") ax.text(10.55, 5.60, ".detach()", fontsize=8, color="#2F6DB5") # ------ Classifier ---------------------------------------------------------- rbox(ax, 11.55, 3.05, 2.30, 1.00, COL_PINK, "Classifier head\n(Linear-GELU-Do-Linear)", fontsize=10) arrow(ax, (12.72, 4.60), (12.72, 4.05)) rbox(ax, 14.15, 3.05, 1.85, 1.00, COL_BG, r"$\hat y = \sigma(\mathrm{logit})$" "\nreal / fake", fontsize=10, edgecolor="#333") arrow(ax, (13.85, 3.55), (14.15, 3.55)) # ------ Losses row (mid-bottom) -------------------------------------------- rbox(ax, 0.35, 3.35, 3.00, 0.90, COL_MINT, r"$\mathcal{L}_{AV}=\mathrm{mean}_{y=0}\,\ell_{AV}$" "\n" r"$\mathcal{L}_{VA}=\mathrm{mean}_{y=0}\,\ell_{VA}$", fontsize=10) ax.text(1.85, 3.05, "trained ONLY on real", ha="center", fontsize=8, color=COL_ACCENT, style="italic") rbox(ax, 3.85, 3.35, 3.55, 0.90, COL_MINT, r"$\mathcal{L}_\text{asym}=\mathrm{ReLU}" r"(\bar{\mathrm{asym}}_\text{fake}-\bar{\mathrm{asym}}_\text{real})$", fontsize=10) ax.text(5.625, 3.05, "margin=0 -- keep signal weak (predictor no-collapse)", ha="center", fontsize=8, color=COL_ACCENT, style="italic") rbox(ax, 7.90, 3.35, 3.10, 0.90, COL_MINT, r"$\mathcal{L}_\text{aux}=\mathrm{MSE}(\mathrm{asym},\mathrm{asym}')$", fontsize=10) ax.text(9.45, 3.05, "same (ref, audio) -> two fakes", ha="center", fontsize=8, color=COL_ACCENT, style="italic") rbox(ax, 14.05, 4.30, 1.95, 0.65, COL_MINT, r"$\mathcal{L}_\text{cls}=\mathrm{BCE}(\hat y, y)$", fontsize=10) arrow(ax, (13.85, 3.55), (15.00, 4.30), curved=True, rad=-0.28) # arrows: sources -> loss boxes arrow(ax, (11.35, 7.35), (7.40, 4.25), curved=True, rad=0.25) arrow(ax, (11.35, 7.10), (11.00, 4.25), curved=True, rad=0.10) arrow(ax, (8.55, 8.35), (3.35, 4.25), curved=True, rad=0.35, color=COL_MUTED) arrow(ax, (8.55, 6.35), (3.35, 4.25), curved=True, rad=-0.15, color=COL_MUTED) # ------ Total loss ---------------------------------------------------------- rbox(ax, 5.10, 1.95, 6.20, 0.95, COL_CREAM, r"$\mathcal{L}=w_{av}\mathcal{L}_{AV}" r"+w_{va}\mathcal{L}_{VA}" r"+w_\text{asym}\mathcal{L}_\text{asym}" r"+w_\text{cls}\mathcal{L}_\text{cls}" r"+w_\text{aux}\mathcal{L}_\text{aux}$", fontsize=11.5, weight="bold") for x_from in [1.85, 5.625, 9.45]: arrow(ax, (x_from, 3.35), (8.20, 2.90), color=COL_MUTED) arrow(ax, (15.00, 4.30), (11.30, 2.90), color=COL_MUTED, curved=True, rad=-0.15) # ------ Callouts (design notes, moved to bottom) ---------------------------- callouts = [ r"$\bigstar$ predictor only sees real -> learns real audio-visual coupling as prior", r"$\bigstar$ $\ell_{AV},\ell_{VA}$ detached into classifier -> BCE cannot distort predictors", r"$\bigstar$ asym is signed (not $|\cdot|$) -> direction of coupling breakage IS the signature", r"$\bigstar$ $\mathcal{L}_\text{aux}$ pulls asym across generators together -> generator-agnostic", ] notes_x, notes_y = 0.35, 1.30 for i, t in enumerate(callouts): row = i // 2 col = i % 2 ax.text(notes_x + col * 8.0, notes_y - 0.42 * row, t, fontsize=10, color=COL_ACCENT) # ------ Legend -------------------------------------------------------------- legend_x, legend_y = 15.10, 10.20 ax.text(legend_x + 0.65, legend_y + 0.05, "Legend", fontsize=10, weight="bold", ha="center") # swatches sw_specs = [ (COL_BLUE, "backbone"), (COL_LAVEND, "cross-modal predictor"), (COL_MINT, "loss / per-sample metric"), (COL_PINK, "core module (asym / cls)"), (COL_CREAM, "feature vector / total"), ] for i, (c, lab) in enumerate(sw_specs): y = legend_y - 0.25 - i * 0.28 ax.add_patch(Rectangle((legend_x, y), 0.28, 0.20, facecolor=c, edgecolor="black", lw=0.8)) ax.text(legend_x + 0.38, y + 0.10, lab, fontsize=8.5, va="center") # dashed = detach; snowflake = stop-grad y = legend_y - 0.25 - 5 * 0.28 ax.plot([legend_x + 0.02, legend_x + 0.26], [y + 0.10, y + 0.10], linestyle=(0, (5, 4)), color="black", lw=1.4) ax.text(legend_x + 0.38, y + 0.10, "detach / stop-grad", fontsize=8.5, va="center") y -= 0.28 snowflake(ax, legend_x + 0.14, y + 0.10, size=0.08) ax.text(legend_x + 0.38, y + 0.10, "snowflake = .detach()", fontsize=8.5, va="center") y -= 0.28 ax.annotate("", xy=(legend_x + 0.26, y + 0.10), xytext=(legend_x + 0.02, y + 0.10), arrowprops=dict(arrowstyle="-|>", color="black", lw=1.2)) ax.text(legend_x + 0.38, y + 0.10, "forward flow", fontsize=8.5, va="center") # ------ Save --------------------------------------------------------------- import os OUT_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "images") os.makedirs(OUT_DIR, exist_ok=True) png_path = os.path.join(OUT_DIR, "cta_framework.png") pdf_path = os.path.join(OUT_DIR, "cta_framework.pdf") plt.savefig(png_path, dpi=220, bbox_inches="tight", facecolor=COL_BG) plt.savefig(pdf_path, bbox_inches="tight", facecolor=COL_BG) print("saved:", png_path) print("saved:", pdf_path)