3D grad-cam
实现和解释计算机视觉中的3D Grad-CAM
背景介绍
Class Activation Mapping (CAM),Grad-CAM及其改进版本Grad-CAM++是在卷积神经网络(CNNs)中用于可视化特定类别的特征图的方法[^2]。这些方法帮助理解模型决策过程,通过高亮输入图像区域来显示哪些部分对于预测类别最重要。
当涉及到三维数据时,如医学影像或者视频序列分析,则需要扩展到3D空间。因此出现了针对这类应用而设计的3D Grad-CAM算法。
3D Grad-CAM的工作原理
为了适应体积数据集的特点,在原有二维基础上增加了第三个维度z轴方向上的梯度加权操作。具体来说:
对于给定的一个3D CNN最后一层feature map ( F_k \in R^{W\times H\times D} ),其中( W,H,D )分别代表宽度、高度以及深度尺寸;
计算目标分类得分相对于该层各位置响应值的偏导数得到全局平均池化后的权重向量( a_k^n=\frac{\partial y^n}{\partial A_k});
将上述计算所得的一维数组重新映射回原始大小的空间分布形式并取绝对值得到最终热力图表示:
[ L_{camm}^n=ReLU(\sum_k a_k^n *F_k)]
此表达式同样适用于处理具有时间连续性的四维张量结构(即包含T帧的时间序列)[^1].
以下是Python代码片段展示如何基于Pytorch框架实现简单的3D Grad-CAM功能:
import torch
from torchvision import models, transforms
import numpy as np
class GuidedBackprop():
"""
Implementation of Guided Back Propagation.
"""
def __init__(self, model):
self.model = model
self.image_reconstruction = None
self.activation_maps = []
self.gradient_maps = []
# Register hook to get gradients from last conv layer
target_layer = list(model.children())[-2][-1]
target_layer.register_forward_hook(self.forward_hook)
target_layer.register_backward_hook(self.backward_hook)
def forward_hook(module, input, output):
activation = output.detach().cpu()
global feature_map
feature_map = activation.numpy()[0]
def backward_hook(module, grad_input, grad_output):
global guided_gradients
guided_gradients = grad_output[0].detach().cpu().numpy()[0]
def generate_3d_grad_cam(image_tensor, class_index=None):
gb_model = GuidedBackprop(models.vgg16(pretrained=True))
prediction = gb_model(image_tensor.unsqueeze_(dim=0).cuda())
_, predicted_class = torch.max(prediction.data.cpu(), 1)
one_hot_vector = torch.zeros((1, prediction.size()[-1]))
if not isinstance(class_index,int):
class_index=predicted_class.item()
one_hot_vector[:, class_index]=1
gb_model.zero_grad()
prediction.backward(one_hot_vector.cuda())
weights = np.mean(guided_gradients,axis=(1,2)) # Compute per-channel mean gradient value over spatial dimensions
cam=np.zeros(feature_map.shape[1:],dtype=np.float32)
for i,w in enumerate(weights):
cam+=w*feature_map[i,:,:]
cam = np.maximum(cam, 0) # Apply ReLU operation on CAM result
cam -= np.min(cam)
cam /= np.max(cam)-np.min(cam)+1e-8
return cam,prediction,class_index
请注意这段代码主要展示了指导反向传播(Guided Backpropagation)与传统2D Grad-CAM相结合的方式,并未完全按照3DCNN架构编写;实际项目开发过程中还需要根据具体的网络结构调整相应参数设置。
相关推荐


















