Skip to content

Gradcam guided

GuidedGradCAM ¤

Bases: XAImethod_Base

XAI Method for Guided Grad-CAM.

Normally, this class is used internally in the aucmedi.xai.decoder.xai_decoder in the AUCMEDI XAI module.

Reference - Implementation

Author: Swapnil Ahlawat
Date: Jul 06, 2020
https://github.com/swapnil-ahlawat/Guided-GradCAM-Keras

Reference - Publication #1

Jost Tobias Springenberg, Alexey Dosovitskiy, Thomas Brox, Martin Riedmiller. 21 Dec 2014. Striving for Simplicity: The All Convolutional Net.
https://arxiv.org/abs/1412.6806

Reference - Publication #2

Ramprasaath R. Selvaraju, Michael Cogswell, Abhishek Das, Ramakrishna Vedantam, Devi Parikh, Dhruv Batra. 7 Oct 2016. Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization.
https://arxiv.org/abs/1610.02391

This class provides functionality for running the compute_heatmap function, which computes a Guided Grad-CAM heatmap for an image with a model.

Source code in aucmedi/xai/methods/gradcam_guided.py
 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
class GuidedGradCAM(XAImethod_Base):
    """ XAI Method for Guided Grad-CAM.

    Normally, this class is used internally in the [aucmedi.xai.decoder.xai_decoder][] in the AUCMEDI XAI module.

    ??? abstract "Reference - Implementation"
        Author: Swapnil Ahlawat <br>
        Date: Jul 06, 2020 <br>
        [https://github.com/swapnil-ahlawat/Guided-GradCAM-Keras](https://github.com/swapnil-ahlawat/Guided-GradCAM-Keras) <br>

    ??? abstract "Reference - Publication #1"
        Jost Tobias Springenberg, Alexey Dosovitskiy, Thomas Brox, Martin Riedmiller. 21 Dec 2014.
        Striving for Simplicity: The All Convolutional Net.
        <br>
        [https://arxiv.org/abs/1412.6806](https://arxiv.org/abs/1412.6806)

    ??? abstract "Reference - Publication #2"
        Ramprasaath R. Selvaraju, Michael Cogswell, Abhishek Das, Ramakrishna Vedantam, Devi Parikh, Dhruv Batra. 7 Oct 2016.
        Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization.
        <br>
        [https://arxiv.org/abs/1610.02391](https://arxiv.org/abs/1610.02391)

    This class provides functionality for running the compute_heatmap function,
    which computes a Guided Grad-CAM heatmap for an image with a model.
    """
    def __init__(self, model, layerName=None):
        """ Initialization function for creating a Guided Grad-CAM as XAI Method object.

        Args:
            model (keras.model):               Keras model object.
            layerName (str):                   Layer name of the convolutional layer for heatmap computation.
        """
        # Initialize XAI methods
        self.bp = GuidedBackpropagation(model, layerName)
        self.gc = GradCAM(model, layerName)

    #---------------------------------------------#
    #             Heatmap Computation             #
    #---------------------------------------------#
    def compute_heatmap(self, image, class_index, eps=1e-8):
        """ Core function for computing the Guided Grad-CAM heatmap 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 Guided Grad-CAM for provided image.
        """
        # Compute Guided Backpropagation
        hm_bp = self.bp.compute_heatmap(image, class_index, eps)
        # Compute Grad-CAM
        hm_gc = self.gc.compute_heatmap(image, class_index, eps)
        hm_gc = Resize(shape=image.shape[1:-1]).transform(hm_gc)
        # Combine both XAI methods
        heatmap = hm_bp * hm_gc
        # 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 Guided Grad-CAM as XAI Method object.

Parameters:

Name Type Description Default
model keras.model

Keras model object.

required
layerName str

Layer name of the convolutional layer for heatmap computation.

None
Source code in aucmedi/xai/methods/gradcam_guided.py
58
59
60
61
62
63
64
65
66
67
def __init__(self, model, layerName=None):
    """ Initialization function for creating a Guided Grad-CAM as XAI Method object.

    Args:
        model (keras.model):               Keras model object.
        layerName (str):                   Layer name of the convolutional layer for heatmap computation.
    """
    # Initialize XAI methods
    self.bp = GuidedBackpropagation(model, layerName)
    self.gc = GradCAM(model, layerName)

compute_heatmap(image, class_index, eps=1e-08) ¤

Core function for computing the Guided Grad-CAM heatmap 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 Guided Grad-CAM for provided image.

Source code in aucmedi/xai/methods/gradcam_guided.py
 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
def compute_heatmap(self, image, class_index, eps=1e-8):
    """ Core function for computing the Guided Grad-CAM heatmap 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 Guided Grad-CAM for provided image.
    """
    # Compute Guided Backpropagation
    hm_bp = self.bp.compute_heatmap(image, class_index, eps)
    # Compute Grad-CAM
    hm_gc = self.gc.compute_heatmap(image, class_index, eps)
    hm_gc = Resize(shape=image.shape[1:-1]).transform(hm_gc)
    # Combine both XAI methods
    heatmap = hm_bp * hm_gc
    # 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