Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Tutorial_SHAP_Images.ipynb #21

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions xai-for-image-data/Tutorial_SHAP_Images.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"source": [
"# Model-Agnostic Interpretation with SHAP for Image Analysis\n",
"\n",
"In this Notebook we will demonstrate how to use the SHapley Additive exPlanations (SHAP) method and interpret its results for image classification models.\n",
"In this Notebook, we will demonstrate how to use the SHapley Additive exPlanations (SHAP) method and interpret its results for image classification models.\n",
"\n",
"--------"
]
Expand All @@ -26,8 +26,8 @@
"\n",
"### Setup Colab environment\n",
"\n",
"If you installed the packages and requirements on your own machine, you can skip this section and start from the import section.\n",
"Otherwise, you can follow and execute the tutorial on your browser. In order to start working on the notebook, click on the following button, this will open this page in the Colab environment and you will be able to execute the code on your own.\n",
"If you installed the packages and requirements on your machine, you can skip this section and start from the import section.\n",
"Otherwise, you can follow and execute the tutorial on your browser. To start working on the notebook, click on the following button. This will open this page in the Colab environment, and you will be able to execute the code on your own.\n",
"\n",
"<a href=\"https://colab.research.google.com/github/HelmholtzAI-Consultants-Munich/Zero2Hero---Introduction-to-XAI/blob/Juelich-2023/xai-for-image-data/Tutorial_SHAP_Images.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n"
]
Expand All @@ -36,13 +36,13 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that you opened the notebook in Colab, follow the next step:\n",
"Now that you opened the notebook in Google Colab, follow the next step:\n",
"\n",
"1. Run this cell to connect your Google Drive to Colab and install packages\n",
"2. Allow this notebook to access your Google Drive files. Click on 'Yes', and select your account.\n",
"3. \"Google Drive for desktop wants to access your Google Account\". Click on 'Allow'.\n",
" \n",
"At this point, a folder has been created in your Drive and you can navigate it through the lefthand panel in Colab, you might also have received an email that informs you about the access on your Google Drive."
"At this point, a folder has been created in your Drive, and you can navigate it through the lefthand panel in Colab. You might also have received an email that informs you about the access on your Google Drive."
]
},
{
Expand Down Expand Up @@ -108,7 +108,7 @@
"source": [
"Let's train a simple 2D Convolutional Neural Network (CNN) for a small number of epochs on our training images.\n",
"\n",
"*Note: this is a very basic model to showcase how SHAP works for image classification. If you want to know more about it, check out the model in the data_and_model folder and other functions we used in the utils.*"
"*Note: this is a very basic model to showcase how SHAP works for image classification. To know more about it, check out the model in the data_and_model folder and other functions we used in the utils.*"
]
},
{
Expand Down Expand Up @@ -179,11 +179,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Now, what does my model actually think is important in the data?\n",
"## Now, what does my model think is important in the data?\n",
"\n",
"We prepared a small [Introduction to SHAP](https://xai-tutorials.readthedocs.io/en/latest/_model_agnostic_xai/shap.html) for you, to help you understand how this method works.\n",
"\n",
"Now let's use SHAP to get some insights into the image classification model. The `shap.Explainer()` class estimates the Shapley values to explain any machine learning model. Within this class, there are multiple explainers that can be used to explain which features are important in an image for classification. Some of them are:\n",
"Now, let's use SHAP to get insights into the image classification model. The `shap.Explainer()` class estimates the Shapley values to explain any machine learning model. Within this class, multiple explainers can be used to explain which features are important in an image for classification. Some of them are:\n",
"* [DeepExplainer](https://shap-lrjball.readthedocs.io/en/latest/generated/shap.DeepExplainer.html)\n",
"* [PartitionExplainer](https://shap-lrjball.readthedocs.io/en/latest/generated/shap.PartitionExplainer.html)\n",
"* [GradientExplainer](https://shap-lrjball.readthedocs.io/en/latest/generated/shap.GradientExplainer.html#shap.GradientExplainer)\n",
Expand All @@ -192,8 +192,8 @@
"\n",
"**GradientExplainer**\n",
"\n",
"Many models, including several neural networks, are gradient-based. This means that we can compute the gradient of the loss function with respect to the model input. When we can compute the gradient with respect to the input, we can use this information to calculate the Shapley values more efficiently. There’s a connection between the Gradient Estimator and a method called Integrated Gradients (Sundararajan et al. 2017). Integrated Gradients is a feature attribution method also based on gradients that outputs the integrated path of the gradient with respect to a reference point as an explanation. The difference between Integrated Gradients and SHAP values is that Integrated Gradients use a single reference point, while Shapley values utilize a background data set. \n",
"We won't go into the details here but we will rather focus on the interpretation of the results. As a reference. If you want to know more about how the SHAP class `shap.GradientExplainer()` approximates the Shapley values, we recommend reading the SHAP documentation and the book [Interpreting Machine Learning Models With SHAP](https://leanpub.com/shap) (Molnar 2022). \n"
"Many models, including several neural networks, are gradient-based. This means that we can compute the gradient of the loss function for the model input. When we can compute the gradient for the input, we can use this information to calculate the Shapley values more efficiently. There’s a connection between the Gradient Estimator and a method called Integrated Gradients (Sundararajan et al. 2017). Integrated Gradients is a feature attribution method also based on gradients that outputs the integrated path of the gradient for a reference point as an explanation. The difference between Integrated Gradients and SHAP values is that Integrated Gradients use a single reference point, while Shapley values utilize a background data set. \n",
"We won't go into the details here, but we rather focus on interpreting the results. To know more about how the SHAP class `shap.GradientExplainer()` approximates the Shapley values, we recommend reading the SHAP documentation and the book [Interpreting Machine Learning Models With SHAP](https://leanpub.com/shap) (Molnar 2022). \n"
]
},
{
Expand All @@ -202,7 +202,7 @@
"source": [
"### Step 1 - Create a baseline\n",
"\n",
"We need to select a part of our data set to create a baseline, i.e. the background dataset to use for integrating our features."
"We need to select a part of our data set to create a baseline, i.e., the background dataset to use for integrating our features."
]
},
{
Expand All @@ -223,7 +223,7 @@
"metadata": {},
"source": [
"### Step 2 - Instantiate the class\n",
"We decided to focus on GradientExplainer which is implemented in the `shap.GradientExplainer()`. \n",
"We decided to focus on GradientExplainer, which is implemented in the `shap.GradientExplainer()`. \n",
"The first argument is the model (or a tuple containing the model and the layer that should be explained, which will return Shapley values for the input of the layer argument). The second argument is the baseline built with the background dataset."
]
},
Expand Down