都跑通了,随取随用

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
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
from model_dense import densenet121 as createmodelclass GradCAM:def __init__(self, model: nn.Module, target_layer: str, size=(224, 224), num_cls=1000, mean=None, std=None) -> None:self.model = modelself.model.eval()# register hook# 可以自己指定层名,没必要一定通过target_layer传递参数# self.model.layer4self.model.features[-1].register_forward_hook(self.__forward_hook)self.model.features[-1].register_backward_hook(self.__backward_hook)# getattr(self.model, target_layer).register_forward_hook(self.__forward_hook)# getattr(self.model, target_layer).register_backward_hook(self.__backward_hook)self.size = sizeself.origin_size = Noneself.num_cls = num_clsself.mean, self.std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]if mean and std:self.mean, self.std = mean, stdself.grads = []self.fmaps = []def forward(self, img_arr: np.ndarray, label=None, show=True, write=False):img_input = self.__img_preprocess(img_arr.copy())# forwardoutput = self.model(img_input)idx = np.argmax(output.cpu().data.numpy())# backwardself.model.zero_grad()loss = self.__compute_loss(output, label)loss.backward()# generate CAMgrads_val = self.grads[0].cpu().data.numpy().squeeze()fmap = self.fmaps[0].cpu().data.numpy().squeeze()cam = self.__compute_cam(fmap, grads_val)# showcam_show = cv2.resize(cam, self.origin_size)img_show = img_arr.astype(np.float32) / 255self.__show_cam_on_image(img_show, cam_show, if_show=show, if_write=write)self.fmaps.clear()self.grads.clear()def __img_transform(self, img_arr: np.ndarray, transform: torchvision.transforms) -> torch.Tensor:img = img_arr.copy()  # [H, W, C]img = Image.fromarray(np.uint8(img))img = transform(img).unsqueeze(0)  # [N,C,H,W]return imgdef __img_preprocess(self, img_in: np.ndarray) -> torch.Tensor:self.origin_size = (img_in.shape[1], img_in.shape[0])  # [H, W, C]img = img_in.copy()img = cv2.resize(img, self.size)img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize(self.mean, self.std)])img_tensor = self.__img_transform(img, transform)return img_tensordef __backward_hook(self, module, grad_in, grad_out):self.grads.append(grad_out[0].detach())def __forward_hook(self, module, input, output):self.fmaps.append(output)def __compute_loss(self, logit, index=None):if not index:index = np.argmax(logit.cpu().data.numpy())else:index = np.array(index)index = index[np.newaxis, np.newaxis]index = torch.from_numpy(index)one_hot = torch.zeros(1, self.num_cls).scatter_(1, index, 1)one_hot.requires_grad = Trueloss = torch.sum(one_hot * logit)return lossdef __compute_cam(self, feature_map, grads):"""feature_map: np.array [C, H, W]grads: np.array, [C, H, W]return: np.array, [H, W]"""cam = np.zeros(feature_map.shape[1:], dtype=np.float32)alpha = np.mean(grads, axis=(1, 2))  # GAPfor k, ak in enumerate(alpha):cam += ak * feature_map[k]  # linear combinationcam = np.maximum(cam, 0)  # relucam = cv2.resize(cam, self.size)cam = (cam - np.min(cam)) / np.max(cam)return camdef __show_cam_on_image(self, img: np.ndarray, mask: np.ndarray, if_show=True, if_write=False):heatmap = cv2.applyColorMap(np.uint8(255 * mask), cv2.COLORMAP_JET)heatmap = np.float32(heatmap) / 255cam = heatmap + np.float32(img)cam = cam / np.max(cam)cam = np.uint8(255 * cam)if if_write:cv2.imwrite("camcam.jpg", cam)if if_show:# 要显示RGB的图片,如果是BGR的 热力图是反过来的plt.imshow(cam[:, :, ::-1])plt.show()# 调用函数
img = cv2.imread('./data/flower/roses/roses1.jpg', 1)
model = torchvision.models.densenet121(pretrained=True)
# model=createmodel(num_classes=2)
# model.load_state_dict(torch.load('./weights/densenet121.pth'))
grad_cam = GradCAM(model, 'features', (224, 224))
grad_cam.forward(img, show=True, write=False)

