CNN 시각화 기법

핵심 요약: CNN의 “어느 부분을 보고 예측했는가”를 시각화하는 대표 기법으로 CAM, Grad-CAM, Grad-CAM++, Score-CAM, XGrad-CAM, Eigen-CAM, Layer-CAM 등이 있다. 이들은 모두 최종 합성곱 층의 특징 맵(feature map)을 기반으로 가중치를 부여해 히트맵을 생성하며, 각 기법은 가중치 산출 방식과 적용 범위에서 차이를 보인다.

1. Class Activation Map (CAM)

  • 개요: CNN의 마지막 합성곱 층 뒤에 Global Average Pooling(GAP)을 추가하고, GAP 출력과 FC층의 가중치를 곱하여 클래스별 히트맵을 구함[1].
  • 수식:
    $$
    L_{CAM}^c(i,j) = \sum_k w_k^c\,f_k(i,j)
    $$
    여기서 $$f_k(i,j)$$는 $$k$$번째 특징 맵의 $$(i,j)$$ 위치 값, $$w_k^c$$는 클래스 $$c$$로 가는 FC층 가중치[1].
  • 장점/단점
  • 간단하며 클래스별 위치 정보 제공
  • GAP+FC 구조로만 적용 가능, 기존 모델 수정 필요[1].

2. Gradient-weighted CAM (Grad-CAM)

  • 개요: 기존 CNN 구조 변경 없이, 최종 합성곱 층의 특징 맵에 대한 클래스 점수의 그래디언트를 이용해 가중치 산출[2].
  • 수식:
  1. $$ \alpha_k^c = \frac{1}{Z}\sum_{i,j} \frac{\partial y^c}{\partial f_k(i,j)} $$
  2. $$ L_{Grad-CAM}^c(i,j) = \mathrm{ReLU}\Bigl(\sum_k \alpha_k^c\,f_k(i,j)\Bigr) $$
  • 특징
  • 재학습 불필요, 다양한 CNN 구조에 적용 가능
  • coarse한 해상도, 양(positive) 영향 영역만 강조[2].

3. Grad-CAM++

  • 개요: Grad-CAM의 gradient 통합 방식을 개선, 다중 객체 및 복잡한 위치에서 더 정확한 히트맵 제공[3].
  • 수식:
    $$
    \alpha_k^c = \frac{\sum_{i,j}\frac{\partial^2 y^c}{(\partial f_k(i,j))^2}}{2\sum_{i,j}\frac{\partial^2 y^c}{(\partial f_k(i,j))^2} + \sum_{i,j} f_k(i,j)\,\frac{\partial^3 y^c}{(\partial f_k(i,j))^3}}
    $$
    이후 Grad-CAM과 유사하게 결합
  • 장점
  • 복수 객체 분리, 정밀한 localization 성능 향상[3].

4. Score-CAM

  • 개요: Gradient 의존성을 제거, 특징 맵을 마스크로 사용해 순전파(forward pass)한 클래스 점수로 가중치 산출[4].
  • 절차
  1. 특징 맵 $$f_k$$를 원본 이미지에 upsample 후 마스크로 적용
  2. 각 마스크 이미지에 대한 모델 출력 $$S_k^c$$를 가중치로 사용
  3. $$ L_{Score-CAM}^c = \sum_k \frac{S_k^c}{\sum_{k’}S_{k’}^c}\,f_k $$
  • 특징
  • gradient noise 제거, 안정적인 시각화
  • 계산 비용 증가[4].

5. XGrad-CAM

  • 개요: Grad-CAM의 가중치 산출에 두 가지 공리(Conservation, Sensitivity)를 적용해 Axiom-based 방식으로 개선[5].
  • 특징
  • 이론적 정합성 확보
  • Grad-CAM, Grad-CAM++ 대비 class-discriminative 성능 우수[5].

6. Eigen-CAM

  • 개요: 특징 맵 행렬의 주성분(principal component)을 이용해 히트맵 생성, gradient 없이 plug-and-play 가능[6].
  • 절차
  1. 최종 합성곱 층의 모든 $$K$$개 특징 맵을 $$K$$×$$M$$ 행렬(각 맵 펼침)으로 재구성
  2. PCA로 첫 번째 고유 벡터 산출
  3. 해당 성분을 히트맵으로 reshaping
  • 특징
  • 간단하고 모델 수정 불필요
  • 클래스 구별성은 미약[6].

7. Layer-CAM

  • 개요: 여러 레이어의 집중 영역을 계층적으로 결합, 중간 및 최종 특징 맵의 정보 활용[7].
  • 절차
  1. 각 레이어에서 positive gradient와 특징 맵의 element-wise 곱으로 가중치 행렬 생성
  2. 레이어별 히트맵을 업샘플링 후 가중 평균 또는 누적
  • 장점
  • 저해상도 최종 맵 한계를 극복, 세밀한 localization 제공[7].

8. 그 외 주요 변형

  • Ablation-CAM: 특징 맵 일부를 제거(ablated)했을 때 점수 감소량으로 가중치 산출
  • EigenGrad-CAM: Eigen-CAM 개념에 gradient 곱합 적용

비교 요약 테이블

