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: Compute GradCAM for a specific class index.
  
  Args:
      validation_data (Tuple[np.ndarray, Optional[np.ndarray]]): Validation data
          to perform the method on. Tuple containing (x, y).
      model (tf.keras.Model): tf.keras model to inspect
      class_index (int): Index of targeted class
      layer_name (str): Targeted layer for GradCAM. If no layer is provided, it is
          automatically infered from the model architecture.
      colormap (int): OpenCV Colormap to use for heatmap visualization
      image_weight (float): An optional `float` value in range [0,1] indicating the weight of
          the input image to be overlaying the calculated attribution maps. Defaults to `0.7`.
      use_guided_grads (boolean): Whether to use guided grads or raw gradients
  
  Returns:
      numpy.ndarray: Grid of all the GradCAM
docstr_tok: ["Compute","GradCAM","for","a","specific","class","index",".","Args",":","validation_data","(","Tuple","[","np.ndarray",",","Optional","[","np.ndarray","]","]",")",":","Validation","data","to","perform","the","method","on",".","Tuple","containing","(","x",",","y",")",".","model","(","tf.keras.Model",")",":","tf.keras","model","to","inspect","class_index","(","int",")",":","Index","of","targeted","class","layer_name","(","str",")",":","Targeted","layer","for","GradCAM",".","If","no","layer","is","provided",",","it","is","automatically","infered","from","the","model","architecture",".","colormap","(","int",")",":","OpenCV","Colormap","to","use","for","heatmap","visualization","image_weight","(","float",")",":","An","optional","`","float","`","value","in","range","[","0,1","]","indicating","the","weight","of","the","input","image","to","be","overlaying","the","calculated","attribution","maps",".","Defaults","to","`","0.7","`",".","use_guided_grads","(","boolean",")",":","Whether","to","use","guided","grads","or","raw","gradients","Returns",":","numpy.ndarray",":","Grid","of","all","the","GradCAM"]
code: def explain(
      self,
      validation_data,
      model,
      class_index,
      layer_name=None,
      use_guided_grads=True,
      colormap=cv2.COLORMAP_VIRIDIS,
      image_weight=0.7,
  ):
      """
      Compute GradCAM for a specific class index.
  
      Args:
          validation_data (Tuple[np.ndarray, Optional[np.ndarray]]): Validation data
              to perform the method on. Tuple containing (x, y).
          model (tf.keras.Model): tf.keras model to inspect
          class_index (int): Index of targeted class
          layer_name (str): Targeted layer for GradCAM. If no layer is provided, it is
              automatically infered from the model architecture.
          colormap (int): OpenCV Colormap to use for heatmap visualization
          image_weight (float): An optional `float` value in range [0,1] indicating the weight of
              the input image to be overlaying the calculated attribution maps. Defaults to `0.7`.
          use_guided_grads (boolean): Whether to use guided grads or raw gradients
  
      Returns:
          numpy.ndarray: Grid of all the GradCAM
      """
      images, _ = validation_data
  
      if layer_name is None:
          layer_name = self.infer_grad_cam_target_layer(model)
  
      outputs, grads = GradCAM.get_gradients_and_filters(
          model, images, layer_name, class_index, use_guided_grads
      )
  
      cams = GradCAM.generate_ponderated_output(outputs, grads)
  
      heatmaps = np.array(
          [
              # not showing the actual image if image_weight=0
              heatmap_display(
                  cam.numpy(), image, colormap, image_weight
              )
              for cam, image in zip(cams, images)
          ]
      )
  
      grid = grid_display(heatmaps)
  
      return grid
  
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 1 for calling the GradCAM obj's underlying member method code with example usage: obj.explain(validation_data, model, class_index, layer_name, use_guided_grads, colormap, image_weight) and returns: grid