gred-cam 的tensorflow实现 热力图

import cv2 as cv
import tensorflow as tf
import numpy as np
import os



def 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 conv
    output, grads_val = sess.run([conv_output, grads], feed_dict=feed_dict)
    print(output.shape)
    weights = np.mean(grads_val, axis=(1, 2)) # average pooling
    cams = np.sum(weights * output, axis=3)
    return cams


def 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 -> BGR
    cam = cv.resize(cam, (224, 224)) # enlarge heatmap
    cam = np.maximum(cam, 0)
    heatmap = cam / np.max(cam) # normalize
    cam = cv.applyColorMap(np.uint8(255 * heatmap), cv.COLORMAP_JET) 
    # balck-and-white to color
    cam = np.float32(cam) + np.float32(image) # everlay heatmap onto the image
    cam = 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  cam




def 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()

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

  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 26
    评论
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值