Skip to content

Xai base

XAImethod_Base ¤

Bases: ABC

An abstract base class for a XAI Method class.

Normally, this class is used internally in the xai_decoder function in the AUCMEDI XAI module.

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

Create a custom XAImethod
from aucmedi.xai.methods.xai_base import XAImethod_Base

class My_custom_XAImethod(XAImethod_Base):
    def __init__(self, model, layerName=None):
        pass

    def compute_heatmap(self, image, class_index, eps=1e-8):
        pass
Required Functions
Function Description
__init__() Object creation function.
compute_heatmap() Application of the XAI Method on an image.
Source code in aucmedi/xai/methods/xai_base.py
28
29
30
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
class XAImethod_Base(ABC):
    """ An abstract base class for a XAI Method class.

    Normally, this class is used internally in the xai_decoder function in the AUCMEDI XAI module.

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

    ???+ example "Create a custom XAImethod"
        ```python
        from aucmedi.xai.methods.xai_base import XAImethod_Base

        class My_custom_XAImethod(XAImethod_Base):
            def __init__(self, model, layerName=None):
                pass

            def compute_heatmap(self, image, class_index, eps=1e-8):
                pass
        ```

    ???+ info "Required Functions"
        | Function            | Description                                |
        | ------------------- | ------------------------------------------ |
        | `__init__()`        | Object creation function.                  |
        | `compute_heatmap()` | Application of the XAI Method on an image. |

    """
    #---------------------------------------------#
    #                Initialization               #
    #---------------------------------------------#
    @abstractmethod
    def __init__(self, model, layerName=None):
        """ Initialization function for creating a XAI Method object.
        ```
        __init__(model, layerName=None)
        ```

        Args:
            model (keras.model):               Keras model object.
            layerName (str):                Layer name of the convolutional layer for heatmap computation.
        """
        pass

    #---------------------------------------------#
    #             Heatmap Computation             #
    #---------------------------------------------#
    def compute_heatmap(self, image, class_index, eps=1e-8):
        """ Core function for computing the XAI 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 should be 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 XAI heatmap for provided image.
        """
        pass

__init__(model, layerName=None) abstractmethod ¤

Initialization function for creating a XAI Method object.

__init__(model, layerName=None)

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/xai_base.py
58
59
60
61
62
63
64
65
66
67
68
69
@abstractmethod
def __init__(self, model, layerName=None):
    """ Initialization function for creating a XAI Method object.
    ```
    __init__(model, layerName=None)
    ```

    Args:
        model (keras.model):               Keras model object.
        layerName (str):                Layer name of the convolutional layer for heatmap computation.
    """
    pass

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

Core function for computing the XAI 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 should be 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 XAI heatmap for provided image.

Source code in aucmedi/xai/methods/xai_base.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def compute_heatmap(self, image, class_index, eps=1e-8):
    """ Core function for computing the XAI 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 should be 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 XAI heatmap for provided image.
    """
    pass