C.W.K.
Stream
Lesson 06 of 06 · published

Grad-CAM — CNN이 보는 걸 보기

~11 min · grad-cam, interpretability, visualization

Level 0Level 0
0 XP0/78 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

Production에서 interpretability가 중요한 이유

"고양이"라고 99% 확신하는 CNN이 어느 픽셀을 봤는지 모르면 쓸모없어. Grad-CAM (Gradient-weighted Class Activation Mapping)은 특정 클래스 예측에 가장 영향 준 이미지 영역을 보여주는 heatmap 만들어.

기법: 선택한 클래스의 logit을 마지막 convolutional layer의 feature map에 대해 gradient 계산. 그 gradient를 spatial 차원으로 평균 → 채널별 중요도 weight. 각 feature map에 weight 곱해서 합산 → 결과는 2D heatmap, 원본 이미지 크기로 upsample.

이게 "model이 고양이에 과신함" 디버깅 방법 — 사실 고양이 아니라 모서리의 워터마크를 보고 있다는 걸 발견. 의료 영상 감사, 콘텐츠 검열 설명, dataset 감사에도 같은 기법.

Code

Grad-CAM implementation·python
import tensorflow as tf
import numpy as np

def grad_cam(model, image, class_idx, last_conv_layer_name):
    """Generate Grad-CAM heatmap for the given class."""
    # Sub-model that returns last conv output AND final predictions
    grad_model = tf.keras.Model(
        inputs=[model.inputs],
        outputs=[model.get_layer(last_conv_layer_name).output, model.output]
    )

    with tf.GradientTape() as tape:
        conv_outputs, predictions = grad_model(image)
        class_channel = predictions[:, class_idx]

    # Gradient of class score w.r.t. last conv output
    grads = tape.gradient(class_channel, conv_outputs)

    # Average gradients over spatial dims → channel importance
    pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))

    # Weight each feature map by its importance, sum to get heatmap
    conv_outputs = conv_outputs[0]
    heatmap = conv_outputs @ pooled_grads[..., tf.newaxis]
    heatmap = tf.squeeze(heatmap)
    heatmap = tf.maximum(heatmap, 0) / tf.reduce_max(heatmap)
    return heatmap.numpy()

# Usage
model = tf.keras.applications.EfficientNetV2S(weights='imagenet')
img = tf.keras.utils.load_img("cat.jpg", target_size=(224, 224))
img_array = tf.expand_dims(tf.keras.utils.img_to_array(img), 0)
img_array = tf.keras.applications.efficientnet_v2.preprocess_input(img_array)

preds = model.predict(img_array)
top_class = int(tf.argmax(preds[0]))
heatmap = grad_cam(model, img_array, top_class, "top_conv")

External links

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.