| """ |
| Poster/logbook figures for the SPLIT-VLM reproduction: |
| 1. fig_mechanism_heatmaps.png — temporal-shift importance vs CLS attention over |
| the 24x24 patch grid for a real image (illustrates position bias, Claim 3), |
| plus which tokens SPLIT vs attention-topk keep at 64 tokens. |
| 2. fig_mechanism_bars.png — summary bars (center-of-mass row, Gini, Spearman). |
| Reuses the cached CLIP vision tower. |
| """ |
| import os, sys, json |
| import numpy as np |
| import torch |
| import matplotlib |
| matplotlib.use("Agg") |
| import matplotlib.pyplot as plt |
| from PIL import Image |
| sys.path.insert(0, os.path.dirname(__file__)) |
| from split_prune import (temporal_shift_importance, region_ids_grid, split_select, |
| attention_select) |
|
|
| MODEL = "openai/clip-vit-large-patch14-336" |
| DEVICE = os.environ.get("FIGDEV","cpu") |
| GRID = (24, 24) |
| REGION = (4, 4) |
|
|
|
|
| def load_feats(path): |
| from transformers import CLIPVisionModel, CLIPImageProcessor |
| proc = CLIPImageProcessor.from_pretrained(MODEL) |
| model = CLIPVisionModel.from_pretrained(MODEL, torch_dtype=torch.float32, |
| attn_implementation="eager").to(DEVICE).eval() |
| img = Image.open(path).convert("RGB") |
| px = proc(images=img, return_tensors="pt")["pixel_values"].to(DEVICE) |
| with torch.no_grad(): |
| out = model(px, output_hidden_states=True, output_attentions=True) |
| hs = [h[0, 1:, :].float().cpu() for h in out.hidden_states] |
| att = torch.stack([a[0, :, 0, 1:].mean(0) for a in out.attentions]).mean(0).float().cpu() |
| imp = temporal_shift_importance(hs) |
| emb = hs[-1] |
| return img, hs, emb, imp, att |
|
|
|
|
| def grid_img(v): |
| return v.numpy().reshape(GRID) |
|
|
|
|
| def main(): |
| path = "outputs/sample_images/cats.jpg" |
| img, hs, emb, imp, att = load_feats(path) |
| N = imp.shape[0] |
| keep_split = split_select(hs, emb, 64, GRID, REGION).numpy() |
| keep_attn = attention_select(att, 64).numpy() |
|
|
| def mask_grid(keep): |
| m = np.zeros(N); m[keep] = 1.0 |
| return m.reshape(GRID) |
|
|
| fig, ax = plt.subplots(1, 5, figsize=(18, 4.2)) |
| ax[0].imshow(img.resize((336, 336))); ax[0].set_title("Input (LLaVA 336x336)", fontsize=12) |
| im1 = ax[1].imshow(grid_img(imp), cmap="viridis"); ax[1].set_title("Temporal-shift importance\n(attention-free, centered)", fontsize=12) |
| im2 = ax[2].imshow(grid_img(att), cmap="magma"); ax[2].set_title("CLS attention\n(position bias -> lower rows)", fontsize=12) |
| ax[3].imshow(mask_grid(keep_split), cmap="Greens"); ax[3].set_title("SPLIT keeps @64\n(all 16 regions covered)", fontsize=12) |
| ax[4].imshow(mask_grid(keep_attn), cmap="Oranges"); ax[4].set_title("Attention-topk keeps @64\n(regions emptied)", fontsize=12) |
| for a in ax: a.set_xticks([]); a.set_yticks([]) |
| plt.tight_layout() |
| plt.savefig("outputs/fig_mechanism_heatmaps.png", dpi=130, bbox_inches="tight") |
| print("wrote outputs/fig_mechanism_heatmaps.png") |
|
|
| |
| with open("outputs/mechanism_validation.json") as f: |
| mv = json.load(f) |
| imgs = list(mv["images"].values()) |
| imp_com = np.mean([x["imp_com_row"] for x in imgs]) |
| att_com = np.mean([x["att_com_row"] for x in imgs]) |
| imp_g = np.mean([x["imp_gini"] for x in imgs]) |
| att_g = np.mean([x["att_gini"] for x in imgs]) |
| spear = np.mean([x["imp_att_spearman"] for x in imgs]) |
|
|
| fig, ax = plt.subplots(1, 3, figsize=(13, 3.6)) |
| ax[0].bar(["temporal\nshift", "CLS\nattention"], [imp_com, att_com], color=["#2D5F8B", "#C1666B"]) |
| ax[0].axhline(11.5, ls="--", c="gray", lw=1); ax[0].text(1.05, 11.5, "grid center", fontsize=8, va="bottom") |
| ax[0].set_title("Center-of-mass row\n(11.5 = unbiased)"); ax[0].set_ylim(10, 14) |
| ax[1].bar(["temporal\nshift", "CLS\nattention"], [imp_g, att_g], color=["#2D5F8B", "#C1666B"]) |
| ax[1].set_title("Gini (concentration)\nlower = more spread") |
| ax[2].bar(["Spearman(imp, attn)"], [spear], color=["#6B4E8B"]) |
| ax[2].axhline(0, c="k", lw=0.8); ax[2].set_ylim(-1, 1) |
| ax[2].set_title("Temporal-shift vs attention\n(anti-correlated => different signal)") |
| plt.tight_layout() |
| plt.savefig("outputs/fig_mechanism_bars.png", dpi=130, bbox_inches="tight") |
| print("wrote outputs/fig_mechanism_bars.png") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|