Bases: XAImethod_Base
XAI Method for LIME Pro.
Normally, this class is used internally in the aucmedi.xai.decoder.xai_decoder in the AUCMEDI XAI module.
Reference - Implementation
Lime: Explaining the predictions of any machine learning classifier
GitHub: https://github.com/marcotcr/lime
Reference - Publication
Marco Tulio Ribeiro, Sameer Singh, Carlos Guestrin. 9 Aug 2016.
"Why Should I Trust You?": Explaining the Predictions of Any Classifier
https://arxiv.org/abs/1602.04938
This class provides functionality for running the compute_heatmap function,
which computes a Lime Pro Map for an image with a model.
Source code in aucmedi/xai/methods/lime_pro.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 | class LimePro(XAImethod_Base):
""" XAI Method for LIME Pro.
Normally, this class is used internally in the [aucmedi.xai.decoder.xai_decoder][] in the AUCMEDI XAI module.
??? abstract "Reference - Implementation"
Lime: Explaining the predictions of any machine learning classifier <br>
GitHub: [https://github.com/marcotcr/lime](https://github.com/marcotcr/lime) <br>
??? abstract "Reference - Publication"
Marco Tulio Ribeiro, Sameer Singh, Carlos Guestrin. 9 Aug 2016.
"Why Should I Trust You?": Explaining the Predictions of Any Classifier
<br>
[https://arxiv.org/abs/1602.04938](https://arxiv.org/abs/1602.04938)
This class provides functionality for running the compute_heatmap function,
which computes a Lime Pro Map for an image with a model.
"""
def __init__(self, model, layerName=None, num_samples=1000):
""" Initialization function for creating a Lime Pro Map as XAI Method object.
Args:
model (keras.model): Keras model object.
layerName (str): Not required in LIME Pro/Con Maps, but defined by Abstract Base Class.
num_samples (int): Number of iterations for LIME instance explanation.
"""
# Cache class parameters
self.model = model
self.num_samples = num_samples
#---------------------------------------------#
# Heatmap Computation #
#---------------------------------------------#
def compute_heatmap(self, image, class_index, eps=1e-8):
""" Core function for computing the Lime Pro 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 Lime Pro Map for provided image.
"""
# Initialize LIME explainer
explainer = lime_image.LimeImageExplainer()
explanation = explainer.explain_instance(image[0].astype("double"),
self.model.predict, hide_color=0,
labels=(class_index,),
num_samples=self.num_samples)
# Obtain PRO explanation mask
temp, mask = explanation.get_image_and_mask(class_index, hide_rest=True,
positive_only=True, negative_only=False)
heatmap = mask
# 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, num_samples=1000)
Initialization function for creating a Lime Pro Map as XAI Method object.
Parameters:
Name |
Type |
Description |
Default |
model |
keras.model
|
Keras model object. |
required
|
layerName |
str
|
Not required in LIME Pro/Con Maps, but defined by Abstract Base Class. |
None
|
num_samples |
int
|
Number of iterations for LIME instance explanation. |
1000
|
Source code in aucmedi/xai/methods/lime_pro.py
49
50
51
52
53
54
55
56
57
58
59 | def __init__(self, model, layerName=None, num_samples=1000):
""" Initialization function for creating a Lime Pro Map as XAI Method object.
Args:
model (keras.model): Keras model object.
layerName (str): Not required in LIME Pro/Con Maps, but defined by Abstract Base Class.
num_samples (int): Number of iterations for LIME instance explanation.
"""
# Cache class parameters
self.model = model
self.num_samples = num_samples
|
compute_heatmap(image, class_index, eps=1e-08)
Core function for computing the Lime Pro 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 Lime Pro Map for provided image. |
Source code in aucmedi/xai/methods/lime_pro.py
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 | def compute_heatmap(self, image, class_index, eps=1e-8):
""" Core function for computing the Lime Pro 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 Lime Pro Map for provided image.
"""
# Initialize LIME explainer
explainer = lime_image.LimeImageExplainer()
explanation = explainer.explain_instance(image[0].astype("double"),
self.model.predict, hide_color=0,
labels=(class_index,),
num_samples=self.num_samples)
# Obtain PRO explanation mask
temp, mask = explanation.get_image_and_mask(class_index, hide_rest=True,
positive_only=True, negative_only=False)
heatmap = mask
# 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
|