# Image Explanations

Understanding how complex models make predictions is essential for promoting transparency, trust, and identifying potential biases. Model explainers offer insights into feature influence on outcomes, supporting debugging and model refinement.

We are going to demonstrate model explanations using [Alibi Explain's Anchor Images](https://docs.seldon.ai/seldon-enterprise-platform/product-tour/model-explanations) method, taking note of the segments in an input image that influenced the prediction the most, as well as, observing the Anchor's Precision and Coverage metrics.

In this demo we will:

* Launch an image classification deployment
* Send prediction requests to the deployment
* Create an explainer for the deployment
* Generate explanations for previously sent prediction requests

The model used in this demo was trained to classify images based on the [CIFAR10 dataset](https://www.cs.toronto.edu/~kriz/cifar.html).

## Create a Seldon Deployment

1. Click on `Create new deployment` button.
2. Enter the deployment details as follows:
   * Name: *cifar10-classifier*
   * Namespace: *seldon*
   * Type: *Seldon Deployment*

![Deployment details](https://1921172648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvLlcdnP8YnaIFsv8EiMA%2Fuploads%2Fgit-blob-8ca7a41d838e34c8883cf873af36589897c06bfa%2Fdeployment-details-core1.png?alt=media)

3. Configure the default predictor as follows:
   * Runtime: *Triton (ONNX, PyTorch, Tensorflow, TensorRT)*
   * Model Project: *default*
   * Model URI: *gs\://seldon-models/triton/tf\_cifar10*
   * Storage Secret: *(leave blank/none)*
   * Model Name: *cifar10*

{% hint style="warning" %}
The `Model Name` is linked to the name described in the `model-settings.json` file, located in the Google Cloud Storage location. Changing the name in the JSON file would also require changing the `Model Name`, and vice versa.
{% endhint %}

![Default predictor](https://1921172648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvLlcdnP8YnaIFsv8EiMA%2Fuploads%2Fgit-blob-2c36f745bd6bbf5509b2d0e0661e3496c7b764ab%2Fdefault-predictor-core1.png?alt=media)

4. Click the `Next` button until the end and, finally, click `Launch`.
5. If your deployment is launched successfully, it will have `Available` status.

## Get Predictions

We will make a prediction request using the image of a frog from the cifar10 dataset. The image is a JSON file in the REST format of the [Open Inference Protocol (OIP)](https://docs.seldon.io/projects/seldon-core/en/v2/contents/apis/inference/v2.html).

{% file src="<https://1921172648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvLlcdnP8YnaIFsv8EiMA%2Fuploads%2Fgit-blob-d9e459a2c892a934617b4d08a1fca77440401f66%2Fcifar10-frog-oip.json?alt=media>" %}

1. Click on the `cifar10-classifier` deployment created in the previous section to enter the deployment dashboard.
2. Inside the deployment dashboard, click on the `Predict` button.
3. On the `Predict` page, click on `Browse` to select and upload the previously downloaded prediction file.
4. Click the `Predict` button, the prediction request should be successful and the response should be shown.

![Successful prediction using a JSON file](https://1921172648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvLlcdnP8YnaIFsv8EiMA%2Fuploads%2Fgit-blob-bfb845a092bbeca4dc58f6edd754d413d3570fb3%2Fpredict-frog.png?alt=media)

## Add an Anchor Image Explainer

1. From the `cifar10-classifier` deployment dashboard, click `Add` inside the `Model Explanation` card.
2. For step 1 of the Explainer Configuration Wizard, select the `Image` model data type and click `Next`.

![Explainer Model Data Type](https://1921172648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvLlcdnP8YnaIFsv8EiMA%2Fuploads%2Fgit-blob-2c863d5b5d72fcbb34e8b26470f42b197c544ca1%2Fexplainer-model-data-type.png?alt=media)

3. For step 2, make sure `Anchor` is selected, then click `Next`.
4. For step 3, enter the following values in the `Explainer URI` tab:
   * Explainer URI: *gs\://seldon-models/tfserving/cifar10/cifar10\_anchor\_image\_py3.7\_alibi-0.7.0*
   * Model Project: *default*
   * Storage Secret: *(leave blank/none)*

![Explainer URI](https://1921172648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvLlcdnP8YnaIFsv8EiMA%2Fuploads%2Fgit-blob-293c608099b6786f0443b24488e8410433b6b423%2Fexplainer-model-uri.png?alt=media)

5. Click the `Next` button until the end, and finally, click `Launch`.

After a short while, the explainer should become available.

## Get an explanation for the request

Now that the explainer is available, we can make use of it to generate an explanation for the prediction request we made earlier.

1. Click on the `cifar10-classifier` deployment created in the previous sections to enter the deployment dashboard.
2. Navigate to the `Requests` page using the left navigation drawer, and you will see the request you made earlier.
3. Click on the `View explanation` button to generate an explanation for the request.

![Previously made prediction request with its predicted response](https://1921172648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvLlcdnP8YnaIFsv8EiMA%2Fuploads%2Fgit-blob-aefd336847f50e9034afadd2d1bd7ab847d1aa16%2Frequests-page-entry.png?alt=media)

4. After a few seconds, the explanation should be generated and displayed on the page.

![The frog image segments that influenced the prediction the most](https://1921172648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvLlcdnP8YnaIFsv8EiMA%2Fuploads%2Fgit-blob-48e247baa3e191ab803ccf19b96523fcb7bbcec4%2Fexplanation-part1.png?alt=media)

![The Precision and Coverage Anchor metrics](https://1921172648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvLlcdnP8YnaIFsv8EiMA%2Fuploads%2Fgit-blob-43f8ffaef90d4c2d1c858a352df96efae9554078%2Fexplanation-part2.png?alt=media)

![Perturbed samples of the prediction request which comply with the prediction](https://1921172648-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvLlcdnP8YnaIFsv8EiMA%2Fuploads%2Fgit-blob-8a9ba0b166b6bd56929922f14fc1206a73b644a9%2Fexplanation-part3.png?alt=media)

Congratulations, you've created an explanation for the request! 🥳

## Next Steps

Why not try our other [demos](https://docs.seldon.ai/seldon-enterprise-platform/demos)? Ready to dive in? Read our [operations guide](https://docs.seldon.ai/seldon-enterprise-platform/operations) to learn more about how to use Enterprise Platform.
