• 版块
  • 最新
  • 标签
  • 热门
  • 用户
  • 群组
  • 注册
  • 登录

Klang(金浪)AI量化论坛

  • 注册
  • 登录
  • 版块
  • 最新
  • 标签
  • 热门
  • 用户
  • 群组

pytorch 特征图绘制 gradcam

AI研究
1
1
172
正在加载更多帖子
  • 从旧到新
  • 从新到旧
  • 最多赞同
回复
  • 在新帖中回复
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • A
    asmcos 管理员 最后由 编辑 2022年4月25日 04:29

    b037a09a-464d-4823-9a10-e5387503c9c4-image.png

    # coding: utf-8
    import cv2
    import numpy as np
    import torch
    
    # 类的作用
    # 1.编写梯度获取hook
    # 2.网络层上注册hook
    # 3.运行网络forward backward
    # 4.根据梯度和特征输出热力图
    
    class ShowGradCam:
        def __init__(self,conv_layer):
            assert isinstance(conv_layer,torch.nn.Module), "input layer should be torch.nn.Module"
            self.conv_layer = conv_layer
            self.conv_layer.register_forward_hook(self.farward_hook)
            self.conv_layer.register_backward_hook(self.backward_hook)
            self.grad_res = []
            self.feature_res = []
    
        def backward_hook(self, module, grad_in, grad_out):
            self.grad_res.append(grad_out[0].detach())
    
        def farward_hook(self,module, input, output):
            self.feature_res.append(output)
    
        def gen_cam(self, 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, (32, 32))
            cam -= np.min(cam)
            cam /= np.max(cam)
            return cam
    
        def show_on_img(self,input_img):
            '''
            write heatmap on target img
            :param input_img: cv2:ndarray/img_pth
            :return: save jpg
            '''
            if isinstance(input_img,str):
                input_img = cv2.imread(input_img)
            img_size = (input_img.shape[1],input_img.shape[0])
            fmap = self.feature_res[0].cpu().data.numpy().squeeze()
            grads_val = self.grad_res[0].cpu().data.numpy().squeeze()
            cam = self.gen_cam(fmap, grads_val)
            cam = cv2.resize(cam, img_size)
            heatmap = cv2.applyColorMap(np.uint8(255 * cam), cv2.COLORMAP_JET)/255.
            cam = heatmap + np.float32(input_img/255.)
            cam = cam / np.max(cam)*255
            cv2.imwrite('grad_feature.jpg',cam)
            print('save gradcam result in grad_feature.jpg')
    
    
    1 条回复 最后回复 回复 引用 0
    1 / 1
    • First post
      1/1
      Last post