[自用代码]pytorch Grad-CAM查看不同模型的激活图

实现了使用grad-cam查看不同网络模型的激活图的功能

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from __future__ import print_function

import cv2
import os
import numpy as np
from PIL import Image
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as transforms
from models import *
import torch.backends.cudnn as cudnn


def img_transform(img_in, transform):
    """
    将img进行预处理,并转换成模型输入所需的形式—— B*C*H*W
    :param img_roi: np.array
    :return:
    """
    img = img_in.copy()
    img = Image.fromarray(np.uint8(img))
    img = transform(img)
    img = img.unsqueeze(0)    # C*H*W --> B*C*H*W
    return img


def img_preprocess(img_in):
    """
    读取图片,转为模型可读的形式
    :param img_in: ndarray, [H, W, C]
    :return: PIL.image
    """
    img = img_in.copy()
    img = cv2.resize(img,(256, 256))
    img = img[:, :, ::-1]   # BGR --> RGB
    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize([0.28201964, 0.2816544, 0.21802017],[0.25426927, 0.24146019, 0.1867752])
    ])

    img_input = img_transform(img, transform)
    return img_input


def backward_hook(module, grad_in, grad_out):
    grad_block.append(grad_out[0].detach())


def farward_hook(module, input, output):
    fmap_block.append(output)


def show_cam_on_image(img, mask,ClassName, out_dir,count):
    heatmap = cv2.applyColorMap(np.uint8(255*mask), cv2.COLORMAP_JET)
    heatmap = np.float32(heatmap) / 255
    cam = heatmap + np.float32(img)
    cam = cam / np.max(cam)
    CamName = ClassName + str(count) + "lapcam.jpg"
    RawName = ClassName + str(count) + "raw.jpg"
    path_cam_img = os.path.join(out_dir, CamName)
    path_raw_img = os.path.join(out_dir, RawName)
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)
    cv2.imwrite(path_cam_img, np.uint8(255 * cam))
    cv2.imwrite(path_raw_img, np.uint8(255 * img))


def comp_class_vec(ouput_vec, index=None):
    """
    计算类向量
    :param ouput_vec: tensor
    :param index: int,指定类别
    :return: tensor
    """
    if not index:
        index = np.argmax(ouput_vec.cpu().data.numpy())
    else:
        index = np.array(index)
    index = index[np.newaxis, np.newaxis]
    index = torch.from_numpy(index)
    one_hot = torch.zeros(1, 7).scatter_(1, index, 1)
    one_hot.requires_grad = True
    class_vec = torch.sum(one_hot * output.cpu())  # one_hot = 11.8605

    return class_vec


def gen_cam(feature_map, grads):
    """
    依据梯度和特征图,生成cam
    :param feature_map: np.array, in [C, H, W]
    :param grads: np.array, in [C, H, W]
    :return: np.array, [H, W]
    """
    cam = np.zeros(feature_map.shape[1:], dtype=np.float32)  # cam shape (H, W)

    weights = np.mean(grads, axis=(1, 2))  #

    for i, w in enumerate(weights):
        cam += w * feature_map[i, :, :]

    cam = np.maximum(cam, 0)
    cam = cv2.resize(cam, (256, 256))
    cam -= np.min(cam)
    cam /= np.max(cam)

    return cam

