Bases: XAImethod_Base
  
      XAI Method for Saliency Map (also called Backpropagation).
Normally, this class is used internally in the aucmedi.xai.decoder.xai_decoder in the AUCMEDI XAI module.
Reference - Implementation #1
Author: Yasuhiro Kubota 
GitHub Profile: https://github.com/keisen 
Date: Aug 11, 2020 
https://github.com/keisen/tf-keras-vis/ 
 
Reference - Implementation #2
Author: Huynh Ngoc Anh 
GitHub Profile: https://github.com/experiencor 
Date: Jun 23, 2017 
https://github.com/experiencor/deep-viz-keras/ 
 
Reference - Publication
Karen Simonyan, Andrea Vedaldi, Andrew Zisserman. 20 Dec 2013.
Deep Inside Convolutional Networks: Visualising Image Classification Models and Saliency Maps.
https://arxiv.org/abs/1312.6034
 
This class provides functionality for running the compute_heatmap function,
which computes a Saliency Map for an image with a model.
        
          Source code in aucmedi/xai/methods/saliency.py
           31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107  | class SaliencyMap(XAImethod_Base):
    """ XAI Method for Saliency Map (also called Backpropagation).
    Normally, this class is used internally in the [aucmedi.xai.decoder.xai_decoder][] in the AUCMEDI XAI module.
    ??? abstract "Reference - Implementation #1"
        Author: Yasuhiro Kubota <br>
        GitHub Profile: [https://github.com/keisen](https://github.com/keisen) <br>
        Date: Aug 11, 2020 <br>
        [https://github.com/keisen/tf-keras-vis/](https://github.com/keisen/tf-keras-vis/) <br>
    ??? abstract "Reference - Implementation #2"
        Author: Huynh Ngoc Anh <br>
        GitHub Profile: [https://github.com/experiencor](https://github.com/experiencor) <br>
        Date: Jun 23, 2017 <br>
        [https://github.com/experiencor/deep-viz-keras/](https://github.com/experiencor/deep-viz-keras/) <br>
    ??? abstract "Reference - Publication"
        Karen Simonyan, Andrea Vedaldi, Andrew Zisserman. 20 Dec 2013.
        Deep Inside Convolutional Networks: Visualising Image Classification Models and Saliency Maps.
        <br>
        [https://arxiv.org/abs/1312.6034](https://arxiv.org/abs/1312.6034)
    This class provides functionality for running the compute_heatmap function,
    which computes a Saliency Map for an image with a model.
    """
    def __init__(self, model, layerName=None):
        """ Initialization function for creating a Saliency Map as XAI Method object.
        Args:
            model (keras.model):               Keras model object.
            layerName (str):                   Not required in Saliency Maps, but defined by Abstract Base Class.
        """
        # Cache class parameters
        self.model = model
    #---------------------------------------------#
    #             Heatmap Computation             #
    #---------------------------------------------#
    def compute_heatmap(self, image, class_index, eps=1e-8):
        """ Core function for computing the Saliency Map for a provided image and for specific classification outcome.
        ???+ attention
            Be aware that the image has to be provided in batch format.
        Args:
            image (numpy.ndarray):              Image matrix encoded as NumPy Array (provided as one-element batch).
            class_index (int):                  Classification index for which the heatmap should be computed.
            eps (float):                        Epsilon for rounding.
        The returned heatmap is encoded within a range of [0,1]
        ???+ attention
            The shape of the returned heatmap is 2D -> batch and channel axis will be removed.
        Returns:
            heatmap (numpy.ndarray):            Computed Saliency Map for provided image.
        """
        # Compute gradient for desierd class index
        with tf.GradientTape() as tape:
            inputs = tf.cast(image, tf.float32)
            tape.watch(inputs)
            preds = self.model(inputs)
            loss = preds[:, class_index]
        gradient = tape.gradient(loss, inputs)
        # Obtain maximum gradient based on feature map of last conv layer
        gradient = tf.reduce_max(gradient, axis=-1)
        # Convert to NumPy & Remove batch axis
        heatmap = gradient.numpy()[0]
        # Intensity normalization to [0,1]
        numer = heatmap - np.min(heatmap)
        denom = (heatmap.max() - heatmap.min()) + eps
        heatmap = numer / denom
        # Return the resulting heatmap
        return heatmap
  | 
 
         
  
  
