class_docstr: Perform Grad CAM algorithm for a given input
  
  Paper: [Grad-CAM: Visual Explanations from Deep Networks
          via Gradient-based Localization](https://arxiv.org/abs/1610.02391)
class_docstr_tok: ["Perform","Grad","CAM","algorithm","for","a","given","input","Paper",":","[","Grad-CAM",":","Visual","Explanations","from","Deep","Networks","via","Gradient-based","Localization","]","(","https",":","\/\/arxiv.org\/abs\/1610.02391",")"]
docstr: Generate guided gradients and convolutional outputs with an inference.
  
  Args:
      model (tf.keras.Model): tf.keras model to inspect
      images (numpy.ndarray): 4D-Tensor with shape (batch_size, H, W, 3)
      layer_name (str): Targeted layer for GradCAM
      class_index (int): Index of targeted class
      use_guided_grads (boolean): Whether to use guided grads or raw gradients
  
  Returns:
      Tuple[tf.Tensor, tf.Tensor]: (Target layer outputs, Guided gradients)
docstr_tok: ["Generate","guided","gradients","and","convolutional","outputs","with","an","inference",".","Args",":","model","(","tf.keras.Model",")",":","tf.keras","model","to","inspect","images","(","numpy.ndarray",")",":","4D-Tensor","with","shape","(","batch_size",",","H",",","W",",","3",")","layer_name","(","str",")",":","Targeted","layer","for","GradCAM","class_index","(","int",")",":","Index","of","targeted","class","use_guided_grads","(","boolean",")",":","Whether","to","use","guided","grads","or","raw","gradients","Returns",":","Tuple","[","tf.Tensor",",","tf.Tensor","]",":","(","Target","layer","outputs",",","Guided","gradients",")"]
code: def get_gradients_and_filters(
      model, images, layer_name, class_index, use_guided_grads
  ):
      """
      Generate guided gradients and convolutional outputs with an inference.
  
      Args:
          model (tf.keras.Model): tf.keras model to inspect
          images (numpy.ndarray): 4D-Tensor with shape (batch_size, H, W, 3)
          layer_name (str): Targeted layer for GradCAM
          class_index (int): Index of targeted class
          use_guided_grads (boolean): Whether to use guided grads or raw gradients
  
      Returns:
          Tuple[tf.Tensor, tf.Tensor]: (Target layer outputs, Guided gradients)
      """
      grad_model = tf.keras.models.Model(
          [model.inputs],
          [model.get_layer(layer_name).output, model.output],
      )
  
      with tf.GradientTape() as tape:
          inputs = tf.cast(images, tf.float32)
          tape.watch(inputs)
          conv_outputs, predictions = grad_model(inputs)
          loss = predictions[:, class_index]
  
      grads = tape.gradient(loss, conv_outputs)
  
      if use_guided_grads:
          grads = (
              tf.cast(conv_outputs > 0, "float32")
              * tf.cast(grads > 0, "float32")
              * grads
          )
  
      return conv_outputs, grads
  
file_path: tf-explain/tf_explain/core/grad_cam.py
imports: import numpy
  import tensorflow
  import cv2
  from tf_explain.utils.display import grid_display, heatmap_display
  from tf_explain.utils.saver import save_rgb
label_desc: Use image node_id 3 for calling the GradCAM obj's underlying member method code with example usage: obj.get_gradients_and_filters(model, images, layer_name, class_index, use_guided_grads) and returns: conv_outputs,  grads