####----------------------------main----------------------------------------####
if __name__ == '__main__':

    
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    ####----------------------------setting the paths----------------------------####    
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    print(BASE_DIR)
    path_img = os.path.join(BASE_DIR, "test_img")
    output_dir = os.path.join(BASE_DIR, "Result", "backward_hook_cam")
    ####---------------------------import classes-------------------------------####
    classes = ('antimonite','arsenopyrite','blende','chalcopyrite','galena','native_gold','pyrite')

    ####--------------------------图片读取;网络加载----------------------------####
    fmap_block = list()
    grad_block = list()

    ####------------------------------Model-------------------------------------####

    # net = vgg16(pretrained=True,NumClass=len(classes))
    # net = resnext50_32x4d(pretrained = True, NumClass=len(classes))
    net = create_RepVGG_B1g2(deploy=False,pretrained = True, clas=len(classes))

    net_name = net.__class__.__name__
    net = net.to(device)
    if device == 'cuda':
        net = torch.nn.DataParallel(net)
        cudnn.benchmark = True
        net.eval()
        
    ####---------------------------Importing weight-----------------------------####
    print('==> Resuming from checkpoint..')
    assert os.path.isdir('checkpoint'), 'Error: no checkpoint directory found!'

    if net_name == 'ResNet':
        path = 'checkpoint\\resnext_acc99.88.pth'
    elif net_name == 'RepVGG':
        path = 'checkpoint\\repvgg_99.82.pth'
    elif net_name == 'VGG':
        path = 'checkpoint\\vgg_acc99.70.pth'
    else:
        print('error')

    checkpoint = torch.load((path), map_location = torch.device('cpu'))
    
    '''
    /home/deyiwang/191112/pytorch-cifar-master/checkpoint/NWPU45
    state = {
        'net': net.state_dict(),
        'acc': acc,
        'epoch': epoch,
    }    
    '''

    net.load_state_dict(checkpoint['net'])

    for root, dirs, files in os.walk(path_img, topdown = True):
        count = 0
        for name in files:
            # 注册hook
            if net_name == 'ResNet':
                net.module.layer4.register_forward_hook(farward_hook)
                net.module.layer4.register_backward_hook(backward_hook)
            elif net_name == 'RepVGG':
                net.module.stage4.register_forward_hook(farward_hook)
                net.module.stage4.register_backward_hook(backward_hook)
            elif net_name == 'VGG':
                net.module.features[40].register_forward_hook(farward_hook)
                net.module.features[40].register_backward_hook(backward_hook)
            else:
                print('error')
            print('======>>>>>>hook done<<<<<<======')
            im_dir = os.path.join(path_img, name)
            ####--------------------------图片读取;网络加载----------------------------####
            fmap_block = list()
            grad_block = list()
            
            img = cv2.imread(str(im_dir), 1)  # H*W*C
            img_input = img_preprocess(img)
            # forward
            output = net(img_input)
            idx = np.argmax(output.cpu().data.numpy())
            print("predict: {}".format(classes[idx]))
        
            # backward
            net.zero_grad()
            class_loss = comp_class_vec(output)
            class_loss.backward()
            print('======>>>>>>backward done<<<<<<======')
            # 生成cam
            grads_val = grad_block[0].cpu().data.numpy().squeeze()
            fmap = fmap_block[0].cpu().data.numpy().squeeze()
            cam = gen_cam(fmap, grads_val)
        
            # 保存cam图片
            ClassName = classes[idx]
            img_show = np.float32(cv2.resize(img, (256, 256))) / 255
            show_cam_on_image(img_show, cam, ClassName, output_dir,count)
            count += 1

