EECS498 · Deep Learning Revisited · 05

Vision Models as Representation Builders

分类 accuracy 只告诉你答案对不对;A4 的 saliency、adversarial attack、class visualization 与 style transfer 在追问:内部 representation 到底保留了什么?

45–60 分钟 A4 Interpretation · synthesis

30-second recall

30 秒回忆

你对输入像素求 gradient、定向改变分类结果,并用深层 features 重建 content/style。

  1. 对输入求梯度与对参数求梯度有何不同?
  2. Gram matrix 丢掉什么、保留什么?
  3. 对抗样本揭示了哪种脆弱性?

Why Did We Need This?

一个 CNN 输出 “cat” 不代表我们知道它依赖了耳朵、纹理还是背景。把参数冻结、把输入当优化变量,就能用同一套 backprop 询问 representation。

image X [N,3,H,W]
features Fℓ [N,Cℓ,Hℓ,Wℓ]
score / feature loss scalar
input gradient ∂L/∂X same as X

What You Actually Implemented

File Implemented Saved artifact
network_visualization.py saliency, targeted attack, class visualization step saliency / adversarial / class-viz JPG
style_transfer.py content loss, Gram matrix, style loss, TV loss style transfer result JPG
Mental model

Representation 是模型为任务保留的坐标系;可视化方法通过固定模型、反向优化输入,探测这个坐标系对什么变化敏感。

Mechanism · 同一个 gradient,不同的提问

Your code · 2022

A4/style_transfer.py · gram_matrix()

View source →
features = features.view(-1, C, H * W)
gram = torch.matmul(features, features.transpose(1, 2))
if normalize:
    gram = gram / (H * W * C)

Why This Design?

直接匹配 pixels 会要求两图逐像素相同;匹配深层 feature 允许低层细节变化但保留内容。Gram 对空间维求和,保留“哪些 channels 经常一起激活”,因此更像纹理统计而非布局。

Historical implementation note:class visualization 的更新项把 l2_reg * img.data ** 2 直接加入梯度方向;严格的 L2 penalty 对 image 的梯度应与 img 成正比,而不是 img²。课程保留原实现并指出差异。

Common misconception:saliency 不是因果证明,attention heatmap 也不自动等于解释。它们显示某种局部敏感度或路由权重,取决于定义的目标与模型状态。

Then vs Now

今天对 LLM 做 activation patching、logit attribution、feature visualization,仍在追问同一个问题:某个 prediction 由哪些内部 representations 支撑?对象从 pixels 变成 tokens,方法论仍是干预与读出。

Concept Checks

F=[N,C,H,W],Gram matrix 的 shape?

[N,C,C]。空间维被展平并求内积,得到每对 channels 的相关统计。

为什么 adversarial update 能很小却改变类别?

高维输入中,模型 score 对许多像素的微小、同向变化可以累积,跨过离当前样本很近的 decision boundary。

若 Gram matrix 相同,两张图片布局必须相同吗?

不必。Gram 汇总空间位置,因而丢掉了 feature 出现在哪里的详细信息。

Depth Lab · 从 prediction 反问 representation

Classifier 给 class score,但 score 本身不解释 input 哪些 pixels 驱动它、hidden features 保留了什么。A4 中 saliency、adversarial examples、feature inversion 与 style transfer 是四种不同 probe。

若正确 class score s_y 对 image X[3,224,224] 求 gradient,得到同 shape ∂s_y/∂X。在 channel 轴取 absolute max 后得到 [224,224] saliency:每个 pixel 的局部敏感度,不是 segmentation probability。

  1. Saliency:固定 model,问微小 pixel change 怎样改变 score。
  2. Adversarial update:沿能提升目标 score 的 input gradient 移动。
  3. Feature inversion:优化 pixels,使某层 activation 接近 target activation。
  4. Style transfer:content loss 保留高层 arrangement;Gram statistics 匹配跨位置 feature correlations。

Feature matrix F[C,HW] 的 Gram matrix G=FFᵀ [C,C] 汇总 channel pairs 在所有 positions 的共激活,丢掉精确 spatial ordering。这正适合 texture/style,未必适合 content geometry。

若 style loss 直接逐位置匹配 F?

会强迫生成图在相同位置复现 target features,更接近 content reconstruction,不再只保留位置无关的 texture statistics。

Misconception diagnosis

高 saliency 不等于“模型的完整解释”;它是当前点的一阶局部 derivative,可能受 saturation、scale 与 downstream composition 影响。

Gram matrix 为什么是 [C,C] 而不是 [HW,HW]?

它比较 feature channels,并在 spatial positions 上求和。

Transfer Question

把同样的 input-gradient probe 用在 token embeddings 上,得到的是对 token ID 的 gradient 还是 embedding activation 的 gradient?为什么离散 ID 本身不能直接微调?

闭卷解释

  1. 区分 saliency 与 segmentation。
  2. 解释 feature inversion 在优化 input 还是 model。
  3. 为什么 Gram statistics 更像 style than layout?

Connection

分类与解释仍是一图一标签;下一课把 output 扩成许多 spatial candidates、boxes 与 classes。

Based on your implementation