import cv2 as cv
import tensorflow as tf
import numpy as np
import osdef grad_cam(prob_name, label, layer_name, sess, feed_dict, nb_classes):"""prob_name为softmax输出层节点名, label标签, layer_name最后一层卷积层的节点名, sess,                 feed_dict, nb_classes分类数"""prob = sess.graph.get_tensor_by_name(prob_name + ':0')loss = tf.multiply(prob, tf.one_hot([label], nb_classes))reduced_loss = tf.reduce_sum(loss[0])conv_output = sess.graph.get_tensor_by_name(layer_name + ':0')grads = tf.gradients(reduced_loss, conv_output)[0] # d loss / d convoutput, grads_val = sess.run([conv_output, grads], feed_dict=feed_dict)print(output.shape)weights = np.mean(grads_val, axis=(1, 2)) # average poolingcams = np.sum(weights * output, axis=3)return camsdef save_cam(cam, image, save_path):"""save Grad-CAM images"""cam = cam[0] # the first GRAD-CAM for the first image in  batch# image = np.uint8(image_batch[0][:, :, ::-1] * 255.0) # RGB -> BGRcam = cv.resize(cam, (224, 224)) # enlarge heatmapcam = np.maximum(cam, 0)heatmap = cam / np.max(cam) # normalizecam = cv.applyColorMap(np.uint8(255 * heatmap), cv.COLORMAP_JET) # balck-and-white to colorcam = np.float32(cam) + np.float32(image) # everlay heatmap onto the imagecam = 255 * cam / np.max(cam)cam = np.uint8(cam)cv.imwrite(save_path+“cam.jpg”, cam)cv.imwrite(save_path+“heatmap.jpg”, (heatmap * 255.0).astype(np.uint8))cv.imwrite(save_path+“segmentation.jpg”, (heatmap[:, :, None].astype(float) * image).astype(np.uint8))return  camdef main():IMAGE_PATH = '/Users....'output_node_names = "newnet/net_v/prob"final_conv_name="newnet/net_v/res8_1/res8_1/pw_linear_bn/cond/Merge"model_path = '/Users....'# ckpt = tf.train.get_checkpoint_state(model_path)  # 通过检查点文件锁定最新的模型saver = tf.train.import_meta_graph(model_path + '.meta')  # 载入图结构,保存在.meta文件中with tf.Session() as sess:saver.restore(sess, model_path)  # 载入参数,参数保存在两个文件中,不过restore会自己寻找img_m = cv.imread(IMAGE_PATH)img_m = cv.resize(img_m, (224, 224))img_m = tf.cast(img_m, tf.float32)img_m = tf.reshape(img_m, [224, 224, 3])img_m_f= sess.run([img_m])input_image_tensor_m = sess.graph.get_tensor_by_name("input/input_m:0")input_is_training_tensor = sess.graph.get_tensor_by_name("input/is_training:0")cam=grad_cam(prob_name=output_node_names, label=0, layer_name=final_conv_name, sess=sess,
feed_dict={input_image_tensor_m: [img_m_f],                                                                                                                           input_is_training_tensor: False},nb_classes=4)dst_m=save_cam(cam, img_m, 'm')cv.imshow('dst_m_v',dst_m)cv.waitKey(0)# print(cam)if __name__ == '__main__':main()

这是我自己项目的案例,大家可以以此为例子进行适配,难点在于必须知道对应节点的名称