在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
### 回答1: 我不太确定这个问题的具体内容,但是我可以简单介绍一下PyTorch Grad-CAM代表的含义和相关代码Grad-CAM是一种可视化技术,可以帮助我们理解模型的决策过程。在PyTorch中,Grad-CAM代码可以通过对预测结果进行反向传播,然后根据传播的梯度信息计算特定部位的重要性,从而得出像各部位对于预测结果的贡献程度。相关代码可以通过PyTorch库中的torch.nn.functional.interpolate函数和cv2库中的resize函数来实现。 ### 回答2: Grad-CAM是一种可视化卷积神经网络中的重要区域的方法,可以评估模型学习(分类)的可解释性。PyTorch是一种用于构建深度学习模型的开源框架,支持灵活的计算形构建、自动求导和动态形优化。在这篇回答中,我们将讨论如何使用PyTorch实现Grad-CAM。 首先需要安装必要的Python包,如PyTorch、NumPy和PIL。接下来,我们需要构建一个PyTorch模型,用于进行分类任务。再次提醒,模型需要以计算的形式定义。 现在,我们需要实现Grad-CAM。Grad-CAM的思想是在给定输入像和类别后,计算出特定类别对每个特征的重要性分数。这可以通过计算由类别分数得到的梯度并在特征上评估梯度的平均值来实现。以下是Grad-CAM的代码: ``` python def grad_cam(model, input, class_idx, layer_name): # get the features based on the input tensor features = model.features(input) # get the output of the classifier based on the features output = model.classifier(features) # zero the gradients model.zero_grad() # compute the gradient of the output category with respect to feature map output[:, class_idx].backward(retain_graph=True) # get the feature activations activations = model.features[layer_name].forward(input) # compute the importance map importance_map = torch.mean(torch.tensor(activations.grad[0]), axis=(1, 2)).detach().numpy() # apply RELU to the importance map importance_map = np.maximum(importance_map, 0) # resize the importance map to the input shape importance_map = cv2.resize(importance_map, input.shape[2:]) # normalize the importance map importance_map = (importance_map - np.min(importance_map)) / (np.max(importance_map) - np.min(importance_map)) return importance_map ``` 在代码中,我们首先提取给定输入的特征。接下来,我们计算由给定类别得到的梯度,并根据这些梯度计算特征的重要性分数。然后,我们使用ReLU激活并调整重要性分数的大小,使其与给定输入匹配。最后,我们返回标准化的重要性映射。 执行Grad-CAM后,我们需要将结果显示在输入像上。以下是一个简单的例子: ``` python input, label = dataset[0] class_idx = label.item() layer_name = 'conv5/relu' importance_map = grad_cam(model, input, class_idx, layer_name) img = input.numpy().transpose((1, 2, 0)) plt.imshow(img) plt.imshow(importance_map, alpha=0.5, cmap='jet') ``` 在代码段中,我们首先获取输入张量和目标类别。然后,我们指定一个带ReLU的层(即最后一个卷积层),并使用Grad-CAM计算重要性映射。最后,我们将输入张量可视化,并将重要性映射叠加在上面。 在这个例子中,我们使用一个简单的CNN进行像分类。使用类似的方法,我们可以对任何模型和任何像进行Grad-CAM计算。因此,通过使用PyTorch,我们可以方便地实现和理解Grad-CAM。 ### 回答3: PyTorch Grad-CAM是一种可视化技术,通过将卷积神经网络的特征与最终输出相结合,可以确定预测的重要区域。Grad-CAM代表梯度加权类激活,它利用梯度信息将网络层的重要性映射到输入像上,使得可以直观地理解卷积神经网络的决策。该技术使得我们可以以像素级别确定模型重点关注区域,以用于调试、可视化和解释该模型如何进行分类决策。 下面是一个使用PyTorch实现Grad-CAM的代码: ``` import torch import torch.nn as nn from torch.autograd import Variable from torchvision import models, transforms import cv2 import numpy as np import sys class CamExtractor(): """ Class for extracting activations and registering gradients from targetted intermediate layers """ def __init__(self, model, target_layers): self.model = model self.target_layers = target_layers self.gradients = [] def save_gradient(self, grad): self.gradients.append(grad) def forward_pass(self, x): """ Does a forward pass on convolutions, hooks the activations and gradients """ conv_output = None for name, module in self.model.named_modules(): x = module(x) if name in self.target_layers: x.register_hook(self.save_gradient) conv_output = x return conv_output, x class GradCam(): def __init__(self, model, target_layers, use_cuda): self.model = model self.model.eval() self.cuda = use_cuda if self.cuda: self.model = model.cuda() self.extractor = CamExtractor(self.model, target_layers) def forward(self, input): return self.model(input) def __call__(self, input, index=None): """Generates class activation map for the input image""" if self.cuda: features, output = self.extractor.forward_pass(input.cuda()) else: features, output = self.extractor.forward_pass(input) if index == None: index = np.argmax(output.cpu().data.numpy()) one_hot = np.zeros((output.size()[-1]), dtype=np.float32) one_hot[index] = 1 one_hot = Variable(torch.from_numpy(one_hot), requires_grad=True) if self.cuda: one_hot = one_hot.cuda() one_hot = torch.sum(one_hot * output) self.model.zero_grad() one_hot.backward() self.weights = self.extractor.gradients[-1].mean(dim=(-1, -2), keepdim=True) cam = torch.sum(self.weights * features, dim=1).squeeze() cam_relu = np.maximum(cam.cpu().data.numpy(), 0) cam_relu = cam_relu / np.max(cam_relu) return cam_relu if __name__ == '__main__': # define the model model = models.resnet50(pretrained=True) grad_cam = GradCam(model=model, target_layers=['layer4'], use_cuda=True) # load and preprocess an input image img = cv2.imread('input.jpg') img = cv2.resize(img, (224, 224)) img = np.float32(img) / 255 input = transforms.ToTensor()(img).unsqueeze(0) # use the grad cam class to generate the heat map cam = grad_cam(input) # use OpenCV to apply the heat map to the input image heatmap = cv2.applyColorMap(np.uint8(255 * cam), cv2.COLORMAP_JET) heatmap = np.float32(heatmap) / 255 cam = heatmap + np.float32(img) cam = cam / np.max(cam) # save the output cv2.imwrite("cam.jpg", np.uint8(255 * cam)) ``` 在该代码中,我们使用了PyTorch模型和变换,其中包括了ResNet-50模型。我们在GradCam类中定义了一个前向函数,将输入片传递给该函数,该函数返回模型输出。然后我们通过计算模型中所有运行层的输出特征,直到我们找到了我们感兴趣的“target layer”,并将它们注册到我们的“CamExtractor”类中。接下来,我们定义了一个“__call__”函数来执行Grad-CAM算法。它首先执行前向传递和后向传递,并计算权重。权重是特征的梯度取平均值。接下来,我们将权重分别乘以特征并在通道维进行求和,这返回一个二维的热力。最后,我们使用OpenCV应用热进行可视化。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

deyiwang89

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值