Bases: XAImethod_Base
XAI Method for Integrated Gradients.
Normally, this class is used internally in the aucmedi.xai.decoder.xai_decoder in the AUCMEDI XAI module.
Reference - Implementation
Author: Aakash Kumar Nain
GitHub Profile: https://github.com/AakashKumarNain
Date: Jun 02, 2020
https://keras.io/examples/vision/integrated_gradients
Reference - Publication
Mukund Sundararajan, Ankur Taly, Qiqi Yan. 04 Mar 2017.
Axiomatic Attribution for Deep Networks.
https://arxiv.org/abs/1703.01365
This class provides functionality for running the compute_heatmap function,
which computes an Integrated Gradients Map for an image with a model.
Source code in aucmedi/xai/methods/integrated_gradients.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 | class IntegratedGradients(XAImethod_Base):
""" XAI Method for Integrated Gradients.
Normally, this class is used internally in the [aucmedi.xai.decoder.xai_decoder][] in the AUCMEDI XAI module.
??? abstract "Reference - Implementation"
Author: Aakash Kumar Nain <br>
GitHub Profile: [https://github.com/AakashKumarNain](https://github.com/AakashKumarNain) <br>
Date: Jun 02, 2020 <br>
[https://keras.io/examples/vision/integrated_gradients](https://keras.io/examples/vision/integrated_gradients) <br>
??? abstract "Reference - Publication"
Mukund Sundararajan, Ankur Taly, Qiqi Yan. 04 Mar 2017.
Axiomatic Attribution for Deep Networks.
<br>
[https://arxiv.org/abs/1703.01365](https://arxiv.org/abs/1703.01365)
This class provides functionality for running the compute_heatmap function,
which computes an Integrated Gradients Map for an image with a model.
"""
def __init__(self, model, layerName=None, num_steps=50):
""" Initialization function for creating a Integrated Gradients Map as XAI Method object.
Args:
model (keras.model): Keras model object.
layerName (str): Not required in Integrated Gradients Maps, but defined by Abstract Base Class.
num_steps (int): Number of iterations for interpolation.
"""
# Cache class parameters
self.model = model
self.num_steps = num_steps
#---------------------------------------------#
# Heatmap Computation #
#---------------------------------------------#
def compute_heatmap(self, image, class_index, eps=1e-8):
""" Core function for computing the Integrated Gradients 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 Integrated Gradients Map for provided image.
"""
# Perform interpolation
baseline = np.zeros(image.shape).astype(np.float32)
interpolated_imgs = []
for step in range(0, self.num_steps + 1):
cii = baseline + (step / self.num_steps) * (image - baseline)
interpolated_imgs.append(cii)
interpolated_imgs = np.array(interpolated_imgs).astype(np.float32)
# Get the gradients for each interpolated image
grads = []
for int_img in interpolated_imgs:
# Compute gradient
with tf.GradientTape() as tape:
inputs = tf.cast(int_img, tf.float32)
tape.watch(inputs)
preds = self.model(inputs)
loss = preds[:, class_index]
gradient = tape.gradient(loss, inputs)
# Add to gradient list
grads.append(gradient[0])
grads = tf.convert_to_tensor(grads, dtype=tf.float32)
# Approximate the integral using the trapezoidal rule
grads = (grads[:-1] + grads[1:]) / 2.0
avg_grads = tf.reduce_mean(grads, axis=0)
# Calculate integrated gradients
integrated_grads = (image - baseline) * avg_grads
# Obtain maximum gradient
integrated_grads = tf.reduce_max(integrated_grads, axis=-1)
# Convert to NumPy & Remove batch axis
heatmap = integrated_grads.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, num_steps=50)
Initialization function for creating a Integrated Gradients Map as XAI Method object.
Parameters:
Name |
Type |
Description |
Default |
model |
keras.model
|
Keras model object. |
required
|
layerName |
str
|
Not required in Integrated Gradients Maps, but defined by Abstract Base Class. |
None
|
num_steps |
int
|
Number of iterations for interpolation. |
50
|
Source code in aucmedi/xai/methods/integrated_gradients.py
51
52
53
54
55
56
57
58
59
60
61 | def __init__(self, model, layerName=None, num_steps=50):
""" Initialization function for creating a Integrated Gradients Map as XAI Method object.
Args:
model (keras.model): Keras model object.
layerName (str): Not required in Integrated Gradients Maps, but defined by Abstract Base Class.
num_steps (int): Number of iterations for interpolation.
"""
# Cache class parameters
self.model = model
self.num_steps = num_steps
|
compute_heatmap(image, class_index, eps=1e-08)
Core function for computing the Integrated Gradients 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 Integrated Gradients Map for provided image. |
Source code in aucmedi/xai/methods/integrated_gradients.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 | def compute_heatmap(self, image, class_index, eps=1e-8):
""" Core function for computing the Integrated Gradients 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 Integrated Gradients Map for provided image.
"""
# Perform interpolation
baseline = np.zeros(image.shape).astype(np.float32)
interpolated_imgs = []
for step in range(0, self.num_steps + 1):
cii = baseline + (step / self.num_steps) * (image - baseline)
interpolated_imgs.append(cii)
interpolated_imgs = np.array(interpolated_imgs).astype(np.float32)
# Get the gradients for each interpolated image
grads = []
for int_img in interpolated_imgs:
# Compute gradient
with tf.GradientTape() as tape:
inputs = tf.cast(int_img, tf.float32)
tape.watch(inputs)
preds = self.model(inputs)
loss = preds[:, class_index]
gradient = tape.gradient(loss, inputs)
# Add to gradient list
grads.append(gradient[0])
grads = tf.convert_to_tensor(grads, dtype=tf.float32)
# Approximate the integral using the trapezoidal rule
grads = (grads[:-1] + grads[1:]) / 2.0
avg_grads = tf.reduce_mean(grads, axis=0)
# Calculate integrated gradients
integrated_grads = (image - baseline) * avg_grads
# Obtain maximum gradient
integrated_grads = tf.reduce_max(integrated_grads, axis=-1)
# Convert to NumPy & Remove batch axis
heatmap = integrated_grads.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
|