gradcam.gradcam

References

[1]Ramprasaath R. Selvaraju, Michael Cogswell, Abhishek Das, Ramakrishna Vedantam, Devi Parikh, Dhruv Batra “Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization” ICCV2017
class Gradcam(x_placeholder, num_classes, featuremap_op_name, predict_op_name=None, graph=None)[source]

Gradcam Class

Parameters:
  • x_placeholder (tf.Tensor) – Data place holder Tensor from tf.placeholder()
  • num_classes (int) – number of classes
  • featuremap_op_name (str) – Operation name of CNN feature map layer To get the list of candidate names, use Gradcam.candidate_featuremap_op_names()
  • predict_op_name (str) – Operation name of prediction layer (decision output) To get the list of candidate names, use Gradcam.candidate_predict_op_names()
  • graph (tf.Graph) – Tensorflow graph
static candidate_featuremap_op_names(sess, graph=None, feed_options=None)[source]

Returns the list of candidates for operation names of CNN feature map layer

Parameters:
  • sess (tf.Session) – Tensorflow session
  • graph (tf.Graph) – Tensorflow graph
  • feed_options (dict) – Optional parameters to graph
Returns:

list – String list of candidates

static candidate_predict_op_names(sess, num_classes, graph=None, feed_options=None)[source]

Returns the list of candidate for operation names of prediction layer

Parameters:
  • sess (tf.Session) – Tensorflow session
  • num_classes (int) – Number of prediction classes
  • graph (tf.Graph) – Tensorflow graph
  • feed_options (dict) – Optional parameters to graph
Returns:

list – String list of candidates

gradcam(sess, input_data, target_index=None, feed_options={})[source]

Calculate Grad-CAM (class activation map) and Guided Grad-CAM for given input on target class

Parameters:
  • sess (tf.Session) – Tensorflow session
  • input_data (numpy.ndarray) – A single input instance
  • target_index (int) – Target class index If None, predicted class index is used
  • feed_options (dict) – Optional parameters to graph
Returns:

dict

Note

Keys in return:
  • gradcam_img: Heatmap overlayed on input
  • guided_gradcam_img: Guided Grad-CAM result
  • heatmap: Heatmap of input on the target class
  • guided_backprop: Guided backprop result
static overlay_gradcam(image, heatmap)[source]

Overlay heatmap on input data