from pytorch_grad_cam import GradCAM, ScoreCAM, GradCAMPlusPlus, AblationCAM, XGradCAM, EigenCAM
from pytorch_grad_cam.utils.image import show_cam_on_image, deprocess_image, preprocess_image
import cv2
import numpy as np
import torchvision
import torch
from PIL import Image
import matplotlib.pyplot as plt
from model_dense import densenet121 as createmodel# import os
# # MacOS系统应该加这行
# os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
# 1.加载模型
# model=createmodel(num_classes=2)
# model.load_state_dict(torch.load('./weights/densenet121.pth'))
model = torchvision.models.resnet50(pretrained=False,num_classes=1000)
model.load_state_dict(torch.load('./premodel/pre-resnet50.pth'))
# 2.选择目标层
target_layer = [model.layer4[-1]]
print(target_layer)
#target_layer = [model.layer4[-1]]   #resnet50
'''
Resnet18 and 50: model.layer4[-1]
VGG and densenet161: model.features[-1]
mnasnet1_0: model.layers[-1]
ViT: model.blocks[-1].norm1
'''
# 3.输入图像
img_path = './data/flower/roses/roses1.jpg'
img = Image.open(img_path).convert('RGB')
#img = img.resize((224,224))
# 一转,'x' is a float32 Numpy array of shape
img = np.array(img)
img_float_np = np.float32(img)/255  #归一化
# define the torchvision image transforms
transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor(),
])input_tensor = transform(img)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
input_tensor = input_tensor.to(device)# 二扩,add a dimension to transform the array into a "batch"
input_tensor = input_tensor.unsqueeze(0)###### x.shape (1,3,447,670)# 4.初始化GradCAM,包括模型,目标层以及是否使用cuda
cam = GradCAM(model=model, target_layers=target_layer, use_cuda=False)
# 5.选定目标类别,如果不设置,则默认为分数最高的那一类
targets = None
# targets = [ClassifierOutputTarget(281)] 第281类
# 6. 计算cam
grayscale_cam = cam(input_tensor=input_tensor, targets=targets)
# 加上 aug_smooth=True(应用水平翻转组合,并通过[1.0,1.1,0.9]对图像进行多路复用,使CAM围绕对象居中)
# eigen_smooth=True(去除大量噪声)
# 7.展示热力图并保存, grayscale_cam是一个batch的结果,只能选择一张进行展示
grayscale_cam = grayscale_cam[0,:]
cam_image = show_cam_on_image(img_float_np, grayscale_cam, use_rgb=True)
plt.imshow(cam_image)
plt.show()
# cv2.imwrite(f'/content/African elephant.jpg', cam_image)
'''
Product Grad_Cam Heatmap
Paper https://arxiv.org/abs/1610.02391
Copyright (c) Xiangzi Dai, 2020
'''
import cv2
import numpy as np
import torch
from torch.autograd import Function
from torchvision import models
import sys
from model_dense import densenet121 as createmodeldef get_last_conv(m):"""Get the last conv layer in an Module."""convs = filter(lambda k: isinstance(k, torch.nn.Conv2d), m.modules())return list(convs)[-1]class Grad_Cam:def __init__(self, model, target_layer_names, use_cuda):self.model = modelself.target = target_layer_namesself.use_cuda = use_cudaself.grad_val = []self.feature = []  # feature dim is same as grad_valself.hook = []self.img = []self.inputs = Noneself._register_hook()def get_grad(self, module, input, output):self.grad_val.append(output[0].detach())def get_feature(self, module, input, output):self.feature.append(output.detach())def _register_hook(self):for i in self.target:self.hook.append(i.register_forward_hook(self.get_feature))self.hook.append(i.register_backward_hook(self.get_grad))def _normalize(self, cam):h, w, c = self.inputs.shapecam = (cam - np.min(cam)) / np.max(cam)cam = cv2.resize(cam, (w, h))heatmap = cv2.applyColorMap(np.uint8(255 * cam), cv2.COLORMAP_JET)heatmap = np.float32(heatmap) / 255cam = heatmap + np.float32(self.inputs)cam = cam / np.max(cam)return np.uint8(255 * cam)def remove_hook(self):for i in self.hook:i.remove()def _preprocess_image(self, img):means = [0.485, 0.456, 0.406]stds = [0.229, 0.224, 0.225]preprocessed_img = img.copy()[:, :, ::-1]for i in range(3):preprocessed_img[:, :, i] = preprocessed_img[:, :, i] - means[i]preprocessed_img[:, :, i] = preprocessed_img[:, :, i] / stds[i]preprocessed_img = \np.ascontiguousarray(np.transpose(preprocessed_img, (2, 0, 1)))preprocessed_img = torch.from_numpy(preprocessed_img)preprocessed_img.unsqueeze_(0)input = preprocessed_img.requires_grad_(True)return inputdef __call__(self, img, idx=None):""":param inputs: [w,h,c]:param idx: class id:return: grad_cam img list"""self.model.zero_grad()self.inputs = np.float32(cv2.resize(img, (224, 224))) / 255inputs = self._preprocess_image(self.inputs)if self.use_cuda:inputs = inputs.cuda()self.model = self.model.cuda()output = self.model(inputs)if idx is None:idx = np.argmax(output.detach().cpu().numpy())  # predict idtarget = output[0][idx]target.backward()# computerweights = []for i in self.grad_val[::-1]:  # i dim: [1,512,7,7]weights.append(np.mean(i.squeeze().cpu().numpy(), axis=(1, 2)))for index, j in enumerate(self.feature):  # j dim:[1,512,7,7]cam = (j.squeeze().cpu().numpy() * weights[index][:, np.newaxis, np.newaxis]).sum(axis=0)cam = np.maximum(cam, 0)  # reluself.img.append(self._normalize(cam))return self.imgif __name__ == '__main__':img_path = './data/flower/roses/roses1.jpg'model_path = './weights/densenet121.pth'use_cuda = torch.cuda.is_available()# load modelcheckpoint = torch.load(model_path)model=createmodel(num_classes=2)model.load_state_dict(checkpoint)# model=models.densenet121(pretrained=True)model.eval()# print(model.state_dict)img = cv2.imread(img_path, 1)m = get_last_conv(model)target_layer = [m]# target_layer=[model.features[-1]]print(target_layer)Grad_cams = Grad_Cam(model, target_layer, use_cuda)grad_cam_list = Grad_cams(img)# cv2.imshow(grad_cam_list[0])# target_layer corresponding grad_cam_listcv2.imwrite("out.jpg", grad_cam_list[0])