gred-cam 的tensorflow实现 热力图相关推荐

  1. 特征可视化技术(CAM)

    https://zhuanlan.zhihu.com/p/269702192 CAM技术可以帮助我们理解CNN在图像中寻找目标对象的过程,并且可以用于可视化CNN的中间层特征,以及对图像分类结果的解释 ...

  2. 【CAM】Learning Deep Features for Discriminative Localization

    论文链接 github Abstract 1. Introduction CNN能保留位置信息,但经过用于分类的全连接神经网络时会丢失位置信息.最近的NIN和GoogLeNet使用全卷积网络.避免使用 ...

  3. 一文详述Attention最新进展

    关注上方"深度学习技术前沿",选择"星标公众号", 资源干货,第一时间送达! 作者:sisiyou 知乎链接:https://zhuanlan.zhihu.co ...

  4. Attention最新进展

    本文主要总结了最新的关于attention的应用文章. Attention-based Dropout Layer for Weakly Supervised Object Localization( ...

  5. 福利丨一门面向所有人的人工智能公开课:MIT 6.S191,深度学习入门

    对初学者来说,有没有易于上手,使用流行神经网络框架进行教学的深度学习课程?近日,麻省理工学院(MIT)正式开源了在线介绍性课程「MIT 6.S191:深度学习入门」.该课程包括一系列有关神经网络及其在 ...

  6. Attention技术应用的最新进展概述

    来源:https://zhuanlan.zhihu.com/ p/130388873 作者:一块小蛋糕 编辑:深度传送门 本文主要总结了最新的关于attention的应用文章. Attention-b ...

  7. 【学习打卡】GradCAM可解释性分析

    文章目录 引言 AI发展 CAM算法缺点 高精度与可解释性 Grad-CAM 算法原理 应用:兼容各样任务 Guided Grad-CAM Guided Backprop 原理 Guided Grad ...

  8. 【学习打卡03】可解释机器学习笔记之CAM类激活热力图

    可解释机器学习笔记之CAM类激活热力图 文章目录 可解释机器学习笔记之CAM类激活热力图 CAM介绍 CAM算法原理 GAP全局平均池化 GAP VS GMP CAM算法的缺点及改进 CAM可视化 同 ...

  9. CNN可视化!从CVPR 2022出发,聊聊CAM是如何激活我们文章的热度!

    点击下方卡片,关注"CVer"公众号 AI/CV重磅干货,第一时间送达 转载自:极市平台  | 作者:matrix明仔 导读 本文从CVPR2022中三篇不同领域的文章中CAM的表 ...

最新文章

  1. linux mysql 5.7.12_Linux环境mysql5.7.12安装教程
  2. 外包干了四年,废了!
  3. [转]Extending the User Interface in Outlook 2010
  4. Eigen矩阵的运算(二)
  5. java静态分派_Java基础——重载、静态分派与动态分派
  6. 项目打包去掉调试时的NSLog、print
  7. c mysql备份还原数据库,MySQL数据库备份与恢复方法
  8. 针对Java中的XSD验证XML
  9. Tensorflow保存神经网络参数有妙招:Saver和Restore
  10. leetCode 203. Remove Linked List Elements 链表
  11. 基于大数据的精准教学模式探究
  12. 在centos7环境下运行.exe文件
  13. http上传文件原理
  14. CS61A 计算机程序的构造与解释 课程介绍及课程学习总结
  15. 英语拼写鉴别器_CodingPark编程公园
  16. 为何说 a16z 是一家通过 VC 获利的媒体公司?
  17. linux运维工程师2017,2017年Linux运维工程师经典面试题汇总
  18. html 水印插件,Watermark.js 在浏览器端添加水印插件 - 文章教程
  19. 计算机与仪表电缆,计算机与仪表电缆
  20. 倍控G30-J4125工控机开箱

热门文章

  1. 常用网站攻击手段及防御方法
  2. 浅谈HTTP缓存机制
  3. Temporal Anti-Aliasing(时域抗锯齿TAA)
  4. 在Sever 2012中应用iSCSI目标程序
  5. 目前最精准的Android开源计步器
  6. 两种取汉字拼音首字母的方法.--函数为转抄而来.
  7. 相伴十六载,讲讲我和数据仓库的故事(一)
  8. 广西首届网络安全选拔赛 MISC Wirteup
  9. 激光雷达与毫米波雷达的区别
  10. 2022.03.24【基因组组装】|获取比对到参考基因组的contig序列