Skip to content

Commit bd88ec6

Browse files
Merge pull request #212838 from vadthyavath/vadthyavath/xai_vision_docs
Explainability intro section on generating explanations for predictions
2 parents 74ee920 + 80fe905 commit bd88ec6

File tree

3 files changed

+394
-11
lines changed

3 files changed

+394
-11
lines changed

articles/machine-learning/how-to-auto-train-image-models.md

Lines changed: 183 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,18 @@ The following is a sample JSONL file for image classification:
189189

190190
Once your data is in JSONL format, you can create training and validation `MLTable` as shown below.
191191

192-
<!-- :::code language="yaml" source="~/azureml-examples-main/sdk/python/jobs/automl-standalone-jobs/automl-image-object-detection-task-fridge-items/data/training-mltable-folder/MLTable"::: -->
192+
```yaml
193+
paths:
194+
- file: ./train_annotations.jsonl
195+
transformations:
196+
- read_json_lines:
197+
encoding: utf8
198+
invalid_lines: error
199+
include_path_column: false
200+
- convert_column_types:
201+
- columns: image_url
202+
column_type: stream_info
203+
```
193204

194205
Automated ML doesn't impose any constraints on training or validation data size for computer vision tasks. Maximum dataset size is only limited by the storage layer behind the dataset (i.e. blob store). There's no minimum number of images or labels. However, we recommend starting with a minimum of 10-15 samples per label to ensure the output model is sufficiently trained. The higher the total number of labels/classes, the more samples you need per label.
195206

