wingbeats.xai package¶
Submodules¶
wingbeats.xai.xai module¶
Library for Explainable AI
-
wingbeats.xai.xai.compute_gradients(model, X, target_ind=None)[source]¶ Compute the gradients of model output at index target_ind w.r.t. input(s) X. If target_ind not provided, the highest activation is considered.
- Parameters
model (tf.Model) – Model to compute gradients on.
X (list or array) – Signal or batch of signals.
target_ind (int, optional) – Class index w.r.t. which to compute the gradients. Defaults to None (highest activation).
- Returns
Gradients and also the prediction(s).
-
wingbeats.xai.xai.flip_pixels(model, image, target_ind, means, std_devs, percent_to_flip=0.25, percent_step=0.005, heatmap=None)[source]¶ Apply Pixel Flipping to an image of dimensions 1 x m x n x 3 of Tensor type. Percent_to_flip % of the pixels are chosen in percent_step % batches to be replaced with a randomly chosen value from the normal distribution with params. means and std_devs (one for every pixel). Parameter heatmap specifies the attention map type that is applied (saliency_map, guided_back_prop or None for random flipping). The model needs to be stripped of its softmax layer.
- Parameters
model (tf.Model) – Model to make predictions.
image (array or Tensor) – Input image to flip pixels on.
target_ind (int) – Class index w.r.t. which to compute the gradients.
means (array) – Matrix of mean values.
std_devs (array) – Matrix of standard deviations.
percent_to_flip (float) – Percent of pixels to flip in total. Defaults to 0.25.
percent_step (float) – Percentual increment of flipped pixels. Defaults to 0.005.
heatmap (array.) – Attention map according to which to flip the pixels. Defaults to None (random flipping).
- Returns
Prediction scores w.r.t. every species and every flipping step, indexes of the flipped pixels (as 1D array) and output image after flipping the pixels
-
wingbeats.xai.xai.guided_back_prop(analyzer, model, X, target_ind=None)[source]¶ Compute the GBP map(s) of model output(s) w.r.t. input(s) X.
- Parameters
analyzer (Class created by factory method from innvestigate.) – Gradient based analyzer, i.e. from innvestigate.innvestigate.analyzer import gradient_based as G; analyzer = G.GuidedBackprop(model).
model (tf.Model) – Model to compute gradients on.
X (list or array) – Signal or batch of signals.
target_ind (int, optional) – Class index w.r.t. which to compute the gradients. Defaults to None (highest activation).
- Returns
Heatmap(s) with the same dimensions as the input sample and the corresponding predictions.
-
wingbeats.xai.xai.integral_approximation(gradients)[source]¶ Approximate integral with the trapezoid rule.
- Parameters
gradients (array) – Discrete gradient values to integrate.
- Returns
Integral approximation
-
wingbeats.xai.xai.integrated_gradients(model, x, baseline, steps=50, target_ind=None)[source]¶ Compute attention map through the Integrated Gradients method.
- Parameters
model (tf.Model) – Model to compute gradients on.
x (list or array) – Signal or batch of signals.
baseline (array) – Starting image i.e. zero image for interpolation.
steps (int) – Number of discrete steps (= number of interpolated images). Defaults to 50.
target_ind (int, optional) – Class index w.r.t. which to compute the gradients. Defaults to None (highest activation).
-
wingbeats.xai.xai.interpolate_images(baseline, image, alphas)[source]¶ Compute a scale of images of increasing intensity between baseline and original image.
- Parameters
baseline (array) – Starting image i.e. zero image.
image (array) – Original (target) image.
alphas – Incremental intensity steps.
- Type
list
- Returns
Images of increasing intensities.
- Return type
array
-
wingbeats.xai.xai.quantile_maps(heatmaps, quantiles, nbins=50)[source]¶ Compute quantile maps out of the pixel distributions from a list of heatmaps.
- Parameters
heatmaps (list) – Distribution of heatmaps to compute statistics on.
quantiles (list) – Quantiles to be computed i.e. 0.25, 0.5, 0.75.
nbins (int) – Number of bins in histograms. Defaults to 50.
- Returns
Quantile heatmaps.
-
wingbeats.xai.xai.saliency_map(model, X, target_ind=None)[source]¶ Compute the saliency map(s) of model output(s) w.r.t. input(s) X.
- Parameters
model (tf.Model) – Model to compute gradients on.
X (list or array) – Signal or batch of signals.
target_ind (int, optional) – Class index w.r.t. which to compute the gradients. Defaults to None (highest activation).
- Returns
Heatmap(s) with the same dimensions as the input sample and the corresponding predictions.