ReX: Causal Responsibility Explanations for image classifiers

ReX is a causal explainability tool for image classifiers. ReX is black-box, that is, agnostic to the internal structure of the classifier. We assume that we can modify the inputs and send them to the classifier, observing the output. ReX provides sufficient, minimal single explanations, non-contiguous explanations (for partially obscured images), multiple explanations and contrastive explanations (sufficient, necessary and minimal).

ReX organisation

For more information and links to the papers, see the background page.

Installation

ReX can be installed using pip. We recommend creating a virtual environment to install ReX. ReX has been tested using versions of Python >= 3.10. The following instructions assume conda:

conda create -n rex python=3.13
conda activate rex
pip install rex_xai

This should install an executable rex in your path. To check that ReX is installed correctly, run:

ReX --help

To build from source, clone the repository and run:

git clone git@github.com:ReX-XAI/ReX.git
cd ReX
conda create -n rex python=3.13
conda activate rex
pip install .

Note:

By default, onnxruntime will be installed. If you wish to use a GPU, you should uninstall onnxruntime and install onnxruntime-gpu instead. You can alternatively clone the project and edit the pyproject.toml to read “onnxruntime-gpu >= 1.17.0” rather than “onnxruntime >= 1.17.0”.

If you want to use ReX with 3D data, you will need to install some optional extra dependencies:

pip install 'rex_xai[3D]'

Quickstart

ReX requires as input an image and a model. ReX natively understands onnx files. Train or download a model (e.g. Resnet50) and, from this directory, run:

ReX tests/test_data/dog.jpg --model resnet50-v1-7.onnx -vv --output dog_exp.jpg

To view an interactive plot for the responsibility map, run::

ReX tests/test_data/dog.jpg --model resnet50-v1-7.onnx -vv --surface

To save the extracted explanation to a file:

ReX tests/test_data/dog.jpg --model resnet50-v1-7.onnx --output dog_exp.jpg

ReX also works with PyTorch, but you will need to write some custom code to provide ReX with the prediction function and model shape, as well as preprocess the input data. See the sample scripts in tests/scripts/.

rex tests/test_data/dog.jpg --script tests/scripts/pytorch_resnet50.py -vv --output dog_exp.jpg

Other options:

# with spatial search rather than the default global search
ReX <path_to_image> --model <path_to_model> --strategy spatial

# to run multiple explanations
ReX <path_to_image> --model <path_to_model> --multi

# to view a responsibility landscape heatmap
ReX <path_to_image> --model <path_to_model>  --heatmap

# to save a responsibility landscape surface plot
ReX <path_to_image> --model <path_to_model>  --surface <path_and_extension>

ReX configuration is mainly handled via a config file; some options can also be set on the command line. ReX looks for the config file rex.toml in the current working directory and then $HOME/.config/rex.toml on unix-like systems.

If you want to use a custom location, use:

ReX <path_to_image> --model <path_to_model> --config <path_to_config>

An example config file is included in the repo as example.rex.toml. Rename this to rex.toml if you wish to use it.

Command line usage

Examples

Explanation

An explanation for a ladybird. This explanation was produced with 20 iterations, using the default masking colour (0). The minimal, sufficient explanation itself is pretty printed using the settings in [rex.visual] in rex.toml.

ladybird responsibility map minimal explanation

Setting raw = true in rex.toml produces the image which was actually classified by the model.

ladybird raw

Multiple Explanations

ReX tests/test_data/peacock.jpg --model resnet50-v1-7.onnx --strategy multi --output peacock.png

The number of explanations found depends on the model and some of the settings in rex.toml.

peacock peacock 1 peacock 2 peacock 3

Occluded Images

occluded bus

occluded_bus_rm

bus_explanation

Explanation Quality

ReX tests/test_data/ladybird.jpg --script tests/scripts/pytorch_resnet50.py --analyse

INFO:ReX:area 0.000399, entropy difference 6.751189, insertion curve 0.964960, deletion curve 0.046096