def grad_cam(img, imgs0, end_points, sess, predicted_class, layer_name, nb_classes, eval_image_size): # Conv layer tensor [?,10,10,2048] conv_layer = end_points[layer_name] # [1000]-D tensor with target class index set to 1 and rest as 0 one_hot = tf.sparse_to_dense(predicted_class, [nb_classes], 1.0) signal = tf.multiply(end_points[_logits_name], one_hot) loss = tf.reduce_mean(signal) grads = tf.gradients(loss, conv_layer)[0] # Normalizing the gradients norm_grads = tf.divide(grads, tf.sqrt(tf.reduce_mean(tf.square(grads))) + tf.constant(1e-5)) output, grads_val = sess.run([conv_layer, norm_grads], feed_dict={imgs0: img}) output = output[0] # [10,10,2048] grads_val = grads_val[0] # [10,10,2048] weights = np.mean(grads_val, axis = (0, 1)) # [2048] cam = np.ones(output.shape[0 : 2], dtype = np.float32) # [10,10] # Taking a weighted average for i, w in enumerate(weights): cam += w * output[:, :, i] # Passing through ReLU cam = np.maximum(cam, 0) cam = cam / np.max(cam) cam3 = cv2.resize(cam, (eval_image_size,eval_image_size)) return cam3
时间: 2023-06-04 10:05:28 浏览: 187
浅谈pytorch grad_fn以及权重梯度不更新的问题
这是一个用于生成Grad-CAM的函数。它需要一个输入图像img、一组参考图像imgs0、模型的中间层特征图end_points、会话sess、预测的类别predicted_class、目标层的名称layer_name、类别数nb_classes以及评估图像的尺寸eval_image_size。函数将提取目标层的特征图,并计算出每个特征图对目标类别的重要性,生成Grad-CAM图像。
阅读全文