grad_cam可视化程序搬运相关推荐

  1. 用XCA(X Certificate and key management)可视化程序管理SSL 证书(3)--创建自己定义的凭证管理中心(Certificate Authority)...

    在第"用XCA(X Certificate and key management)可视化程序管理SSL 证书(2)---创建证书请求"章节中,我们介绍了怎样用XCA创建SSL证书请 ...

  2. matlab氢原子杂化轨道,网络版原子和分子结构可视化程序的开发

    网络版原子和分子结构可视化程序的开发 以MATLAB为开发平台,编写一种基于网络的原子和分子结构可视化程序AMSW,有绘制主量子数小于9的任意类氢原 (本文共5页) 阅读全文>> 随着交通 ...

  3. Flash AS3.0实例教程:构建简单的声音可视化程序(波型图)

    本例为Flash AS3.0实例教程,在教程中我们将学习运用SoundMixer.computeSpectrum() 方法来构建简单的声音可视化程序(即波形图),希望能给朋友们带来帮助~~ AS3.0 ...

  4. python实现的、带GUI界面电影票房数据可视化程序

    代码地址如下: http://www.demodashi.com/demo/14588.html ##详细说明: Tushare是一个免费.开源的python财经数据接口包.主要实现对股票等金融数据从 ...

  5. 【Java例题】8.2 手工编写字符串统计的可视化程序

      2. 手工编写字符串统计的可视化程序. 一个Frame窗体容器,布局为null,两个TextField组件,一个Button组件. Button组件上添加ActionEvent事件监听器Actio ...

  6. c语言可视化学生管理系统,课程的设计C语言可视化程序学生成绩管理系统.doc

    课程的设计C语言可视化程序学生成绩管理系统 C语言可视化程序 设计报告 行政班级 机械0805 姓名 魏永涛 学号 0806080524 指导老师 夏建芳 课题名称 学生成绩管理系统 C可视化程序设计 ...

  7. ubuntu18.04+pcl1.8:运行visualization可视化程序,vtk6.3报错,vtk版本过低需重新安装对应版本vtk7.1.1

    1.测试程序,以及问题来源 #include <pcl/io/pcd_io.h> #include <pcl/io/ply_io.h> #include <pcl/poi ...

  8. c语言利用指针函数等完成学生成绩管理系统,课程设计C语言可视化程序学生成绩管理系统...

    <课程设计C语言可视化程序学生成绩管理系统>由会员分享,可在线阅读,更多相关<课程设计C语言可视化程序学生成绩管理系统(37页珍藏版)>请在人人文库网上搜索. 1.C语言可视化 ...

  9. [C++/Learning] 基于SMO的非线性支持向量机(SVM)可视化程序(附代码)

    前段时间一直准备在期末考试(竟然连着近2个月!),完全没时间捣鼓这些小玩意儿.现在准备过年终于有时间写代码了~.于是我就写了这样的一个SVM可视化程序来练练手. 这篇文章将提供基于smo求解SVM问题 ...

