Base Functions

XAIBase.jl interface for calling LIME and SHAP

JML_XAI_Project.LIMEType
(method::LIME)(input, output_selector::AbstractOutputSelector)

This function applies the LIME (Local Interpretable Model-agnostic Explanations) method to a given input and returns an explanation of the model's prediction.

Arguments

  • input: The input data for the model, typically a 4D array (e.g., image data with dimensions height x width x channels x batch size).
  • output_selector::AbstractOutputSelector: An object that selects specific outputs from the model's predictions.

Returns

  • Explanation: An object containing the following fields:
    • val: The explanation value generated by the explain_instance function.
    • output: The output of the model for the given input.
    • output_selection[1]: The selected output used for generating the explanation.
    • :MyMethod: The method used for generating the explanation.
    • :attribution: The type of explanation provided (attribution).
    • extras: Additional information related to the explanation (currently set to nothing).
source
JML_XAI_Project.LIMEMethod
(method::LIME)(input, output_selector::AbstractOutputSelector)

This function applies the LIME (Local Interpretable Model-agnostic Explanations) method to a given input and returns an explanation of the model's prediction.

Arguments

  • input: The input data for the model, typically a 4D array (e.g., image data with dimensions height x width x channels x batch size).
  • output_selector::AbstractOutputSelector: An object that selects specific outputs from the model's predictions.

Returns

  • Explanation: An object containing the following fields:
    • val: The explanation value generated by the explain_instance function.
    • output: The output of the model for the given input.
    • output_selection[1]: The selected output used for generating the explanation.
    • :MyMethod: The method used for generating the explanation.
    • :attribution: The type of explanation provided (attribution).
    • extras: Additional information related to the explanation (currently set to nothing).
source

Generation of a new interpretable data set

Conversion of the input image into mulitple disturbed interpretable representations

JML_XAI_Project.explain_instanceFunction
explain_instance(image, classifier_fn, output_selection, num_features=16, num_samples=64, batch_size=5, distance_metric="cosine",)

Generates explanations for a prediction.

  1. we generate neighborhood data by randomly perturbing features from the instance.
  2. We then learn locally weighted linear models on this neighborhood data to explain each of the classes in an interpretable way.

Parameters

  • image: 4 dimension RGB image.
  • classifier_fn: classifier prediction probability function, which takes a image Matrix and outputs prediction probabilities.
  • labels: iterable with labels to be explained.
  • num_features: maximum number of features present in explanation, default = 16.
  • num_samples: size of the neighborhood (perturbed images) to learn the linear model, default = 64.
  • batch_size: batch size for model predictions, default = 8.
  • distance_metric: the distance metric to use for weights, default = 'cosine'

Returns:

  • An ImageExplanation object with the corresponding explanations.
source
JML_XAI_Project.default_segmentation_functionFunction
function default_segmentation_function(algo_type::String)

Return image segmantation function, if no function was passed originally. Based on Scikit-Image implementation.

felzenszwalb:

  • further explainations: https://www.analyticsvidhya.com/blog/2021/05/image-segmentation-with-felzenszwalbs-algorithm/

Parameters

  • algo_type: string, segmentation algorithm among the following: "felzenszwalb"

Returns

  • segmentation_func::Function: A segmantation function.
source
JML_XAI_Project.cosine_similiarityFunction
cosine_similarity(A::AbstractArray, B::AbstractArray)

Computes the cosine similarity between corresponding rows of two arrays.

Parameters

  • A::AbstractArray: The first input array. Each row represents a vector.
  • B::AbstractArray: The second input array. Each row represents a vector. Must have the same number of columns as A.

Returns

  • AbstractArray: An array of cosine similarities between corresponding rows of A and B.
source
JML_XAI_Project.pairwise_distanceFunction
pairwise_distance(A::AbstractArray, B::AbstractArray; method="cosine")

Computes the pairwise distance between corresponding rows of two arrays using the specified distance metric.

Parameters

  • A::AbstractArray: The first input array. Each row represents a vector.
  • B::AbstractArray: The second input array. Each row represents a vector. Must have the same number of columns as A.
  • method::String="cosine": The distance metric to use. Options are "cosine" for cosine similarity or "euclidean" for Euclidean distance. Defaults to "cosine".

Returns

  • AbstractArray: An array of distances between corresponding rows of A and B.
source
JML_XAI_Project.data_labelsFunction
function data_labels(image::Matrix{RGB{<:AbstractFloat}}, fudged_image::Matrix{RGB{<:AbstractFloat}}, segments::Matrix{<:Integer}, classifier_fn::Function, num_samples<:Integer, batch_size<:Integer=10)

Generates perturbed versions of a given image by turning superpixels on or off,using a specified segmentation map. It then predicts the class probabilities for these perturbed images using a provided classifier function. The function returns a tuple containing the binary matrix of perturbed images (data) and their corresponding prediction probabilities (labels). This is useful for techniques like LIME to understand and explain model predictions locally.

Parameters

  • image::Matrix{RGB{Float32}}: The original input image.
  • fudged_image::Matrix{RGB{Float32}}: The fudged image with mean colors for segments.
  • segments::Matrix{Int}: The segmentation map where each segment is labeled with an integer.
  • classifier_fn::Function: A function that takes a batch of images and returns their predicted labels.
  • num_samples::Int: The number of samples to generate.
  • batch_size::Int=10: The size of the batches to process through the classifier function. Defaults to 10.

