File size: 11,540 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
"""Render the CTA (A1_full) framework — cleaned, paper-grade version.

Output: outputs/analysis/figs_framework/cta_framework.{png,pdf}

Design principles:
  * Two columns: left = data flow (single chain), right = side notes
    (training losses + innovation list).
  * Real/Fake sample paths use color (green / red) but only on the few
    arrows that actually differ. Most arrows are shared (gray).
  * Each innovation is marked with a single ★ inside the box; the
    long captions live in the right-side panel, NOT on the diagram.
  * Generous padding between modules. Total figure ~ 17 x 11.

Run from project root:
    python3 scripts/analysis/draw_cta_framework.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


OUT_DIR = Path("outputs/analysis/figs_framework")
OUT_DIR.mkdir(parents=True, exist_ok=True)

# ============================================================
# Palette
# ============================================================
C_REAL    = "#1E8449"
C_FAKE    = "#C0392B"
C_SHARED  = "#7F8C8D"

C_DATA_REAL_FILL  = "#E8F8F0"
C_DATA_FAKE_FILL  = "#FBEAEA"
C_DATA_AUDIO_FILL = "#FCF3CF"
C_DATA_AUDIO_EDGE = "#B7950B"

C_BB_FILL  = "#EAF2F8"
C_BB_EDGE  = "#5499C7"

C_CORE_FILL = "#FDEBD0"
C_CORE_EDGE = "#D35400"
C_CORE_TEXT = "#7B241C"

C_NEUTRAL_FILL = "#F4F6F7"
C_NEUTRAL_EDGE = "#7F8C8D"

C_OUT_FILL = "#E8DAEF"
C_OUT_EDGE = "#7D3C98"

C_PANEL_LOSS = "#FEF9E7"
C_PANEL_LOSS_EDGE = "#B7950B"
C_PANEL_INNO = "#FDEDEC"
C_PANEL_INNO_EDGE = "#C0392B"


# ============================================================
# Drawing 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):
    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,
    )
    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)


# ============================================================
# Canvas
# ============================================================
fig, ax = plt.subplots(figsize=(17, 11))
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)
ax.set_aspect("equal")
ax.axis("off")


# Title
ax.text(50, 96.5, "CTA: Cross-modal Translation Asymmetry — Framework",
        ha="center", va="center", fontsize=18, weight="bold")
small(ax, 50, 92.8,
      "★ marks the four innovations (described on the right)",
      fontsize=10, italic=True, color="#566573")


# Tiny color legend — top-right corner of the data-flow zone
ax.plot([6, 10], [89, 89], color=C_REAL, lw=2.4)
ax.text(10.6, 89, "real", fontsize=9, va="center", color=C_REAL, weight="bold")
ax.plot([16, 20], [89, 89], color=C_FAKE, lw=2.4, linestyle=(0, (5, 3)))
ax.text(20.6, 89, "fake", fontsize=9, va="center", color=C_FAKE, weight="bold")
ax.plot([26, 30], [89, 89], color=C_SHARED, lw=2.4)
ax.text(30.6, 89, "shared", fontsize=9, va="center", color=C_SHARED)


# ========== LEFT ZONE = single chain (x ∈ [4, 66]) ==========
COL_L, COL_R = 22, 50          # video column / audio column on backbone level
COL_C = (COL_L + COL_R) / 2    # center

# ---- Inputs ----
y_in = 84
box(ax, 12,  y_in, 16, 5, "Real video\n(label = 0)",
    C_DATA_REAL_FILL, C_REAL, fontsize=10)
box(ax, 31,  y_in, 16, 5, "Fake video\n(label = 1)",
    C_DATA_FAKE_FILL, C_FAKE, fontsize=10)
box(ax, 56,  y_in, 22, 5, "Audio  ★ always real",
    C_DATA_AUDIO_FILL, C_DATA_AUDIO_EDGE, fontsize=10, weight="bold",
    text_color="#7E5109")

# ---- Backbones ----
y_bb = 73
box(ax, COL_L, y_bb, 28, 5,
    "VideoMAE-base   (freeze 70 %)",
    C_BB_FILL, C_BB_EDGE, fontsize=10)
box(ax, 56, y_bb, 22, 5,
    "Wav2Vec2-base   (freeze 80 %)",
    C_BB_FILL, C_BB_EDGE, fontsize=10)

# input → backbone arrows
arrow(ax, 12, y_in - 2.6, COL_L - 5, y_bb + 2.6, color=C_REAL, lw=2.0)
arrow(ax, 31, y_in - 2.6, COL_L + 5, y_bb + 2.6, color=C_FAKE, lw=2.0, dashed=True)
arrow(ax, 56, y_in - 2.6, 56, y_bb + 2.6, color=C_SHARED, lw=2.0)

# Token notation (compact, side-of-arrow)
small(ax, 22, y_bb - 4, "v.tokens / v.pooled",
      fontsize=8, italic=True, color="#566573")
small(ax, 56, y_bb - 4, "a.tokens / a.pooled",
      fontsize=8, italic=True, color="#566573")


# ---- Predictors (CORE / Innovation 1+2) ----
y_pr = 60
box(ax, 22, y_pr, 28, 7,
    "$f_{A \\to V}$  predictor\nTransformer Decoder × 4",
    C_CORE_FILL, C_CORE_EDGE, fontsize=10, lw=2.5, star=True,
    text_color=C_CORE_TEXT, weight="bold")
box(ax, 56, y_pr, 28, 7,
    "$f_{V \\to A}$  predictor\nTransformer Decoder × 4",
    C_CORE_FILL, C_CORE_EDGE, fontsize=10, lw=2.5, star=True,
    text_color=C_CORE_TEXT, weight="bold")

# small "REAL only" badges below each predictor — Innovation 2 in 5 chars
small(ax, 22, y_pr - 4.5,
      "trained on real pairs only",
      fontsize=8.2, italic=True, color=C_REAL, weight="bold")
small(ax, 56, y_pr - 4.5,
      "trained on real pairs only",
      fontsize=8.2, italic=True, color=C_REAL, weight="bold")

# arrows backbone → predictors
# v.tokens → A→V (tgt) and V→A (src). a.tokens → A→V (src) and V→A (tgt).
arrow(ax, 22, y_bb - 2.6, 22, y_pr + 4, color=C_SHARED, lw=1.4)        # v→A→V
arrow(ax, 22, y_bb - 2.6, 50, y_pr + 4, color=C_SHARED, lw=1.0,
      alpha=0.55)
arrow(ax, 56, y_bb - 2.6, 56, y_pr + 4, color=C_SHARED, lw=1.4)        # a→V→A
arrow(ax, 56, y_bb - 2.6, 28, y_pr + 4, color=C_SHARED, lw=1.0,
      alpha=0.55)


# ---- Per-sample residuals ----
y_res = 47
box(ax, 22, y_res, 26, 4.5,
    "$L_{A \\to V}$ = MSE(v_pred, v.tokens)",
    C_CORE_FILL, C_CORE_EDGE, fontsize=10, lw=2.0)
box(ax, 56, y_res, 26, 4.5,
    "$L_{V \\to A}$ = MSE(a_pred, a.tokens)",
    C_CORE_FILL, C_CORE_EDGE, fontsize=10, lw=2.0)

arrow(ax, 22, y_pr - 4, 22, y_res + 2.5, color=C_SHARED, lw=1.4)
arrow(ax, 56, y_pr - 4, 56, y_res + 2.5, color=C_SHARED, lw=1.4)


# ---- Asymmetry score (Innovation 3) ----
y_asym = 38
box(ax, 39, y_asym, 50, 5,
    "$s_{\\rm asym}$ = $L_{V\\to A}$ − $L_{A\\to V}$        "
    "$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")

arrow(ax, 22, y_res - 2.3, 30, y_asym + 2.5, color=C_SHARED, lw=1.4)
arrow(ax, 56, y_res - 2.3, 48, y_asym + 2.5, color=C_SHARED, lw=1.4)


# ---- detach (Innovation 4) ----
y_det = 30.5
small(ax, 39, y_det, ".detach()  ★  cuts gradient back to predictors",
      fontsize=10, italic=True, color=C_CORE_TEXT, weight="bold")
arrow(ax, 39, y_asym - 2.5, 39, y_det + 1, color=C_SHARED, lw=1.4)


# ---- Classifier head ----
y_cls = 22.5
box(ax, 39, y_cls, 56, 6,
    "Classifier Head    MLP  (1538 → 256 → 1)\n"
    "input = concat[ v.pooled , a.pooled , $s_{\\rm asym}$ , $L_{\\rm total}$ ]",
    C_BB_FILL, C_BB_EDGE, fontsize=10)
arrow(ax, 39, y_det - 0.7, 39, y_cls + 3, color=C_SHARED, lw=1.4)

# pooled features feed in from the side (subtle)
arrow(ax, 22, y_bb - 2.6, 12, y_cls + 1, color=C_SHARED, lw=0.7,
      alpha=0.45)
arrow(ax, 56, y_bb - 2.6, 66, y_cls + 1, color=C_SHARED, lw=0.7,
      alpha=0.45)


# ---- Output ----
y_out = 13
box(ax, 39, y_out, 28, 4.5,
    "score  = sigmoid(logit) ∈ [0, 1]\nhigh ⇒ fake",
    C_OUT_FILL, C_OUT_EDGE, fontsize=10.5, weight="bold")
arrow(ax, 39, y_cls - 3, 39, y_out + 2.3, color=C_SHARED, lw=1.6)


# ========== RIGHT ZONE = info panels (x ∈ [70, 99]) ==========

# ---- Innovation panel (top-right) ----
inn_w, inn_h = 28, 38
inn_cx, inn_cy = 84, 67
inn = FancyBboxPatch(
    (inn_cx - inn_w/2, inn_cy - inn_h/2), inn_w, inn_h,
    boxstyle="round,pad=0.5",
    facecolor=C_PANEL_INNO, edgecolor=C_PANEL_INNO_EDGE, linewidth=2,
)
ax.add_patch(inn)

ax.text(inn_cx, inn_cy + inn_h/2 - 2,
        "Key Innovations  ★",
        ha="center", fontsize=12.5, weight="bold", color=C_CORE_TEXT)

# Each line: "★ N. Title" then 1-line gloss
ix = inn_cx - inn_w/2 + 1.2
iy = inn_cy + inn_h/2 - 5.0

bullets = [
    ("★  1.  Bidirectional cross-modal predictors",
     "    A→V  and  V→A  (Transformer Decoders)"),
    ("★  2.  Trained on REAL pairs ONLY",
     "    loss_av, loss_va  masked by  label = 0"),
    ("★  3.  Asymmetry score $s_\\mathrm{asym}$  =  detector",
     "    real ≈ 0   ;   fake  ≪ 0    (OOD signature)"),
    ("★  4.  detach()  before classifier head",
     "    keeps predictors from leaking to BCE"),
    ("★  5.  Cross-generator consistency loss",
     "    same num, alt generator  →  same $s_\\mathrm{asym}$"),
]
for title, gloss in bullets:
    ax.text(ix, iy, title, fontsize=9.4, weight="bold", color=C_CORE_TEXT)
    iy -= 1.7
    ax.text(ix, iy, gloss, fontsize=8.2, color="#7B241C", style="italic")
    iy -= 3.6


# ---- Loss panel (bottom-right) ----
lp_w, lp_h = 28, 36
lp_cx, lp_cy = 84, 25
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 - 2.0,
        "Training losses  &  sample masks",
        ha="center", fontsize=11.5, weight="bold")

lx = lp_cx - lp_w/2 + 1.2
ly = lp_cy + lp_h/2 - 5.0

losses = [
    ("$\\mathrm{loss}_{av}$ = mean($L_{A\\to V}$  |  label=0)",
     "REAL only", C_REAL),
    ("$\\mathrm{loss}_{va}$ = mean($L_{V\\to A}$  |  label=0)",
     "REAL only", C_REAL),
    ("$\\mathrm{loss}_{asym}$ = ReLU($s_\\mathrm{fake}$ − $s_\\mathrm{real}$)",
     "BOTH (margin)", "#566573"),
    ("$\\mathrm{loss}_{cls}$ = BCE(score, label)",
     "BOTH", "#566573"),
    ("$\\mathrm{loss}_{aux}$ = MSE($s_\\mathrm{asym}$, $s_\\mathrm{asym, alt}$)",
     "paired FAKEs", C_FAKE),
]
for line, mask, color in losses:
    ax.text(lx, ly, line, fontsize=9.0)
    ly -= 1.5
    ax.text(lx, ly, "      sample mask: " + mask,
            fontsize=8, color=color, style="italic")
    ly -= 2.7

ax.text(lp_cx, lp_cy - lp_h/2 + 3.5,
        "Total = 1·$loss_{av}$ + 1·$loss_{va}$ + 0.5·$loss_{asym}$\n"
        "         + 1·$loss_{cls}$ + 0.1·$loss_{aux}$",
        ha="center", va="center", fontsize=8.8, weight="bold")


# ============================================================
# Save
# ============================================================
out_png = OUT_DIR / "cta_framework.png"
out_pdf = OUT_DIR / "cta_framework.pdf"
plt.tight_layout(pad=0.6)
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}")