omnixai.explanations.image package
Pixel importance explanations for vision tasks. |
|
Image mask explanations for vision tasks. |
|
Contrastive explanations for vision tasks. |
|
Counterfactual explanations for vision tasks. |
omnixai.explanations.image.pixel_importance module
Pixel importance explanations for vision tasks.
- class omnixai.explanations.image.pixel_importance.PixelImportance(mode, explanations=None, use_heatmap=False)
Bases:
ExplanationBase
The class for pixel importance explanations. The pixel importance scores of the input instances are stored in a list. Each item in the list is a dict with the following format: {“image”: the input image, “scores”: the pixel importance scores}. If the task is classification, the dict has an additional entry {“target_label”: the predicted label of the input instance}.
- Parameters
mode – The task type, e.g., classification or regression.
explanations – The explanation results for initializing
PixelImportance
, which is optional.use_heatmap – True if plot heatmaps (for Grad-CAM methods) or False if plot raw values (for integrated gradients and SHAP).
- add(image, target_label, importance_scores, **kwargs)
Adds the generated explanation of one image.
- Parameters
image – The input image.
target_label – The label to be explained, which is ignored for regression.
importance_scores – The list of the corresponding pixel importance scores.
- get_explanations(index=None)
Gets the generated explanations.
- Parameters
index – The index of an explanation result stored in
PixelImportance
. Whenindex
is None, the function returns a list of all the explanations.- Returns
The explanation for one specific image (a dict) or the explanations for all the instances (a list of dicts). Each dict has the following format: {“image”: the input image, “scores”: the pixel importance scores}. If the task is classification, the dict has an additional entry {“target_label”: the predicted label of the input instance}.
- plot(index=None, class_names=None, max_num_figures=20, **kwargs)
Returns matplotlib figures plotting pixel importance scores.
- Parameters
index – The index of an explanation result stored in
PixelImportance
, e.g., it will plot the first explanation result whenindex = 0
. Whenindex
is None, it returns a figure plotting the pixel importance scores for the first 10 instances.class_names – A list of the class names indexed by the labels, e.g.,
class_name = ['dog', 'cat']
means that label 0 corresponds to ‘dog’ and label 1 corresponds to ‘cat’.max_num_figures – The maximum number of figures to plot.
- Returns
A list of matplotlib figures plotting pixel importance scores.
- plotly_plot(index=0, class_names=None, max_num_figures=20, **kwargs)
Returns a plotly dash figure plotting the pixel importance scores for one specific instance.
- Parameters
index – The index of the instance which cannot be None, e.g., it will plot the first explanation result when
index = 0
.class_names – A list of the class names indexed by the labels, e.g.,
class_name = ['dog', 'cat']
means that label 0 corresponds to ‘dog’ and label 1 corresponds to ‘cat’.max_num_figures – The maximum number of figures to plot.
- Returns
A plotly dash figure plotting the pixel importance scores.
- ipython_plot(index=0, class_names=None, max_num_figures=20, **kwargs)
Plots the pixel importance scores in IPython.
- Parameters
index – The index of the instance which cannot be None, e.g., it will plot the first explanation result when
index = 0
.class_names – A list of the class names indexed by the labels, e.g.,
class_name = ['dog', 'cat']
means that label 0 corresponds to ‘dog’ and label 1 corresponds to ‘cat’.max_num_figures – The maximum number of figures to plot.
- classmethod from_dict(d)
omnixai.explanations.image.mask module
Image mask explanations for vision tasks.
- class omnixai.explanations.image.mask.MaskExplanation
Bases:
ExplanationBase
The class for image mask explanations used by LIME. It uses a list to store image mask explanations of the input instances. Each item in the list is a dict with the following format: {“images”: the input images, “labels”: the predicted labels, “masks”: the masks returned by the explainer, e.g., LIME, “boundary”: the boundaries extracted from “masks”}.
- add(labels, images, masks)
Adds the generated explanation of one image.
- Parameters
labels – The predicted labels.
images – The input images.
masks – The mask explanation results.
- get_explanations(index=None)
Gets the generated explanations.
- Parameters
index – The index or name of the instance. When
index
is None, this method return all the explanations.- Returns
The explanation for one specific instance (a dict) or the explanations for all the instances (a list of dicts). Each dict has the following format: {“images”: the input images, “labels”: the predicted labels, “masks”: the masks returned by the explainer, e.g., LIME, “boundary”: the boundaries extracted from “masks”}.
- plot(index=None, class_names=None, **kwargs)
Returns a matplotlib figure showing the explanations.
- Parameters
index – The index of the instance, e.g., it will plot the first explanation result when
index = 0
. Whenindex
is None, it returns a figure showing the mask explanations for the first 10 instances.class_names – A list of the class names indexed by the labels, e.g.,
class_name = ['dog', 'cat']
means that label 0 corresponds to ‘dog’ and label 1 corresponds to ‘cat’.
- Returns
A matplotlib figure plotting mask explanations.
- plotly_plot(index=0, class_names=None, **kwargs)
Returns a plotly dash figure showing the explanations for one specific instance.
- Parameters
index – The index of the instance which cannot be None, e.g., it will plot the first explanation result when
index = 0
.class_names – A list of the class names indexed by the labels, e.g.,
class_name = ['dog', 'cat']
means that label 0 corresponds to ‘dog’ and label 1 corresponds to ‘cat’.
- Returns
A plotly dash figure plotting mask explanations.
- ipython_plot(index=0, class_names=None, **kwargs)
Plots mask explanations in IPython.
- Parameters
index – The index of the instance which cannot be None, e.g., it will plot the first explanation result when
index = 0
.class_names – A list of the class names indexed by the labels, e.g.,
class_name = ['dog', 'cat']
means that label 0 corresponds to ‘dog’ and label 1 corresponds to ‘cat’.
- classmethod from_dict(d)
omnixai.explanations.image.contrast module
Contrastive explanations for vision tasks.
- class omnixai.explanations.image.contrast.ContrastiveExplanation
Bases:
ExplanationBase
The class for image contrastive explanations. It uses a list to store the contrastive explanations of the input instances. Each item in the list is a dict with the following format: {“image”: the input image, “label”: the predicted label of the input, “pn”: the pertinent negative, “pn_label”: the predicted label of the pertinent negative, “pp”: the pertinent positive, “pp_label”: the predicted label of the pertinent positive}.
- add(image, label, pn, pn_label, pp, pp_label)
Adds the generated explanation of one image.
- Parameters
image – The input image.
label – The label of the input.
pn – The pertinent negative (PN).
pn_label – The label of the pertinent negative.
pp – The pertinent positive (PP).
pp_label – The label of the pertinent positive.
- get_explanations(index=None)
Gets the generated explanations.
- Parameters
index – The index of an explanation result stored in
ContrastiveExplanation
. Whenindex
is None, the function returns a list of all the explanations.- Returns
The explanation for one specific image (a dict) or the explanations for all the instances (a list of dicts). Each dict has the following format: {“image”: the input image, “label”: the predicted label of the input, “pn”: the pertinent negative, “pn_label”: the predicted label of the pertinent negative, “pp”: the pertinent positive, “pp_label”: the predicted label of the pertinent positive}.
- plot(index=None, class_names=None, **kwargs)
Returns a matplotlib figure showing the contrastive explanations.
- Parameters
index – The index of an explanation result stored in
ContrastiveExplanation
, e.g., it will plot the first explanation result whenindex = 0
. Whenindex
is None, it returns a figure showing the PNs and PPs for the first 5 instances.class_names – A list of the class names indexed by the labels, e.g.,
class_name = ['dog', 'cat']
means that label 0 corresponds to ‘dog’ and label 1 corresponds to ‘cat’.
- Returns
A matplotlib figure plotting PNs and PPs.
- plotly_plot(index=0, class_names=None, **kwargs)
Returns a plotly dash figure showing the contrastive explanations for one specific instance.
- Parameters
index – The index of an explanation result stored in
ContrastiveExplanation
which cannot be None, e.g., it will plot the first explanation result whenindex = 0
.class_names – A list of the class names indexed by the labels, e.g.,
class_name = ['dog', 'cat']
means that label 0 corresponds to ‘dog’ and label 1 corresponds to ‘cat’.
- Returns
A plotly dash figure plotting PNs and PPs.
- ipython_plot(index=0, class_names=None, **kwargs)
Plots PNs and PPs in IPython.
- Parameters
index – The index of an explanation result stored in
ContrastiveExplanation
which cannot be None, e.g., it will plot the first explanation result whenindex = 0
.class_names – A list of the class names indexed by the labels, e.g.,
class_name = ['dog', 'cat']
means that label 0 corresponds to ‘dog’ and label 1 corresponds to ‘cat’.
- classmethod from_dict(d)
omnixai.explanations.image.counterfactual module
Counterfactual explanations for vision tasks.
- class omnixai.explanations.image.counterfactual.CFExplanation
Bases:
ExplanationBase
The class for image counterfactual explanations. The counterfactual examples of the input instances are stored in a list. Each item in the list is a dict with the following format: {“image”: the input image, “label”: the predicted label of the input image, “cf”: the counterfactual example, “cf_label”: the predicted label of the counterfactual example}.
- add(image, label, cf, cf_label)
Adds the generated explanation of one image.
- Parameters
image – The input image.
label – The label of the input.
cf – The counterfactual image.
cf_label – The label of the counterfactual image.
- get_explanations(index=None)
Gets the generated explanations.
- Parameters
index – The index of an explanation result stored in
CounterfactualExplanation
. Whenindex
is None, the function returns a list of all the explanations.- Returns
The explanation for one specific image (a dict) or the explanations for all the instances (a list of dicts). Each dict has the following format: {“image”: the input image, “label”: the predicted label of the input image, “cf”: the counterfactual example, “cf_label”: the predicted label of the counterfactual example}.
- plot(index=None, class_names=None, **kwargs)
Returns a matplotlib figure showing the counterfactual explanations.
- Parameters
index – The index of an explanation result stored in
CounterfactualExplanation
, e.g., it will plot the first explanation result whenindex = 0
. Whenindex
is None, it returns a figure showing the PNs and PPs for the first 5 instances.class_names – A list of the class names indexed by the labels, e.g.,
class_name = ['dog', 'cat']
means that label 0 corresponds to ‘dog’ and label 1 corresponds to ‘cat’.
- Returns
A matplotlib figure plotting counterfactuals.
- plotly_plot(index=0, class_names=None, **kwargs)
Returns a plotly dash figure showing the counterfactual explanations for one specific instance.
- Parameters
index – The index of an explanation result stored in
CounterfactualExplanation
which cannot be None, e.g., it will plot the first explanation result whenindex = 0
.class_names – A list of the class names indexed by the labels, e.g.,
class_name = ['dog', 'cat']
means that label 0 corresponds to ‘dog’ and label 1 corresponds to ‘cat’.
- Returns
A plotly dash figure plotting counterfactual examples.
- ipython_plot(index=0, class_names=None, **kwargs)
Plots counterfactual examples in IPython.
- Parameters
index – The index of an explanation result stored in
CounterfactualExplanation
which cannot be None, e.g., it will plot the first explanation result whenindex = 0
.class_names – A list of the class names indexed by the labels, e.g.,
class_name = ['dog', 'cat']
means that label 0 corresponds to ‘dog’ and label 1 corresponds to ‘cat’.
- classmethod from_dict(d)