File size: 16,732 Bytes
eea47ad | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | """Render the CTA (A1_full) framework — v3, complete training-time view.
Improvements over v2:
* Cross-attention internals shown (Q from tgt-modality, K/V from src-modality).
* All FIVE losses drawn as separate dashed back-arrows, color-coded.
* detach() barrier explicit (red ⊥ symbol on the s_asym→classifier path).
* is_real mask shown on loss_av/loss_va (★ REAL only badges).
* Real-sample numbers from trace_cta_training.log are annotated next to
L_AV / L_VA / s_asym / score boxes.
Output: outputs/analysis/figs_framework/cta_framework_v3.{png,pdf}
Run from project root:
python3 scripts/analysis/draw_cta_framework_v3.py
"""
from __future__ import annotations
from pathlib import Path
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.patches import FancyArrowPatch, FancyBboxPatch, Rectangle
OUT_DIR = Path("outputs/analysis/figs_framework")
OUT_DIR.mkdir(parents=True, exist_ok=True)
# ============================================================
# Palette
# ============================================================
C_REAL = "#1E8449"
C_FAKE = "#C0392B"
C_SHARED = "#5D6D7E"
C_AUDIO = "#B7950B"
C_DATA_REAL_FILL = "#E8F8F0"
C_DATA_FAKE_FILL = "#FBEAEA"
C_DATA_AUDIO_FILL = "#FCF3CF"
C_BB_FILL = "#EAF2F8"
C_BB_EDGE = "#5499C7"
C_CORE_FILL = "#FDEBD0"
C_CORE_EDGE = "#D35400"
C_CORE_TEXT = "#7B241C"
C_OUT_FILL = "#E8DAEF"
C_OUT_EDGE = "#7D3C98"
C_PANEL_LOSS = "#FEF9E7"
C_PANEL_LOSS_EDGE = "#B7950B"
# Loss arrow colors — one per loss
C_L_AV = "#1E8449" # green
C_L_VA = "#138D75" # teal
C_L_ASYM = "#7D3C98" # purple
C_L_CLS = "#1F618D" # blue
C_L_AUX = "#C0392B" # red
# ============================================================
# Helpers
# ============================================================
def box(ax, cx, cy, w, h, text, fill, edge, *,
fontsize=10, weight="normal", lw=1.5, padding=0.4, star=False,
text_color="black"):
rect = FancyBboxPatch(
(cx - w/2, cy - h/2), w, h,
boxstyle=f"round,pad={padding}",
facecolor=fill, edgecolor=edge, linewidth=lw,
)
ax.add_patch(rect)
label = ("★ " + text) if star else text
ax.text(cx, cy, label, ha="center", va="center",
fontsize=fontsize, weight=weight, color=text_color)
def arrow(ax, x1, y1, x2, y2, *, color=C_SHARED, lw=1.6, dashed=False,
mutation=14, alpha=1.0, connectionstyle="arc3,rad=0"):
ls = (0, (5, 3)) if dashed else "-"
a = FancyArrowPatch(
(x1, y1), (x2, y2),
arrowstyle="->", mutation_scale=mutation,
color=color, linewidth=lw, linestyle=ls, alpha=alpha,
shrinkA=2, shrinkB=2, connectionstyle=connectionstyle,
)
ax.add_patch(a)
def small(ax, cx, cy, text, *, fontsize=8.5, color="#566573", italic=True,
ha="center", weight="normal"):
style = "italic" if italic else "normal"
ax.text(cx, cy, text, ha=ha, va="center",
fontsize=fontsize, style=style, color=color, weight=weight)
def detach_barrier(ax, x, y, length=2.0, color="#C0392B"):
"""Draw a small ⊥ to indicate detach() / gradient barrier."""
ax.plot([x - length/2, x + length/2], [y, y],
color=color, linewidth=2.5, zorder=5)
ax.plot([x, x], [y - length/3, y + length/3],
color=color, linewidth=2.5, zorder=5)
# ============================================================
# Canvas — wider to fit cross-attention internals
# ============================================================
fig, ax = plt.subplots(figsize=(20, 13))
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)
ax.set_aspect("equal")
ax.axis("off")
# ---- Title ----
ax.text(50, 96.5, "CTA Framework — Forward Pass + Five Losses + Gradient Routing",
ha="center", va="center", fontsize=17, weight="bold")
small(ax, 50, 93.5,
"Solid arrows = forward. Dashed arrows = gradient (color-coded by loss). "
"★ marks our four innovations. ⊥ marks detach() barrier.",
fontsize=9.5, italic=True, color="#566573")
# ---- tiny legend (forward / shared / detach) ----
ax.plot([4, 7], [89.5, 89.5], color=C_REAL, lw=2.4)
ax.text(7.4, 89.5, "real path", fontsize=8.5, va="center", color=C_REAL, weight="bold")
ax.plot([15, 18], [89.5, 89.5], color=C_FAKE, lw=2.4, linestyle=(0, (5, 3)))
ax.text(18.4, 89.5, "fake path", fontsize=8.5, va="center", color=C_FAKE, weight="bold")
ax.plot([26, 29], [89.5, 89.5], color=C_SHARED, lw=2.4)
ax.text(29.4, 89.5, "shared", fontsize=8.5, va="center", color=C_SHARED)
# ============================================================
# INPUTS (y ≈ 84)
# ============================================================
y_in = 85
box(ax, 10, y_in, 14, 4.5,
"Real video",
C_DATA_REAL_FILL, C_REAL, fontsize=10)
box(ax, 26, y_in, 14, 4.5,
"Fake video",
C_DATA_FAKE_FILL, C_FAKE, fontsize=10)
box(ax, 49, y_in, 22, 4.5,
"Audio ★ ALWAYS REAL",
C_DATA_AUDIO_FILL, C_AUDIO, fontsize=10, weight="bold",
text_color="#7E5109")
small(ax, 10, y_in - 3.2, "label = 0", fontsize=8, italic=False, color=C_REAL)
small(ax, 26, y_in - 3.2, "label = 1", fontsize=8, italic=False, color=C_FAKE)
small(ax, 49, y_in - 3.2, "(generator only synthesizes video)", fontsize=8, italic=True)
# ============================================================
# BACKBONES (y ≈ 75)
# ============================================================
y_bb = 75
box(ax, 18, y_bb, 26, 5,
"VideoMAE-base (freeze 70 %)\nv.tokens (B,1568,768) , v.pooled (B,768)",
C_BB_FILL, C_BB_EDGE, fontsize=9.5)
box(ax, 49, y_bb, 22, 5,
"Wav2Vec2-base (freeze 80 %)\na.tokens (B,127,768) , a.pooled (B,768)",
C_BB_FILL, C_BB_EDGE, fontsize=9.5)
# input → backbone arrows
arrow(ax, 10, y_in - 4.5, 14, y_bb + 2.5, color=C_REAL, lw=2.0)
arrow(ax, 26, y_in - 4.5, 22, y_bb + 2.5, color=C_FAKE, lw=2.0, dashed=True)
arrow(ax, 49, y_in - 4.5, 49, y_bb + 2.5, color=C_SHARED, lw=2.0)
# ============================================================
# CROSS-MODAL PREDICTORS (y ≈ 60) with cross-attention insets
# ============================================================
y_pr = 60
# left predictor (A→V)
predA = FancyBboxPatch(
(5, y_pr - 5), 30, 10,
boxstyle="round,pad=0.4",
facecolor=C_CORE_FILL, edgecolor=C_CORE_EDGE, linewidth=2.5,
)
ax.add_patch(predA)
ax.text(20, y_pr + 3, r"★ $f_{A \to V}$ predictor",
ha="center", fontsize=11, weight="bold", color=C_CORE_TEXT)
ax.text(20, y_pr + 1.4, "Transformer Decoder × 4",
ha="center", fontsize=9, color=C_CORE_TEXT)
# attention sub-block
ax.text(13, y_pr - 1.2, "Q", ha="center", fontsize=9, color="black", weight="bold")
ax.text(20, y_pr - 1.2, "K, V", ha="center", fontsize=9, color="black", weight="bold")
ax.text(13, y_pr - 2.4, "(v.tokens)", ha="center", fontsize=7.5, style="italic", color="#566573")
ax.text(20, y_pr - 2.4, "(a.tokens)", ha="center", fontsize=7.5, style="italic", color="#566573")
ax.text(27, y_pr - 1.8, "→ v_pred", ha="center", fontsize=9.5, color=C_CORE_TEXT, weight="bold")
small(ax, 20, y_pr - 4.2, "trained on REAL pairs only",
fontsize=8.2, italic=True, color=C_REAL, weight="bold")
# right predictor (V→A)
predB = FancyBboxPatch(
(40, y_pr - 5), 30, 10,
boxstyle="round,pad=0.4",
facecolor=C_CORE_FILL, edgecolor=C_CORE_EDGE, linewidth=2.5,
)
ax.add_patch(predB)
ax.text(55, y_pr + 3, r"★ $f_{V \to A}$ predictor",
ha="center", fontsize=11, weight="bold", color=C_CORE_TEXT)
ax.text(55, y_pr + 1.4, "Transformer Decoder × 4",
ha="center", fontsize=9, color=C_CORE_TEXT)
ax.text(48, y_pr - 1.2, "Q", ha="center", fontsize=9, color="black", weight="bold")
ax.text(55, y_pr - 1.2, "K, V", ha="center", fontsize=9, color="black", weight="bold")
ax.text(48, y_pr - 2.4, "(a.tokens)", ha="center", fontsize=7.5, style="italic", color="#566573")
ax.text(55, y_pr - 2.4, "(v.tokens)", ha="center", fontsize=7.5, style="italic", color="#566573")
ax.text(62, y_pr - 1.8, "→ a_pred", ha="center", fontsize=9.5, color=C_CORE_TEXT, weight="bold")
small(ax, 55, y_pr - 4.2, "trained on REAL pairs only",
fontsize=8.2, italic=True, color=C_REAL, weight="bold")
# arrows backbone → predictor (only the dominant tgt arrows; src arrows shown as ghosts)
arrow(ax, 18, y_bb - 2.5, 13, y_pr + 5, color=C_SHARED, lw=1.6)
arrow(ax, 49, y_bb - 2.5, 20, y_pr + 5, color=C_SHARED, lw=1.0, alpha=0.5)
arrow(ax, 49, y_bb - 2.5, 48, y_pr + 5, color=C_SHARED, lw=1.6)
arrow(ax, 18, y_bb - 2.5, 55, y_pr + 5, color=C_SHARED, lw=1.0, alpha=0.5)
# ============================================================
# RESIDUALS (y ≈ 47) — per-sample MSE
# ============================================================
y_res = 47
box(ax, 13, y_res, 22, 5,
"$L_{A \\to V}$ = MSE(v_pred, v.tokens)\n[per-sample scalar]",
C_CORE_FILL, C_CORE_EDGE, fontsize=10, lw=2.0)
box(ax, 55, y_res, 22, 5,
"$L_{V \\to A}$ = MSE(a_pred, a.tokens)\n[per-sample scalar]",
C_CORE_FILL, C_CORE_EDGE, fontsize=10, lw=2.0)
arrow(ax, 13, y_pr - 5, 13, y_res + 2.5, color=C_SHARED, lw=1.4)
arrow(ax, 62, y_pr - 5, 55, y_res + 2.5, color=C_SHARED, lw=1.4)
# real / fake values from trace_cta_training.log
small(ax, 13, y_res - 4.8,
"real: 0.001 fake: 0.012 (×12)",
fontsize=8, italic=False, color="#1B4F72")
small(ax, 55, y_res - 4.8,
"real: 0.0004 fake: 0.0023 (×6)",
fontsize=8, italic=False, color="#1B4F72")
# ============================================================
# ASYMMETRY SCORE (y ≈ 36)
# ============================================================
y_asym = 36
box(ax, 34, y_asym, 50, 5,
r"$s_{\rm asym}$ = $L_{V \to A}$ − $L_{A \to V}$ "
r"$L_{\rm total}$ = $L_{V \to A}$ + $L_{A \to V}$",
C_CORE_FILL, C_CORE_EDGE, fontsize=10.5, lw=2.5, star=True,
text_color=C_CORE_TEXT, weight="bold")
small(ax, 34, y_asym - 3.6,
"real $s_{\\rm asym}$ ≈ -0.0007 fake $s_{\\rm asym}$ ≈ -0.010 (gap × 14)",
fontsize=8, italic=False, color="#1B4F72")
arrow(ax, 13, y_res - 2.5, 25, y_asym + 2.5, color=C_SHARED, lw=1.4)
arrow(ax, 55, y_res - 2.5, 43, y_asym + 2.5, color=C_SHARED, lw=1.4)
# ============================================================
# DETACH BARRIER + CLASSIFIER HEAD
# ============================================================
y_cls = 25.5
# vertical arrow from s_asym down to classifier, with detach() barrier
ax.annotate("", xy=(34, y_cls + 3.5), xytext=(34, y_asym - 2.5),
arrowprops=dict(arrowstyle="->", lw=1.6, color=C_SHARED))
detach_barrier(ax, 34, y_asym - 4.6, length=3.0, color="#C0392B")
ax.text(36.5, y_asym - 4.6, ".detach()",
fontsize=9, color="#C0392B", weight="bold", style="italic", va="center")
box(ax, 34, y_cls, 56, 6,
"Classifier Head MLP (1538 → 256 → 1)\n"
r"input = concat[ v.pooled , a.pooled , $s_{\rm asym}$ , $L_{\rm total}$ ]",
C_BB_FILL, C_BB_EDGE, fontsize=10)
# pooled features feed in from the side (subtle long curves)
arrow(ax, 18, y_bb - 2.5, 8, y_cls + 1, color=C_SHARED, lw=0.7, alpha=0.45,
connectionstyle="arc3,rad=-0.3")
arrow(ax, 49, y_bb - 2.5, 60, y_cls + 1, color=C_SHARED, lw=0.7, alpha=0.45,
connectionstyle="arc3,rad=0.3")
small(ax, 11, y_cls + 4, "v.pooled", fontsize=7.5, color="#7F8C8D")
small(ax, 57, y_cls + 4, "a.pooled", fontsize=7.5, color="#7F8C8D")
# ============================================================
# OUTPUT
# ============================================================
y_out = 14.5
box(ax, 34, y_out, 28, 5,
"score = sigmoid(logit) ∈ [0, 1]\nhigh ⇒ fake real: 0.0006 fake: 0.997",
C_OUT_FILL, C_OUT_EDGE, fontsize=10, weight="bold")
arrow(ax, 34, y_cls - 3, 34, y_out + 2.5, color=C_SHARED, lw=1.6)
# ============================================================
# FIVE LOSS BACK-ARROWS (color-coded; dashed; with badges)
# ============================================================
def loss_arrow(x1, y1, x2, y2, color, lw=1.6, rad=0.0):
a = FancyArrowPatch(
(x1, y1), (x2, y2),
arrowstyle="->", mutation_scale=14,
color=color, linewidth=lw, linestyle=(0, (4, 3)),
shrinkA=3, shrinkB=3,
connectionstyle=f"arc3,rad={rad}",
)
ax.add_patch(a)
# loss_av: green dashed back-arrow, L_AV → A→V predictor (only this loss)
loss_arrow(13, y_res + 2.5, 13, y_pr - 5, C_L_AV, lw=2.0, rad=-0.4)
ax.text(7.5, 51.5, r"$loss_{av}$",
fontsize=10, color=C_L_AV, weight="bold")
ax.text(7.5, 49.6, "★ REAL only",
fontsize=7.5, color=C_L_AV, weight="bold", style="italic")
ax.text(7.5, 47.7, "(is_real mask)",
fontsize=7, color="#566573", style="italic")
# loss_va: teal dashed back-arrow, L_VA → V→A predictor
loss_arrow(55, y_res + 2.5, 55, y_pr - 5, C_L_VA, lw=2.0, rad=0.4)
ax.text(66, 51.5, r"$loss_{va}$",
fontsize=10, color=C_L_VA, weight="bold")
ax.text(66, 49.6, "★ REAL only",
fontsize=7.5, color=C_L_VA, weight="bold", style="italic")
ax.text(66, 47.7, "(is_real mask)",
fontsize=7, color="#566573", style="italic")
# loss_asym: purple, from s_asym box back to BOTH predictors
loss_arrow(20, y_asym + 2.5, 20, y_pr - 5, C_L_ASYM, lw=1.4, rad=-0.45)
loss_arrow(48, y_asym + 2.5, 48, y_pr - 5, C_L_ASYM, lw=1.4, rad=0.45)
ax.text(34, y_asym + 6.5,
r"$loss_{asym}$ = ReLU($s_f - s_r$) → margin (BOTH)",
ha="center", fontsize=9.5, color=C_L_ASYM, weight="bold")
# loss_cls: blue, from output back to classifier; STOPS at detach() barrier
# (visualized by the arrow ending at y_cls and a separate ⊥ to predictors)
loss_arrow(34, y_out + 2.5, 34, y_cls - 3, C_L_CLS, lw=2.0, rad=0.0)
ax.text(46, y_cls + 4.5,
r"$loss_{cls}$ = BCE(score, label) → classifier (BOTH)",
ha="center", fontsize=9.5, color=C_L_CLS, weight="bold")
# explicit "blocked by detach" annotation on the path classifier→predictor
ax.text(40, y_asym - 4.6, " ← BCE gradient blocked here",
fontsize=7.5, color="#C0392B", style="italic", va="center")
# loss_aux: red, only on fake (drawn as a small badge on the side)
ax.text(82, 38, r"$loss_{aux}$",
fontsize=10, color=C_L_AUX, weight="bold")
ax.text(82, 36.5,
r"= MSE($s_{\rm asym}$, $s_{\rm asym, alt\_gen}$)",
fontsize=8, color=C_L_AUX, style="italic")
ax.text(82, 35,
"★ paired FAKEs (cross-generator)",
fontsize=7.5, color=C_L_AUX, weight="bold")
loss_arrow(82, 39.2, 70, y_pr - 4, C_L_AUX, lw=1.0, rad=0.4)
# ============================================================
# Right-side LEGEND PANEL of the 5 losses
# ============================================================
lp_w, lp_h = 28, 24
lp_cx, lp_cy = 86, 14
lp = FancyBboxPatch(
(lp_cx - lp_w/2, lp_cy - lp_h/2), lp_w, lp_h,
boxstyle="round,pad=0.5",
facecolor=C_PANEL_LOSS, edgecolor=C_PANEL_LOSS_EDGE, linewidth=2,
)
ax.add_patch(lp)
ax.text(lp_cx, lp_cy + lp_h/2 - 1.6, "Five Losses & gradient routing",
ha="center", fontsize=11, weight="bold")
rows = [
(r"$loss_{av}$", C_L_AV, "REAL only", "→ A→V predictor + backbones"),
(r"$loss_{va}$", C_L_VA, "REAL only", "→ V→A predictor + backbones"),
(r"$loss_{asym}$", C_L_ASYM, "BOTH (margin)", "→ both predictors (weak)"),
(r"$loss_{cls}$", C_L_CLS, "BOTH (BCE)", "→ classifier head only"),
(r"$loss_{aux}$", C_L_AUX, "paired FAKEs", "→ both predictors (cross-gen)"),
]
ay = lp_cy + lp_h/2 - 4
for name, color, mask, dest in rows:
ax.plot([lp_cx - lp_w/2 + 1.0, lp_cx - lp_w/2 + 3.5],
[ay + 0.3, ay + 0.3],
color=color, lw=2.2, linestyle=(0, (4, 3)))
ax.text(lp_cx - lp_w/2 + 4.0, ay + 0.3, name,
fontsize=9, color=color, weight="bold")
ax.text(lp_cx - lp_w/2 + 9, ay + 0.3, mask,
fontsize=7.5, color="#566573", style="italic")
ax.text(lp_cx - lp_w/2 + 1.0, ay - 1.5, dest,
fontsize=7.2, color="#34495E")
ay -= 3.8
ax.text(lp_cx, lp_cy - lp_h/2 + 1.5,
r"Total = 1·$loss_{av}$ + 1·$loss_{va}$ + 0.5·$loss_{asym}$"
"\n "
r"+ 1·$loss_{cls}$ + 0.1·$loss_{aux}$",
ha="center", fontsize=8.5, weight="bold")
# ============================================================
# Save
# ============================================================
out_png = OUT_DIR / "cta_framework_v3.png"
out_pdf = OUT_DIR / "cta_framework_v3.pdf"
plt.tight_layout(pad=0.5)
plt.savefig(out_png, dpi=240, bbox_inches="tight", facecolor="white")
plt.savefig(out_pdf, bbox_inches="tight", facecolor="white")
plt.close(fig)
print(f"[draw] wrote {out_png}")
print(f"[draw] wrote {out_pdf}")
|