python grad_Grad-CAM的Pytorch实现

Grad-CAM implementation in Pytorch

What makes the network think the image label is 'pug, pug-dog' and 'tabby, tabby cat':

Combining Grad-CAM with Guided Backpropagation for the 'pug, pug-dog' class:

Gradient class activation maps are a visualization technique for deep learning networks.

The paper authors torch implementation: https://github.com/ramprs/grad-cam

This uses VGG19 from torchvision. It will be downloaded when used for the first time.

The code can be modified to work with any model. However the VGG models in torchvision have features/classifier methods for the convolutional part of the network, and the fully connected part. This code assumes that the model passed supports these two methods.

Usage: python gradcam.py --image-path

To use with CUDA: python gradcam.py --image-path --use-cuda

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1D-Grad-CAM是一种基于梯度的可视化方法,用于理解深度学习模型在输入序列中的关注点。下面是使用Pytorch实现1D-Grad-CAM的步骤: 1. 首先,加载训练好的模型和输入序列。可以使用torchvision.models中的预训练模型,例如resnet18。 2. 然后,定义一个Grad-CAM类,该类包含一个前向传递函数和一个反向传递函数。前向传递函数计算模型输出和特定层的特征图,反向传递函数计算特征图相对于输出的梯度。 3. 接下来,使用Grad-CAM类计算输入序列的梯度。这可以通过将输入序列传递给前向传递函数,然后将输出和特定层的特征图传递给反向传递函数来完成。 4. 最后,将梯度与特征图相乘,并将结果求和。这将生成一个热力图,用于可视化模型在输入序列中的关注点。 下面是一个使用Pytorch实现1D-Grad-CAM的示例代码: ```python import torch import torch.nn as nn import torch.nn.functional as F from torchvision import models class GradCAM: def __init__(self, model, target_layer): self.model = model self.target_layer = target_layer self.feature_maps = None self.gradient = None def save_feature_maps(self, module, input, output): self.feature_maps = output.detach() def save_gradient(self, grad): self.gradient = grad.detach() def forward(self, x): for name, module in self.model.named_modules(): if name == self.target_layer: module.register_forward_hook(self.save_feature_maps) module.register_backward_hook(self.save_gradient) break output = self.model(x) output = F.softmax(output, dim=1) return output def backward(self): self.gradient = torch.mean(self.gradient, dim=[2, 3], keepdim=True) feature_maps_weights = torch.mean(self.gradient * self.feature_maps, dim=1, keepdim=True) cam = F.relu(torch.sum(feature_maps_weights * self.feature_maps, dim=1, keepdim=True)) cam = F.interpolate(cam, size=x.shape[-1], mode='linear', align_corners=False) cam = cam.squeeze() cam = cam - torch.min(cam) cam = cam / torch.max(cam) return cam # 加载模型和输入序列 model = models.resnet18(pretrained=True) x = torch.randn(1, 3, 224, 224) # 创建Grad-CAM对象并计算热力图 grad_cam = GradCAM(model, 'layer4') output = grad_cam.forward(x) output[:, 0].backward() cam = grad_cam.backward() # 可视化热力图 import matplotlib.pyplot as plt plt.imshow(cam.detach().numpy()) plt.show() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值