Base Functions
XAIBase.jl interface for calling LIME and SHAP
JML_XAI_Project.LIME — Type(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 theexplain_instancefunction.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 tonothing).
JML_XAI_Project.LIME — Method(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 theexplain_instancefunction.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 tonothing).
Generation of a new interpretable data set
Conversion of the input image into mulitple disturbed interpretable representations
JML_XAI_Project.explain_instance — Functionexplain_instance(image, classifier_fn, output_selection, num_features=16, num_samples=64, batch_size=5, distance_metric="cosine",)Generates explanations for a prediction.
- we generate neighborhood data by randomly perturbing features from the instance.
- 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.
JML_XAI_Project.default_segmentation_function — Functionfunction 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.
JML_XAI_Project.cosine_similiarity — Functioncosine_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 asA.
Returns
AbstractArray: An array of cosine similarities between corresponding rows ofAandB.
JML_XAI_Project.cosine_distance — Functioncosine_similarity(A::AbstractArray, B::AbstractArray)Computes the cosine distance: 1 - cosine_similarity(A,B)
JML_XAI_Project.pairwise_distance — Functionpairwise_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 asA.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 ofAandB.
JML_XAI_Project.data_labels — Functionfunction 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 indata.
JML_XAI_Project.euclidian_distance — Functionfunction 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
JML_XAI_Project.exponential_kernel — Functionexponential_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.
JML_XAI_Project.create_fudged_image — Functioncreate_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.
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.
Feature selection and train simplified model on the interpretable data set
JML_XAI_Project.explain_instance_with_data — Functionfunction 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 dataneighborhood_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 explanationnum_features: maximum number of features in explanationmodel_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
JML_XAI_Project.weighted_data — Functionnormalize_data(X::Matrix, y::Vector, weights::Vector)Returns the weight normalization of features and labels using a weight vector.
Parameters
X::Matrix{<:Real}: featuresy::Vector{<:Real}: labelsweights::Vector{<:Real}: weight vector
Returns
- Normalization of features and labels using a weight vector.
JML_XAI_Project.train_ridge_regressor — Functiontrain_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 featuresy::Vector{<:Real}: Corresponding labels
Returns
Vector{Float64}: Simplified linear model
JML_XAI_Project.feature_selection — Functionfeature_selection(X::Matrix, y::Vector, max_feat::Int) -> VectorSelects 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 featuresy: weighted labelsmax_feat: maximum number of feature that can be selected
Returns
- indices of selected features
Shap
JML_XAI_Project.agnostic_kernel — Functionagnostic_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.
Index
JML_XAI_Project.LIMEJML_XAI_Project.LIMEJML_XAI_Project.agnostic_kernelJML_XAI_Project.cosine_distanceJML_XAI_Project.cosine_similiarityJML_XAI_Project.create_fudged_imageJML_XAI_Project.data_labelsJML_XAI_Project.default_segmentation_functionJML_XAI_Project.euclidian_distanceJML_XAI_Project.explain_instanceJML_XAI_Project.explain_instance_with_dataJML_XAI_Project.exponential_kernelJML_XAI_Project.feature_selectionJML_XAI_Project.pairwise_distanceJML_XAI_Project.train_ridge_regressorJML_XAI_Project.weighted_data