Returns

  • data::Matrix{Int}: A binary matrix where each row indicates which features (segments) are active.
  • labels::Matrix{Float64}: A matrix of classifier predictions for the corresponding images in data.
source
JML_XAI_Project.euclidian_distanceFunction
function euclidian_distance(A,B)

calculates the euclidian distance between each column vector in input matrix A and the column vectors in input matrix B

Parameters:

  • A: matrix (m,n)
  • B: matrix (m,n) or (1,n)

Returns:

  • distance: 1-d array of distances
source
JML_XAI_Project.exponential_kernelFunction
exponential_kernel(d::Vector{AbstractFloat}; kernel_width=0.25)

Computes the exponential kernel for a given array of distances.

Parameters

  • d::Vector{AbstractFloat}: An array of distances.
  • kernel_width::AbstractFloat=0.25: The width of the kernel. Defaults to 0.25.

Returns

  • Vector{AbstractFloat}: An array of kernel values computed from the input distances.
source
JML_XAI_Project.create_fudged_imageFunction
create_fudged_image(img::Matrix{RGB{Float32}}, seg_map::Matrix{<:Integer})

Creates a "fudged" image where the pixels of each segment are replaced by the mean color of that segment.

Parameters

  • img::Matrix{RGB{Float32}}: The input image as a matrix of RGB colors with floating point values.
  • seg_map::Matrix{<:Integer}: The segmentation map containing the segment labels for each pixel.

Returns

  • Matrix{RGB{Float32}}: A new image where the pixels of each segment are replaced by the mean color of that segment.
source
create_fudged_image(img::Matrix{Float32}, seg_map::Matrix{<:Integer})

Creates a "fudged" image where the pixels of each segment are replaced by the mean color of that segment.

Parameters

  • img::Matrix{Float32}: The input image as a matrix of RGB colors with floating point values.
  • seg_map::Matrix{<:Integer}: The segmentation map containing the segment labels for each pixel.

Returns

  • Matrix{RGB{Float32}}: A new image where the pixels of each segment are replaced by the mean color of that segment.
source

Feature selection and train simplified model on the interpretable data set

JML_XAI_Project.explain_instance_with_dataFunction
function explain_instance_with_data(neighborhood_data, neighborhood_labels, distances, label, num_features, kernel_fn = (x) -> 1 .- x)

Takes perturbed data, labels and distances, returns explanation. Generates a relevance score for each feature based on the local approximition using the neighborhood_data. A relevance score close to zero means that the feature is not relevant. A larger, positive value means that the feature contributes positively to the specific label, and negative value means that the feature decreases the chance for the classification of the specific label.

Parameters

  • neighborhood_data: perturbed data
  • neighborhood_labels: corresponding perturbed labels. should have as many columns as the number of possible labels.
  • distances: distances to original data point.
  • kernel_fn: (similiarity) kernel function that transforms an array of distances into an array of proximity values (floats)
  • label: label for which we want an explanation
  • num_features: maximum number of features in explanation
  • model_regressor: sklearn regressor to use in explanation. Defaults to Ridge regression if None. Must have modelregressor.coef and 'sampleweight' as a parameter to modelregressor.fit()

Returns

  • Vector{AbstractFloat}: relevance score for each feature
source
JML_XAI_Project.weighted_dataFunction
normalize_data(X::Matrix, y::Vector, weights::Vector)

Returns the weight normalization of features and labels using a weight vector.

Parameters

  • X::Matrix{<:Real}: features
  • y::Vector{<:Real}: labels
  • weights::Vector{<:Real}: weight vector

Returns

  • Normalization of features and labels using a weight vector.
source
JML_XAI_Project.train_ridge_regressorFunction
train_ridge_regressor(X::Matrix{<:Real}, y::Vector{<:Real}; lam::Real, weights::Vector{Real})

Returns the trained simplified linear model as a matrix using ridge regression:

Parameters

  • X::Matrix{<:Real}: Simplified features
  • y::Vector{<:Real}: Corresponding labels

Returns

  • Vector{Float64}: Simplified linear model
source
JML_XAI_Project.feature_selectionFunction
feature_selection(X::Matrix, y::Vector, max_feat::Int) -> Vector

Selects features for the model using LARS with Lasso [https://tibshirani.su.domains/ftp/lars.pdf] s.t. len(selectedfeatures) <= maxfeat Uses the LARS package: https://github.com/simonster/LARS.jl, which needs to be installed manually via: [package manager] add https://github.com/e-strauss/LARS.jl

Parameters

  • X: weighted features
  • y: weighted labels
  • max_feat: maximum number of feature that can be selected

Returns

  • indices of selected features
source

Shap

JML_XAI_Project.agnostic_kernelFunction
agnostic_kernel(simpleFeatures::Matrix{<:Real})

Calculates the weights used for building the ridge regression model (simplified model), when applying SHAP with a model-agnostic kernel. Implentation based on "A Unified Approach to Interpreting Model Predictions" (https://arxiv.org/abs/1705.07874)

Parameters

  • simpleFeatures::Matrix{<:Real}: The simplified features used for building the simlified model.

Returns

  • Vector{Float64}: A vector of weights with one weight for each simplified sample.
source

Index