@@ -810,6 +821,177 @@ If you want to use tiling, and want to control tiling behavior, the following pa
810821
### Test the deployment
811822
Please check this [Test the deployment](./tutorial-auto-train-image-models.md#test-the-deployment) section to test the deployment and visualize the detections from the model.
812823

824+
## Generate explanations for predictions
825+
826+
> [!IMPORTANT]
827+
> These settings are currently in public preview. They are provided without a service-level agreement. Certain features might not be supported or might have constrained capabilities. For more information, see [Supplemental Terms of Use for Microsoft Azure Previews](https://azure.microsoft.com/support/legal/preview-supplemental-terms/).
828+
829+
> [!WARNING]
830+
> **Model Explainability** is supported only for **multi-class classification** and **multi-label classification**.
831+
832+
Some of the advantages of using Explainable AI (XAI) with AutoML for images:
833+
- Improves the transparency in the complex vision model predictions
834+
- Helps the users to understand the important features/pixels in the input image that are contributing to the model predictions
835+
- Helps in troubleshooting the models
836+
- Helps in discovering the bias
837+
838+
### Explanations
839+
Explanations are **feature attributions** or weights given to each pixel in the input image based on its contribution to model's prediction. Each weight can be negative (negatively correlated with the prediction) or positive (positively correlated with the prediction). These attributions are calculated against the predicted class. For multi-class classification, exactly one attribution matrix of size `[3, valid_crop_size, valid_crop_size]` will be generated per sample, whereas for multi-label classification, attribution matrix of size `[3, valid_crop_size, valid_crop_size]` will be generated for each predicted label/class for each sample.
840+
841+
Using Explainable AI in AutoML for Images on the deployed endpoint, users can get **visualizations** of explanations (attributions overlaid on an input image) and/or **attributions** (multi-dimensional array of size `[3, valid_crop_size, valid_crop_size]`) for each image. Apart from visualizations, users can also get attribution matrices to gain more control over the explanations (like generating custom visualizations using attributions or scrutinizing segments of attributions). All the explanation algorithms will use cropped square images with size `valid_crop_size` for generating attributions.
842+
843+
844+
Explanations can be generated either from **online endpoint** or **batch endpoint**. Once the deployment is done, this endpoint can be utilized to generate the explanations for predictions. In case of online deployment, make sure to pass `request_settings = OnlineRequestSettings(request_timeout_ms=90000)` parameter to `ManagedOnlineDeployment` and set `request_timeout_ms` to its maximum value to avoid **timeout issues** while generating explanations (refer to [register and deploy model section](#register-and-deploy-model)). Some of the explainability (XAI) methods like `xrai` consume more time (specially for multi-label classification as we need to generate attributions and/or visualizations against each predicted label). So, we recommend any GPU instance for faster explanations. For more information on input and output schema for generating explanations, see the [schema docs](reference-automl-images-schema.md#data-format-for-online-scoring-and-explainability-xai).
845+
846+
847+
We support following state-of-the-art explainability algorithms in AutoML for images:
848+
- [XRAI](https://arxiv.org/abs/1906.02825) (xrai)
849+
- [Integrated Gradients](https://arxiv.org/abs/1703.01365) (integrated_gradients)
850+
- [Guided GradCAM](https://arxiv.org/abs/1610.02391v4) (guided_gradcam)
851+
- [Guided BackPropagation](https://arxiv.org/abs/1412.6806) (guided_backprop)
852+
853+
Following table describes the explainability algorithm specific tuning parameters for XRAI and Integrated Gradients. Guided backpropagation and guided gradcam don't require any tuning parameters.
854+
855+
| XAI algorithm | Algorithm specific parameters | Default Values |
856+
|--------- |------------- | --------- |
857+
| `xrai` | 1. `n_steps`: The number of steps used by the approximation method. Larger number of steps lead to better approximations of attributions (explanations). Range of n_steps is [2, inf), but the performance of attributions starts to converge after 50 steps. <br> `Optional, Int` <br><br> 2. `xrai_fast`: Whether to use faster version of XRAI. if `True`, then computation time for explanations is faster but leads to less accurate explanations (attributions) <br>`Optional, Bool` <br> | `n_steps = 50` <br> `xrai_fast = True` |
858+
| `integrated_gradients` | 1. `n_steps`: The number of steps used by the approximation method. Larger number of steps lead to better attributions (explanations). Range of n_steps is [2, inf), but the performance of attributions starts to converge after 50 steps.<br> `Optional, Int` <br><br> 2. `approximation_method`: Method for approximating the integral. Available approximation methods are `riemann_middle` and `gausslegendre`.<br> `Optional, String` | `n_steps = 50` <br> `approximation_method = riemann_middle` |
859+
860+
861+
Internally XRAI algorithm uses integrated gradients. So, `n_steps` parameter is required by both integrated gradients and XRAI algorithms. Larger number of steps consume more time for approximating the explanations and it may result in timeout issues on the online endpoint.
862+
863+
We recommend using XRAI > Guided GradCAM > Integrated Gradients > Guided BackPropagation algorithms for better explanations, whereas Guided BackPropagation > Guided GradCAM > Integrated Gradients > XRAI are recommended for faster explanations in the specified order.
864+
865+
A sample request to the online endpoint looks like the following. This request generates explanations when `model_explainability` is set to `True`. Following request will generate visualizations and attributions using faster version of XRAI algorithm with 50 steps.
866+
867+
```python
868+
import base64
869+
import json
870+
871+
def read_image(image_path):
872+
with open(image_path, "rb") as f:
873+
return f.read()
874+
875+
sample_image = "./test_image.jpg"
876+
877+
# Define explainability (XAI) parameters
878+
model_explainability = True
879+
xai_parameters = {"xai_algorithm": "xrai",
880+
"n_steps": 50,
881+
"xrai_fast": True,
882+
"visualizations": True,
883+
"attributions": True}
884+
885+
# Create request json
886+
request_json = {"input_data": {"columns": ["image"],
887+
"data": [json.dumps({"image_base64": base64.encodebytes(read_image(sample_image)).decode("utf-8"),
888+
"model_explainability": model_explainability,
889+
"xai_parameters": xai_parameters})],
890+
}
891+
}
892+
893+
request_file_name = "sample_request_data.json"
894+
895+
with open(request_file_name, "w") as request_file:
896+
json.dump(request_json, request_file)
897+
898+
resp = ml_client.online_endpoints.invoke(
899+
endpoint_name=online_endpoint_name,
900+
deployment_name=deployment.name,
901+
request_file=request_file_name,
902+
)
903+
predictions = json.loads(resp)
904+
```
905+
906+
For more information on generating explanations, see [GitHub notebook repository for automated machine learning samples](https://github.com/Azure/azureml-examples/tree/rvadthyavath/xai_vision_notebooks/sdk/python/jobs/automl-standalone-jobs).
907+
908+
### Interpreting Visualizations
909+
Deployed endpoint returns base64 encoded image string if both `model_explainability` and `visualizations` are set to `True`. Decode the base64 string as described in [notebooks](https://github.com/Azure/azureml-examples/tree/rvadthyavath/xai_vision_notebooks/sdk/python/jobs/automl-standalone-jobs) or use the following code to decode and visualize the base64 image strings in the prediction.
910+
911+
```python
912+
import base64
913+
from io import BytesIO
914+
from PIL import Image
915+
916+
def base64_to_img(base64_img_str):
917+
base64_img = base64_img_str.encode("utf-8")
918+
decoded_img = base64.b64decode(base64_img)
919+
return BytesIO(decoded_img).getvalue()
920+
921+
# For Multi-class classification:
922+
# Decode and visualize base64 image string for explanations for first input image
923+
# img_bytes = base64_to_img(predictions[0]["visualizations"])
924+
925+
# For Multi-label classification:
926+
# Decode and visualize base64 image string for explanations for first input image against one of the classes
927+
img_bytes = base64_to_img(predictions[0]["visualizations"][0])
928+
image = Image.open(BytesIO(img_bytes))
929+
```
930+
931+
Following picture describes the Visualization of explanations for a sample input image.
932+
![Screenshot of visualizations generated by XAI for AutoML for images.](./media/how-to-auto-train-image-models/xai-visualization.jpg)
933+
934+
Decoded base64 figure will have four image sections within a 2 x 2 grid.
935+
936+
- Image at Top-left corner (0, 0) is the cropped input image
937+
- Image at top-right corner (0, 1) is the heatmap of attributions on a color scale bgyw (blue green yellow white) where the contribution of white pixels on the predicted class is the highest and blue pixels is the lowest.
938+
- Image at bottom left corner (1, 0) is blended heatmap of attributions on cropped input image
939+
- Image at bottom right corner (1, 1) is the cropped input image with top 30 percent of the pixels based on attribution scores.
940+
941+
942+
### Interpreting Attributions
943+
Deployed endpoint returns attributions if both `model_explainability` and `attributions` are set to `True`. Fore more details, refer to [multi-class classification and multi-label classification notebooks](https://github.com/Azure/azureml-examples/tree/rvadthyavath/xai_vision_notebooks/sdk/python/jobs/automl-standalone-jobs).
944+
945+
These attributions give more control to the users to generate custom visualizations or to scrutinize pixel level attribution scores.
946+
Following code snippet describes a way to generate custom visualizations using attribution matrix. For more information on the schema of attributions for multi-class classification and multi-label classification, see the [schema docs](reference-automl-images-schema.md#data-format-for-online-scoring-and-explainability-xai).
947+
948+
Use the exact `valid_resize_size` and `valid_crop_size` values of the selected model to generate the explanations (default values are 256 and 224 respectively). Following code uses [Captum](https://captum.ai/) visualization functionality to generate custom visualizations. Users can utilize any other library to generate visualizations. For more details, please refer to the [captum visualization utilities](https://captum.ai/api/utilities.html#visualization).
949+
950+
```python
951+
import colorcet as cc
952+
import numpy as np
953+
from captum.attr import visualization as viz
954+
from PIL import Image
955+
from torchvision import transforms
956+
957+
def get_common_valid_transforms(resize_to=256, crop_size=224):
958+
959+
return transforms.Compose([
960+
transforms.Resize(resize_to),
961+
transforms.CenterCrop(crop_size)
962+
])
963+
964+
# Load the image
965+
valid_resize_size = 256
966+
valid_crop_size = 224
967+
sample_image = "./test_image.jpg"
968+
image = Image.open(sample_image)
969+
# Perform common validation transforms to get the image used to generate attributions
970+
common_transforms = get_common_valid_transforms(resize_to=valid_resize_size,
971+
crop_size=valid_crop_size)
972+
input_tensor = common_transforms(image)
973+
974+
# Convert output attributions to numpy array
975+
976+
# For Multi-class classification:
977+
# Selecting attribution matrix for first input image
978+
# attributions = np.array(predictions[0]["attributions"])
979+
980+
# For Multi-label classification:
981+
# Selecting first attribution matrix against one of the classes for first input image
982+
attributions = np.array(predictions[0]["attributions"][0])
983+
984+
# visualize results
985+
viz.visualize_image_attr_multiple(np.transpose(attributions, (1, 2, 0)),
986+
np.array(input_tensor),
987+
["original_image", "blended_heat_map"],
988+
["all", "absolute_value"],
989+
show_colorbar=True,
990+
cmap=cc.cm.bgyw,
991+
titles=["original_image", "heatmap"],
992+
fig_size=(12, 12))
993+
```
994+
813995
## Large datasets
814996

815997
If you're using AutoML to train on large datasets, there are some experimental settings that may be useful.
@@ -884,7 +1066,6 @@ image_object_detection_job.set_training_parameters(
8841066
Review detailed code examples and use cases in the [GitHub notebook repository for automated machine learning samples](https://github.com/Azure/azureml-examples/tree/main/sdk/python/jobs/automl-standalone-jobs). Please check the folders with 'automl-image-' prefix for samples specific to building computer vision models.
8851067

8861068

887-
8881069
## Code examples
8891070

8901071
# [Azure CLI](#tab/cli)
120 KB
Loading

0 commit comments

Comments
 (0)