__init__(model, layerName=None)
  
  
      Initialization function for creating a Saliency Map as XAI Method object.
  Parameters:
  
    
      
        | Name | 
        Type | 
        Description | 
        Default | 
      
    
    
        
          model | 
          
                keras.model
           | 
          Keras model object.  | 
          
              required
           | 
        
        
          layerName | 
          
                str
           | 
          Not required in Saliency Maps, but defined by Abstract Base Class.  | 
          
                None
           | 
        
    
  
      
        Source code in aucmedi/xai/methods/saliency.py
        57
58
59
60
61
62
63
64
65  | def __init__(self, model, layerName=None):
    """ Initialization function for creating a Saliency Map as XAI Method object.
    Args:
        model (keras.model):               Keras model object.
        layerName (str):                   Not required in Saliency Maps, but defined by Abstract Base Class.
    """
    # Cache class parameters
    self.model = model
  | 
 
       
   
 
compute_heatmap(image, class_index, eps=1e-08)
  
  
      Core function for computing the Saliency Map for a provided image and for specific classification outcome.
Attention
Be aware that the image has to be provided in batch format.
 
  Parameters:
  
    
      
        | Name | 
        Type | 
        Description | 
        Default | 
      
    
    
        
          image | 
          
                numpy.ndarray
           | 
          Image matrix encoded as NumPy Array (provided as one-element batch).  | 
          
              required
           | 
        
        
          class_index | 
          
                int
           | 
          Classification index for which the heatmap should be computed.  | 
          
              required
           | 
        
        
          eps | 
          
                float
           | 
          Epsilon for rounding.  | 
          
                1e-08
           | 
        
    
  
      The returned heatmap is encoded within a range of [0,1]
Attention
The shape of the returned heatmap is 2D -> batch and channel axis will be removed.
 
  Returns:
  
    
      
| Name |         Type | 
        Description | 
      
    
    
        
heatmap |           
                numpy.ndarray
           | 
          Computed Saliency Map for provided image.  | 
        
    
  
      
        Source code in aucmedi/xai/methods/saliency.py
         70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107  | def compute_heatmap(self, image, class_index, eps=1e-8):
    """ Core function for computing the Saliency Map for a provided image and for specific classification outcome.
    ???+ attention
        Be aware that the image has to be provided in batch format.
    Args:
        image (numpy.ndarray):              Image matrix encoded as NumPy Array (provided as one-element batch).
        class_index (int):                  Classification index for which the heatmap should be computed.
        eps (float):                        Epsilon for rounding.
    The returned heatmap is encoded within a range of [0,1]
    ???+ attention
        The shape of the returned heatmap is 2D -> batch and channel axis will be removed.
    Returns:
        heatmap (numpy.ndarray):            Computed Saliency Map for provided image.
    """
    # Compute gradient for desierd class index
    with tf.GradientTape() as tape:
        inputs = tf.cast(image, tf.float32)
        tape.watch(inputs)
        preds = self.model(inputs)
        loss = preds[:, class_index]
    gradient = tape.gradient(loss, inputs)
    # Obtain maximum gradient based on feature map of last conv layer
    gradient = tf.reduce_max(gradient, axis=-1)
    # Convert to NumPy & Remove batch axis
    heatmap = gradient.numpy()[0]
    # Intensity normalization to [0,1]
    numer = heatmap - np.min(heatmap)
    denom = (heatmap.max() - heatmap.min()) + eps
    heatmap = numer / denom
    # Return the resulting heatmap
    return heatmap
  |