Basic API
All methods in ExplainableAI.jl work by calling analyze
on an input and an analyzer:
XAIBase.analyze
— Functionanalyze(input, method)
analyze(input, method, output_selection)
Apply the analyzer method
for the given input, returning an Explanation
. If output_selection
is specified, the explanation will be calculated for that output. Otherwise, the output with the highest activation is automatically chosen.
See also Explanation
.
XAIBase.Explanation
— TypeExplanation(val, output, output_selection, analyzer, heatmap, extras)
Return type of analyzers when calling analyze
.
Fields
val
: numerical output of the analyzer, e.g. an attribution or gradientoutput
: model output for the given analyzer inputoutput_selection
: index of the output used for the explanationanalyzer
: symbol corresponding the used analyzer, e.g.:Gradient
or:LRP
heatmap
: symbol indicating a preset heatmapping style, e.g.:attribution
,:sensitivity
or:cam
extras
: optional named tuple that can be used by analyzers to return additional information.
For heatmapping functionality, take a look at either VisionHeatmaps.jl or TextHeatmaps.jl. Both provide heatmap
methods for visualizing explanations, either for images or text, respectively.
Analyzers
ExplainableAI.Gradient
— TypeGradient(model)
Analyze model by calculating the gradient of a neuron activation with respect to the input.
ExplainableAI.InputTimesGradient
— TypeInputTimesGradient(model)
Analyze model by calculating the gradient of a neuron activation with respect to the input. This gradient is then multiplied element-wise with the input.
ExplainableAI.SmoothGrad
— FunctionSmoothGrad(analyzer, [n=50, std=1.0f0, rng=GLOBAL_RNG])
SmoothGrad(analyzer, [n=50, distribution=Normal(0.0f0, 1.0f0), rng=GLOBAL_RNG])
Analyze model by calculating a smoothed sensitivity map. This is done by averaging sensitivity maps of a Gradient
analyzer over random samples in a neighborhood of the input, typically by adding Gaussian noise with mean 0.
References
- Smilkov et al., SmoothGrad: removing noise by adding noise
ExplainableAI.IntegratedGradients
— FunctionIntegratedGradients(analyzer, [n=50])
IntegratedGradients(analyzer, [n=50])
Analyze model by using the Integrated Gradients method.
References
- Sundararajan et al., Axiomatic Attribution for Deep Networks
ExplainableAI.GradCAM
— TypeGradCAM(feature_layers, adaptation_layers)
Calculates the Gradient-weighted Class Activation Map (GradCAM). GradCAM provides a visual explanation of the regions with significant neuron importance for the model's classification decision.
Parameters
feature_layers
: The layers of a convolutional neural network (CNN) responsible for extracting feature maps.adaptation_layers
: The layers of the CNN used for adaptation and classification.
Note
Flux is not required for GradCAM. GradCAM is compatible with a wide variety of CNN model-families.
References
- Selvaraju et al., Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization
Input augmentations
SmoothGrad
and IntegratedGradients
are special cases of the input augmentations NoiseAugmentation
and InterpolationAugmentation
, which can be applied as a wrapper to any analyzer:
ExplainableAI.NoiseAugmentation
— TypeNoiseAugmentation(analyzer, n)
NoiseAugmentation(analyzer, n, std::Real)
NoiseAugmentation(analyzer, n, distribution::Sampleable)
A wrapper around analyzers that augments the input with n
samples of additive noise sampled from a scalar distribution
. This input augmentation is then averaged to return an Explanation
.
Defaults to the normal distribution Normal(0, std^2)
with std=1.0f0
. For optimal results, Smilkov et al., SmoothGrad: removing noise by adding noise recommends setting std
between 10% and 20% of the input range of each sample, e.g. std = 0.1 * (maximum(input) - minimum(input))
.
Keyword arguments
rng::AbstractRNG
: Specify the random number generator that is used to sample noise from thedistribution
. Defaults toGLOBAL_RNG
.
ExplainableAI.InterpolationAugmentation
— TypeInterpolationAugmentation(model, [n=50])
A wrapper around analyzers that augments the input with n
steps of linear interpolation between the input and a reference input (typically zero(input)
). The gradients w.r.t. this augmented input are then averaged and multiplied with the difference between the input and the reference input.