# 类激活映射

类激活映射（class activation map，CAM）是一种用于卷积神经网络的可视化技术，它在输入图像上生成热力图，突出显示对网络预测某一类别最具影响的区域。由于该图直接由网络自身的权重导出，分类网络无需任何边界框标注即可转化为目标定位器，这使 CAM 成为模型可解释性和弱监督定位的基础工具。

| 关键信息 | 内容 |
|---|---|
| 提出者 | B. Zhou、A. Khosla、A. Lapedriza、A. Oliva 和 A. Torralba，《学习用于判别性定位的深度特征》（Learning Deep Features for Discriminative Localization），CVPR 2016（arXiv:1512.04150，2015）<sup>[1](https://doi.org/10.48550/arxiv.1512.04150)</sup> |
| 核心机制 | 类别分数是全局平均后的末层卷积特征图的加权和，因此将 softmax 层的权重投影到未池化的特征图上即可得到热力图，无需额外训练 <sup>[1](https://doi.org/10.48550/arxiv.1512.04150)</sup> |
| 架构要求 | 特征图必须通过全局平均池化直接位于 softmax 之前；以多个全连接层结尾的网络（AlexNet、VGG-16、VGG-19）无法使用 <sup>[2](https://link.springer.com/article/10.1007/s11263-019-01228-7)</sup><sup> • </sup><sup>[3](https://www.mathworks.com/help/deeplearning/ug/investigate-network-predictions-using-class-activation-mapping.html)</sup> |
| 图分辨率 | 修改版 AlexNet 为 13×13，修改版 VGGnet 为 14×14，修改版 GoogLeNet 为 7×7；通过插值上采样后叠加显示 <sup>[1](https://doi.org/10.48550/arxiv.1512.04150)</sup> |
| 主要成果 | GoogLeNet-GAP 在弱监督设置下于 ILSVRC 2014 上达到 37.1% 的 top-5 定位误差，接近全监督 AlexNet 的 34.2% <sup>[1](https://doi.org/10.48550/arxiv.1512.04150)</sup> |
| 最知名的变体 | Grad-CAM，CAM 的严格推广，使用梯度而非分类层权重，且无需改变架构 <sup>[2](https://link.springer.com/article/10.1007/s11263-019-01228-7)</sup> |

## 工作原理

该技术利用了以全局平均池化（GAP）结尾的网络结构。在 GAP 下，末层卷积层的每个特征图 \( F_{k} \) 通过对所有 \( Z \) 个空间位置求平均而缩减为一个数，类别 \( c \) 的类逻辑值是这些平均值的加权和：

\[ z_{c} = b_{c} + \sum_{k} w_{ck} \cdot F_{k}, \qquad F_{k} = \frac{1}{Z} \sum_{x,y} f_{k}(x,y) \]

由于相同的权重 \( w_{ck} \) 将池化后的特征图连接到 softmax，将它们投影回未池化的特征图即定义了类激活映射：

\[ M_{c}(x,y) = \sum_{k} w_{ck}\, f_{k}(x,y) \]

于是 \( z_{c} = b_{c} + \frac{1}{Z} \sum_{x,y} M_{c}(x,y) \)。因此，图中每个像素直接表示该空间网格位置上的激活对于将图像分类为类别 \( c \) 的重要性。<sup>[1](https://doi.org/10.48550/arxiv.1512.04150)</sup> softmax 偏置设为 0，因为它对图的影响很小。<sup>[1](https://doi.org/10.48550/arxiv.1512.04150)</sup> 实践中，所使用的权重是预测类别对应的最后一个全连接层的权重，作用于末层卷积层之后的 ReLU 激活。<sup>[3](https://www.mathworks.com/help/deeplearning/ug/investigate-network-predictions-using-class-activation-mapping.html)</sup> 获取该图无需额外训练。<sup>[1](https://doi.org/10.48550/arxiv.1512.04150)</sup>

## 实现步骤

从训练好的网络生成 CAM 需要四个步骤：

1. **使用以 GAP 结尾的网络。** 末层卷积特征图必须馈入全局平均池化层，随后是产生类逻辑值的[线性分类器](https://www.edgepedia.org/zh/linear-classifier)，然后是 softmax，中间不得有非线性隐藏全连接层。ResNet、DenseNet、SqueezeNet 和 [Inception](https://www.edgepedia.org/zh/inception-network) 等现代架构已具备此结构，因此无需修改网络即可生成热力图。<sup>[4](https://github.com/zhoubolei/CAM/blob/master/README.md)</sup>
2. **提取激活和权重。** 取末层卷积激活图 \( A_{k}(x,y) \) 和全连接层对目标类别的权重 \( w_{k}^{(c)} \)。<sup>[5](https://github.com/frgfm/torch-cam/blob/master/torchcam/methods/activation.py)</sup>
3. **计算加权和。** 图为 \( L^{(c)}_{\text{CAM}}(x,y) = \text{ReLU}\big(\sum_{k} w_{k}^{(c)} A_{k}(x,y)\big) \)。<sup>[5](https://github.com/frgfm/torch-cam/blob/master/torchcam/methods/activation.py)</sup>
4. **阈值化、上采样和叠加。** 对于弱监督边界框，原始工作将图中高于最大值 20% 的区域分割出来，并取覆盖最大连通分量的边界框。<sup>[1](https://doi.org/10.48550/arxiv.1512.04150)</sup> 该图处于末层卷积层的分辨率，修改版 AlexNet 为 13×13，修改版 VGGnet 为 14×14，修改版 [GoogLeNet](https://www.edgepedia.org/zh/googlenet) 为 7×7，通过插值上采样到图像尺寸。<sup>[1](https://doi.org/10.48550/arxiv.1512.04150)</sup>

## 起源

类激活映射由周博磊（Bolei Zhou）及其同事在《学习用于判别性定位的深度特征》（Learning Deep Features for Discriminative Localization）一文中提出，该文于 2015 年发布在 arXiv 上，并在 CVPR 2016 上发表。<sup>[1](https://doi.org/10.48550/arxiv.1512.04150)</sup> 该方法建立在早期基于梯度的可视化工作之上，即西莫尼扬（Simonyan）、韦达尔迪（Vedaldi）和齐瑟曼（Zisserman）于 2013 年在 arXiv 上提出的图像特定类[显著性图](https://www.edgepedia.org/zh/saliency-map)。<sup>[6](https://doi.org/10.48550/arxiv.1312.6034)</sup> 作者发布了官方 Caffe 预训练模型（ImageNet 上的 GoogLeNet-CAM 和 VGG16-CAM、Places205 上的 GoogLeNet-CAM 以及 AlexNet+-CAM）及 PyTorch 演示代码，并将该技术和模型开放供无限制使用。<sup>[4](https://github.com/zhoubolei/CAM/blob/master/README.md)</sup>

## 变体

CAM 的一般形式为 \( \text{CAM}_{c}(x) = \text{ReLU}\big(\sum_{k=1}^{N_{l}} \alpha_{k} \cdot A_{k}\big) \)，其中 \( A_{k} \) 是第 \( k \) 个激活通道，\( \alpha_{k} \) 是其对目标类别的重要性；各变体的区别在于 \( \alpha_{k} \) 的获取方式。<sup>[7](https://openaccess.thecvf.com/content/CVPR2021W/RCV/papers/Poppi_Revisiting_the_Evaluation_of_Class_Activation_Mapping_for_Explainability_A_CVPRW_2021_paper.pdf)</sup>

**Grad-CAM** 由塞尔瓦拉朱（Selvaraju）及其同事提出，用类别分数的全局平均池化梯度替代分类层权重，\( L^{c}_{\text{Grad-CAM}} = \text{ReLU}(\sum_{k} \alpha_{k}^{c} \cdot A^{k}) \)，无需改变架构或重新训练。<sup>[2](https://link.springer.com/article/10.1007/s11263-019-01228-7)</sup><sup> • </sup><sup>[8](https://doi.org/10.48550/arxiv.1610.02391)</sup> 它是 CAM 的严格推广：CAM 权重 \( w_{k}^{c} \) 与 \( \alpha_{k}^{c} \) 仅相差一个归一化常数因子，因此对于全卷积架构，CAM 是 Grad-CAM 的特例。<sup>[2](https://link.springer.com/article/10.1007/s11263-019-01228-7)</sup>

**Grad-CAM++** 由查托帕迪亚伊（Chattopadhyay）、萨卡尔（Sarkar）、豪拉德尔（Howlader）和巴拉苏布拉马尼亚安（Balasubramanian）提出，使用类别分数对末层卷积特征图激活的正偏导数的加权组合，改善了对多个目标实例的定位和全目标覆盖。<sup>[9](https://doi.org/10.48550/arxiv.1710.11063)</sup>

**无梯度变体。** Score-CAM 由王（Wang）及其同事提出，用基于分数的权重替代全连接权重，其计算方式是用每个归一化、上采样后的激活对输入进行掩码，并测量相对于基线的类别分数增量 <sup>[5](https://github.com/frgfm/torch-cam/blob/master/torchcam/methods/activation.py)</sup><sup> • </sup><sup>[10](https://doi.org/10.48550/arxiv.1910.01279)</sup>；SS-CAM 在高斯噪声样本上对这些权重进行平滑，IS-CAM 使用积分分数权重。<sup>[5](https://github.com/frgfm/torch-cam/blob/master/torchcam/methods/activation.py)</sup>

**面向忠实性的变体。** HiResCAM 由德雷洛斯（Draelos）和卡林（Carin）提出，通过使用梯度和激活张量的 Hadamard 积来增强 Grad-CAM，在 CT 肺部异常定位等医学领域表现优于 Grad-CAM，后者在这些场景中聚焦于无关的解剖区域。<sup>[11](https://www.thieme-connect.com/products/ejournals/pdf/10.1055/a-2562-2163.pdf)</sup><sup> • </sup><sup>[12](https://doi.org/10.48550/arxiv.2011.08891)</sup> F-CAM 由贝尔哈尔比（Belharbi）及其同事提出，通过引导参数化上采样实现全分辨率图。<sup>[13](https://doi.org/10.48550/arxiv.2109.07069)</sup> 一篇以方法为中心的综述还收录了 LayerCAM、Relevance-CAM 和 LIFT-CAM 等众多方法，并发现该领域正转向比较性、多层、概率性和基础模型感知的解释。<sup>[14](https://arxiv.org/pdf/2608.12299.pdf)</sup>

## 应用

**弱监督目标定位**是原始应用：使用边界框启发式的 GoogLeNet-GAP 在 ILSVRC 2014 测试集上达到 37.1% 的 top-5 误差，接近全监督 AlexNet 的 34.2%，且完全无需任何边界框标注。<sup>[1](https://doi.org/10.48550/arxiv.1512.04150)</sup> 后来的改进方法建立在相同思路上；例如 GRCAM 使用分类损失和回归函数的梯度来挖掘完整的目标区域，在 ILSVRC 和 CUB-200-2011 上进行了评估。<sup>[15](https://www.sciencedirect.com/science/article/abs/pii/S0031320322001455)</sup>

**医学影像**是一个主要领域。在胸腰椎 X 射线骨折检测任务中，使用 ROAD 指标，HiResCAM 被选为表现最佳的 CAM 变体，优于 Grad-CAM、Grad-CAM++、XGrad-CAM 和 LayerCAM。<sup>[11](https://www.thieme-connect.com/products/ejournals/pdf/10.1055/a-2562-2163.pdf)</sup> 在有边界框真值的 RSNA 气胸数据集上，DiffCAM 在七种比较方法中取得了最高的 AUPRC，GradCAM 被描述为在医学应用中非常稳健。<sup>[16](https://openaccess.thecvf.com/content/CVPR2025/papers/Li_DiffCAM_Data-Driven_Saliency_Maps_by_Capturing_Feature_Differences_CVPR_2025_paper.pdf)</sup> CAM 也用于模型调试，揭示哪些图像区域驱动了预测。<sup>[4](https://github.com/zhoubolei/CAM/blob/master/README.md)</sup>

## 局限与替代方法

**架构约束。** CAM 要求特征图直接位于 softmax 层之前（卷积特征图，然后 GAP，然后 softmax），这将其限制在特定架构上，这些架构在某些任务上可能达到较低的精度。<sup>[2](https://link.springer.com/article/10.1007/s11263-019-01228-7)</sup> 以多个全连接层结尾的网络，如 AlexNet、VGG-16 和 VGG-19，无法使用它，而 SqueezeNet、GoogLeNet、ResNet-18 和 MobileNet-v2 可以使用；SqueezeNet 的图分辨率是其他网络的四倍。<sup>[3](https://www.mathworks.com/help/deeplearning/ug/investigate-network-predictions-using-class-activation-mapping.html)</sup> 在原始形式中，移除全连接层使 VGGnet 参数减少约 90%，但损失 1–2% 的分类精度，通过在 GAP 之前添加一个 3×3、步长 1、填充 1、1024 个单元的卷积层在很大程度上得到补偿。<sup>[1](https://doi.org/10.48550/arxiv.1512.04150)</sup> 用于 VGG-16 的 Grad-CAM 取得了比 CAM 更好的 top-1 定位误差，后者需要改变架构和重新训练，且 top-1 分类误差差 2.98%。<sup>[2](https://link.springer.com/article/10.1007/s11263-019-01228-7)</sup>

**分辨率和池化偏差。** CAM 是粗糙的，处于最终卷积特征图的分辨率（VGG 和 AlexNet 为 14×14），上采样通过插值完成。<sup>[1](https://doi.org/10.48550/arxiv.1512.04150)</sup><sup> • </sup><sup>[2](https://link.springer.com/article/10.1007/s11263-019-01228-7)</sup> GAP 损失鼓励网络识别目标的范围，而全局最大池化鼓励仅识别一个判别性部分；在相似分类性能下，GAP 在定位方面优于 GMP。<sup>[1](https://doi.org/10.48550/arxiv.1512.04150)</sup>

**失败模式。** 当图像包含同一类别的多个实例时，Grad-CAM 无法正确定位目标，Grad-CAM++ 解决了这一问题。<sup>[9](https://doi.org/10.48550/arxiv.1710.11063)</sup> 在细粒度识别中，CAM 会解释所有支持目标类别的特征，包括与相似类别共享的特征，从而失败；Finer-CAM 则将目标类别与相似参考类别进行比较，并解释类逻辑值的差异。<sup>[14](https://arxiv.org/pdf/2608.12299.pdf)</sup>

**忠实性与评估。** 高亮区域是否真正解释了一个预测已被定量测试，结果发人深省。在 PASCAL 2007 验证集上以遮挡作为忠实性参考，Guided Grad-CAM 的秩相关系数为 0.254 和 0.261，而 Guided Backpropagation、c-MWP 和 CAM 分别为 0.168、0.220 和 0.208。<sup>[2](https://link.springer.com/article/10.1007/s11263-019-01228-7)</sup> 一个平凡的 Fake-CAM 对照——它基本上不突出任何东西——在平均下降和平均增加指标上远优于 Grad-CAM、Grad-CAM++ 和 Score-CAM 等真实方法，表明基于置信度的指标可以被钻空子。<sup>[7](https://openaccess.thecvf.com/content/CVPR2021W/RCV/papers/Poppi_Revisiting_the_Evaluation_of_Class_Activation_Mapping_for_Explainability_A_CVPRW_2021_paper.pdf)</sup> 在弱监督定位中，一项基准测试发现最近五种 WSOL 方法相较于 CAM 基线没有重大改进，并且所报道的相对于 CAM 的最先进提升部分是虚假的，是由全监督下的超参数调优造成的。<sup>[17](https://ar5iv.labs.arxiv.org/html/2001.07437)</sup> 另一个忠实性陷阱是背景依赖：因果和去偏方法（CI-CAM、C-CAM、Debiased-CAM）表明，如果模型实际上依赖于背景上下文，那么图可能与目标重叠却不忠实。<sup>[14](https://arxiv.org/pdf/2608.12299.pdf)</sup>

**替代方法。** Grad-CAM 每张图像仅需一次前向和部分反向传播，使其通常比基于扰动的定位方法（如遮挡）高效一个数量级。<sup>[2](https://link.springer.com/article/10.1007/s11263-019-01228-7)</sup> LIFT-CAM 提供了建立在 DeepLIFT 上的类 SHAP 近似。<sup>[14](https://arxiv.org/pdf/2608.12299.pdf)</sup>

## References

1. [Zhou, Bolei and colleagues (2015). Learning Deep Features for Discriminative Localization. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1512.04150)
2. [Grad-CAM: Visual Explanations from Deep Networks via Gradient-Based Localization (IJCV journal version)](https://link.springer.com/article/10.1007/s11263-019-01228-7)
3. [Investigate Network Predictions Using Class Activation Mapping (MathWorks documentation)](https://www.mathworks.com/help/deeplearning/ug/investigate-network-predictions-using-class-activation-mapping.html)
4. [zhoubolei/CAM, official code repository](https://github.com/zhoubolei/CAM/blob/master/README.md)
5. [torch-cam activation.py (CAM, ScoreCAM, SS-CAM, IS-CAM implementations)](https://github.com/frgfm/torch-cam/blob/master/torchcam/methods/activation.py)
6. [Simonyan, Karen, Vedaldi, Andrea, Zisserman, Andrew (2013). Deep Inside Convolutional Networks: Visualising Image Classification Models and Saliency Maps. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1312.6034)
7. [Revisiting the Evaluation of Class Activation Mapping for Explainability: A Novel Metric and Experimental Analysis](https://openaccess.thecvf.com/content/CVPR2021W/RCV/papers/Poppi_Revisiting_the_Evaluation_of_Class_Activation_Mapping_for_Explainability_A_CVPRW_2021_paper.pdf)
8. [Selvaraju, Ramprasaath R. and colleagues (2016). Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1610.02391)
9. [Chattopadhyay, Aditya and colleagues (2017). Grad-CAM++: Improved Visual Explanations for Deep Convolutional Networks. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1710.11063)
10. [Wang, Haofan and colleagues (2019). Score-CAM: Score-Weighted Visual Explanations for Convolutional Neural Networks. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1910.01279)
11. [Alternative Strategies to Generate Class Activation Maps (fracture detection X-ray study)](https://www.thieme-connect.com/products/ejournals/pdf/10.1055/a-2562-2163.pdf)
12. [Draelos, Rachel Lea, Carin, Lawrence (2020). Use HiResCAM instead of Grad-CAM for faithful explanations of convolutional neural networks. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2011.08891)
13. [Belharbi, Soufiane and colleagues (2021). F-CAM: Full Resolution Class Activation Maps via Guided Parametric Upscaling. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2109.07069)
14. [Class Activation Mapping in Explainable Computer Vision: A Method-Centered Review of CNN, Transformer, and Foundation-Model-Era Visual Explanations](https://arxiv.org/pdf/2608.12299.pdf)
15. [Gradient-based refined class activation map for weakly supervised object localization (Pattern Recognition)](https://www.sciencedirect.com/science/article/abs/pii/S0031320322001455)
16. [DiffCAM: Data-Driven Saliency Maps by Capturing Feature Differences (CVPR 2025)](https://openaccess.thecvf.com/content/CVPR2025/papers/Li_DiffCAM_Data-Driven_Saliency_Maps_by_Capturing_Feature_Differences_CVPR_2025_paper.pdf)
17. [Evaluating Weakly Supervised Object Localization Methods Right](https://ar5iv.labs.arxiv.org/html/2001.07437)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Neural networks and deep learning › Titles A to H

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/class-activation-mapping

English original: https://www.edgepedia.org/class-activation-mapping
