Basic API

All methods in ExplainableAI.jl work by calling analyze on an input and an analyzer:

XAIBase.analyze — Function
analyze(input, method)
analyze(input, method, output_selection)

Apply the analyzer method for the given input, returning an Attribution. If output_selection is specified, the attribution will be calculated for that output. Otherwise, the output with the highest activation is automatically chosen.

See also Attribution.

source
XAIBase.Attribution — Type
Attribution(val, input, output, output_selection, pooling; extras)

Return type of feature-attribution methods when calling analyze.

Fields

  • val: numerical output of the analyzer, e.g. an attribution or gradient
  • input: input for the given analyzer
  • output: model output for the given analyzer input
  • output_selection: index of the output the attribution was computed for
  • pooling: an AbstractPooling function that reduces val over its feature dimension when projecting the attribution onto a human-interpretable space, e.g. for visualization. Downstream packages such as VisionHeatmaps.jl call it as pooling(val, dims), choosing the reduced dimension dims themselves. Defaults to NormPooling.
  • extras: optional named tuple that can be used by analyzers to return additional information. Keyword argument, defaults to nothing.

pooling is an optional positional argument, defaulting to NormPooling.

source

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 — Type
Gradient(model)

Analyze model by calculating the gradient of a neuron activation with respect to the input.

source
ExplainableAI.InputTimesGradient — Type
InputTimesGradient(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.

source
ExplainableAI.SmoothGrad — Type
SmoothGrad(model)
SmoothGrad(model, [n, std, rng])
SmoothGrad(model, [n, distribution, rng])

Analyze model by calculating a smoothed sensitivity map. This is done by averaging the gradient over n random samples in a neighborhood of the input. Defaults to 50 samples from the normal distribution with zero mean and 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

  • backend::AbstractADType: AD backend used to compute gradients. Defaults to ADTypes.AutoZygote().

References

  • Smilkov et al., SmoothGrad: removing noise by adding noise
source
ExplainableAI.IntegratedGradients — Type
IntegratedGradients(model, [n=50])

Analyze model by using the Integrated Gradients method.

Keyword arguments

  • backend::AbstractADType: AD backend used to compute gradients. Defaults to ADTypes.AutoZygote().

References

  • Sundararajan et al., Axiomatic Attribution for Deep Networks
source
ExplainableAI.GradCAM — Type
GradCAM(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
source

All gradient-based analyzers use AD backends from ADTypes.jl via DifferentiationInterface.jl, which can be selected on construction and queried via backend:

ExplainableAI.backend — Function
backend(analyzer)

Return the automatic differentiation backend used by a gradient-based analyzer.

For analyzers that wrap another analyzer, such as NoiseAugmentation and InterpolationAugmentation, the backend of the wrapped analyzer is returned.

Example

julia> analyzer = SmoothGrad(model; backend = AutoEnzyme());

julia> backend(analyzer)
AutoEnzyme()
source

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 — Type
NoiseAugmentation(analyzer, n, [std::Real, rng]; pooling)
NoiseAugmentation(analyzer, n, [distribution::Sampleable, rng]; pooling)

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 Attribution. Defaults to the normal distribution with zero mean and 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

  • pooling::AbstractPooling: Pooling of the returned Attribution, e.g. NormPooling(). Required, since the right pooling depends on the wrapped analyzer.
  • rng::AbstractRNG: Specify the random number generator that is used to sample noise from the distribution. Defaults to GLOBAL_RNG.
  • show_progress:Bool: Show progress meter while sampling augmentations. Defaults to true.
source
ExplainableAI.InterpolationAugmentation — Type
InterpolationAugmentation(analyzer, n; pooling)

A wrapper around analyzers that augments the input with n points of linear interpolation between a reference input (typically zero(input)) and the input, both endpoints included. The attributions of these augmented inputs are integrated over the path using the trapezoidal rule, and multiplied with the difference between the input and the reference input.

The reference input can be set via the keyword argument input_ref of analyze.

Keyword arguments

  • pooling::AbstractPooling: Pooling of the returned Attribution, e.g. SumPooling(). Required, since the right pooling depends on the wrapped analyzer.
source

Index