기법가중치 산출Gradient 의존모델 수정주요 특징
CAM[1]FC 가중치×필요간단, GAP+FC 전용
Grad-CAM[2]평균 그래디언트불필요범용, coarse
Grad-CAM++[3]고차 미분불필요정밀 localization
Score-CAM[4]forward score×불필요노이즈 감소
XGrad-CAM[5]axiom-based gradient불필요이론적 정합성
Eigen-CAM[6]PCA×불필요간단, 비차별적
Layer-CAM[7]grad×activation불필요다층 통합

참고 문헌
[1] CAM 설명(tyami’s study blog)
[4] Score-CAM: arXiv:1910.01279
[3] Grad-CAM++ 논문 요약
[7] Layer-CAM: IEEE TIP 2021
[5] XGrad-CAM: arXiv:2008.02312
[6] Eigen-CAM: arXiv:2008.00299
[2] Grad-CAM: arXiv:1610.02391

출처
[1] CNN visualization: CAM and Grad-CAM 설명 – tyami’s study blog https://tyami.github.io/deep%20learning/CNN-visualization-Grad-CAM/
[2] Grad-CAM: Visual Explanations from Deep Networks via Gradient … https://arxiv.org/abs/1610.02391
[3] Grad-CAM++: Generalized Gradient-Based Visual Explanations for Deep Convolutional Networks https://raiithold.iith.ac.in/4757/
[4] Score-CAM: Score-Weighted Visual Explanations for Convolutional … https://arxiv.org/abs/1910.01279
[5] Axiom-based Grad-CAM: Towards Accurate Visualization … – arXiv https://arxiv.org/abs/2008.02312
[6] Eigen-CAM: Class Activation Map using Principal Components – arXiv https://arxiv.org/abs/2008.00299
[7] LayerCAM: Exploring Hierarchical Class Activation Maps … – SciSpace https://scispace.com/papers/layercam-exploring-hierarchical-class-activation-maps-for-einima0f0r
[8] Grad-CAM Reveals the Why Behind Deep Learning Decisions https://kr.mathworks.com/help/deeplearning/ug/gradcam-explains-why.html
[9] 2) CAM, Grad CAM – 한땀한땀 딥러닝 컴퓨터 비전 백과사전 – 위키독스 https://wikidocs.net/135874
[10] Grad-CAM: Visual https://users.cs.fiu.edu/~sjha/class2024/2016GradCAM.pdf
[11] Official implementation of Score-CAM in PyTorch – GitHub https://github.com/haofanwang/Score-CAM
[12] adityac94/Grad_CAM_plus_plus: A generalized gradient … – GitHub https://github.com/adityac94/Grad_CAM_plus_plus
[13] JOURNAL OF LATEX CLASS FILES, VOL. 14, NO. 8, JUNE 2024 http://arxiv.org/pdf/2406.02605.pdf
[14] XGrad-CAM implementation in Pytorch – GitHub https://github.com/Fu0511/XGrad-CAM
[15] Understanding Your YOLOv8 model with Eigen-CAM | Datature Blog https://datature.com/blog/understanding-your-yolov8-model-with-eigen-cam
[16] XGrad-CAM Explained – Papers With Code https://paperswithcode.com/method/xgrad-cam
[17] EigenCAM for YOLO5 — Advanced AI explainability with pytorch … https://jacobgil.github.io/pytorch-gradcam-book/EigenCAM%20for%20YOLO5.html
[18] Eigen-CAM: Class Activation Map Using Principal Components – iMTE https://wewinserv.tistory.com/168
[19] jacobgil/pytorch-grad-cam: Advanced AI Explainability for … – GitHub https://github.com/jacobgil/pytorch-grad-cam
[20] 9. Grad-CAM: Visual Explanations from Deep Networks via Gradient … https://cumulu-s.tistory.com/40
[21] [X:AI] Grad-CAM 논문 리뷰 – hyeon827 – 티스토리 https://hyeon827.tistory.com/49
[22] [1610.02391] Grad-CAM: Visual Explanations from Deep Networks … https://ar5iv.labs.arxiv.org/html/1610.02391
[23] Grad-CAM: Visual Explanations from Deep Networks http://arxiv.org/pdf/1610.02391.pdf
[24] [PDF] arXiv:1610.02391v3 [cs.CV] 21 Mar 2017 https://mit6874.github.io/assets/misc/selvaraju.pdf
[25] Papers with Code – Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization https://paperswithcode.com/paper/grad-cam-visual-explanations-from-deep
[26] Grad-CAM: Why did you say that? Visual Explanations from Deep Networks via Gradient-based Localization http://arxiv.org/abs/1610.02391v1
[27] arXiv:2008.02312v3 [cs.CV] 15 Aug 2020 http://www.arxiv.org/pdf/2008.02312v3.pdf
[28] GitHub – rigvedrs/YOLO-V11-CAM https://github.com/rigvedrs/YOLO-V11-CAM
[29] Grad-CAM: Visual Explanations From Deep Networks via Gradient-Based Localization https://openaccess.thecvf.com/content_ICCV_2017/papers/Selvaraju_Grad-CAM_Visual_Explanations_ICCV_2017_paper.pdf

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다