最新文章

  1. 《精通Unreal游戏引擎》一第4步 使用BSP创建地图
  2. 忽如一夜冬风来,团队忽然就解散了
  3. g4e基础篇#6 了解Git历史记录
  4. AI芯片格局最全分析
  5. 用javascript实现(页面正在加载的效果)
  6. mysql 从服务器同步数据_MySQL 同一台服务器同步数据
  7. Android 去除标题栏和状态栏的方法
  8. 大专java考试试题_专科—程序设计基础题库-java.doc
  9. 云计算之IasS、PasS、SaaS
  10. 第三章 平稳时间序列模型
  11. sqlserver修改主键id自增
  12. 踩坑记录:关于低版本firefox43.0.1在控件中定义onclick=remove(),点击按钮,按钮会消失。
  13. Flutter 淡入淡出与逐渐出现动画
  14. 20220408-CH9121串口转以太网模块学习
  15. 关于回溯法的递归与非递归-----N皇后问题
  16. java到大数据学习路线
  17. RFC8402 Segment Routing Architecture 翻译
  18. 小傻蛋的妹妹跟着小甲鱼学习Python的第七节007
  19. 概念---金融工程1:外汇的无套利定价模型
  20. 正则表达式匹配数字和字母组合,且不能为纯数字或纯字母

热门文章

  1. 儒家、道家、佛家不同思想文化!
  2. 元宇宙的六大核心技术
  3. 题组: 简单搜索进阶搜索
  4. 根据密码子生成蛋白质序列(根据字典破译密码)
  5. 华为matepad切换电脑模式_华为matepadpro可不可以当电脑(华为matepadpro可以当电脑吗)...
  6. tcp协议系列文章(2):从man 7 tcp开始
  7. 深圳软件测试培训:Docker下部署MySQL和Wordpress
  8. Linux:安装最新版本R + Rstudio成功使用最新R
  9. bios 闪存颗粒_使用USB闪存盘从失败的BIOS刷新中恢复HP Compaq BIOS危机
  10. GOOGLE Chrome谷歌翻译失效(用不了/打不开)的解决方法