Skip to content

Commit

Permalink
Update integrated gradients example for Keras 3. (keras-team#1587)
Browse files Browse the repository at this point in the history
  • Loading branch information
hertschuh authored Nov 9, 2023
1 parent 331e91b commit 26caa15
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 50 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 11 additions & 8 deletions examples/vision/integrated_gradients.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Date created: 2020/06/02
Last modified: 2020/06/02
Description: How to obtain integrated gradients for a classification model.
Accelerator: NONE
Accelerator: None
"""

"""
Expand Down Expand Up @@ -55,12 +55,13 @@
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage
from IPython.display import Image
from IPython.display import Image, display

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.applications import xception
import keras
from keras import layers
from keras.applications import xception


# Size of the input image
img_size = (299, 299, 3)
Expand Down Expand Up @@ -284,7 +285,10 @@ def morphological_cleanup_fn(self, attributions, structure=np.ones((4, 4))):
return opened

def draw_outlines(
self, attributions, percentage=90, connected_component_structure=np.ones((3, 3))
self,
attributions,
percentage=90,
connected_component_structure=np.ones((3, 3)),
):
# 1. Binarize the attributions.
attributions = self.binarize(attributions)
Expand All @@ -293,7 +297,7 @@ def draw_outlines(
attributions = ndimage.binary_fill_holes(attributions)

# 3. Compute connected components
connected_components, num_comp = ndimage.measurements.label(
connected_components, num_comp = ndimage.label(
attributions, structure=connected_component_structure
)

Expand Down Expand Up @@ -449,7 +453,6 @@ def visualize(

"""
## Let's test-drive it
You can try the model on [Hugging Face Spaces](https://huggingface.co/spaces/keras-io/integrated_gradients).
"""

# 1. Convert the image to numpy array
Expand Down
38 changes: 20 additions & 18 deletions examples/vision/ipynb/integrated_gradients.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"**References:**\n",
"\n",
"- Integrated Gradients original [paper](https://arxiv.org/abs/1703.01365)\n",
"- [Original implementation](https://github.com/ankurtaly/Integrated-Gradients)\n"
"- [Original implementation](https://github.com/ankurtaly/Integrated-Gradients)"
]
},
{
Expand All @@ -65,7 +65,7 @@
"colab_type": "text"
},
"source": [
"## Setup\n"
"## Setup"
]
},
{
Expand All @@ -80,12 +80,13 @@
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from scipy import ndimage\n",
"from IPython.display import Image\n",
"from IPython.display import Image, display\n",
"\n",
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"from tensorflow.keras import layers\n",
"from tensorflow.keras.applications import xception\n",
"import keras\n",
"from keras import layers\n",
"from keras.applications import xception\n",
"\n",
"\n",
"# Size of the input image\n",
"img_size = (299, 299, 3)\n",
Expand All @@ -95,7 +96,7 @@
"\n",
"# The local path to our target image\n",
"img_path = keras.utils.get_file(\"elephant.jpg\", \"https://i.imgur.com/Bvro0YD.png\")\n",
"display(Image(img_path))\n"
"display(Image(img_path))"
]
},
{
Expand All @@ -104,7 +105,7 @@
"colab_type": "text"
},
"source": [
"## Integrated Gradients algorithm\n"
"## Integrated Gradients algorithm"
]
},
{
Expand Down Expand Up @@ -232,7 +233,7 @@
" # 3. Return the average integrated gradients for the image\n",
" integrated_grads = tf.convert_to_tensor(integrated_grads)\n",
" return tf.reduce_mean(integrated_grads, axis=0)\n",
"\n"
""
]
},
{
Expand All @@ -241,7 +242,7 @@
"colab_type": "text"
},
"source": [
"## Helper class for visualizing gradients and integrated gradients\n"
"## Helper class for visualizing gradients and integrated gradients"
]
},
{
Expand Down Expand Up @@ -337,7 +338,10 @@
" return opened\n",
"\n",
" def draw_outlines(\n",
" self, attributions, percentage=90, connected_component_structure=np.ones((3, 3))\n",
" self,\n",
" attributions,\n",
" percentage=90,\n",
" connected_component_structure=np.ones((3, 3)),\n",
" ):\n",
" # 1. Binarize the attributions.\n",
" attributions = self.binarize(attributions)\n",
Expand All @@ -346,7 +350,7 @@
" attributions = ndimage.binary_fill_holes(attributions)\n",
"\n",
" # 3. Compute connected components\n",
" connected_components, num_comp = ndimage.measurements.label(\n",
" connected_components, num_comp = ndimage.label(\n",
" attributions, structure=connected_component_structure\n",
" )\n",
"\n",
Expand Down Expand Up @@ -498,7 +502,7 @@
" ax[1].set_title(\"Normal gradients\")\n",
" ax[2].set_title(\"Integrated gradients\")\n",
" plt.show()\n",
"\n"
""
]
},
{
Expand All @@ -507,9 +511,7 @@
"colab_type": "text"
},
"source": [
"## Let's test-drive it\n",
"\n",
"You can try the model on [Hugging Face Spaces](https://huggingface.co/spaces/keras-io/integrated_gradients)."
"## Let's test-drive it"
]
},
{
Expand Down Expand Up @@ -560,12 +562,12 @@
" clip_below_percentile=28,\n",
" morphological_cleanup=True,\n",
" outlines=True,\n",
")\n"
")"
]
}
],
"metadata": {
"accelerator": "GPU",
"accelerator": "None",
"colab": {
"collapsed_sections": [],
"name": "integrated_gradients",
Expand Down
49 changes: 25 additions & 24 deletions examples/vision/md/integrated_gradients.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,22 @@ consider reading this excellent
- Integrated Gradients original [paper](https://arxiv.org/abs/1703.01365)
- [Original implementation](https://github.com/ankurtaly/Integrated-Gradients)


---
## Setup



```python

import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage
from IPython.display import Image
from IPython.display import Image, display

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.applications import xception
import keras
from keras import layers
from keras.applications import xception


# Size of the input image
img_size = (299, 299, 3)
Expand All @@ -76,24 +75,18 @@ model = xception.Xception(weights="imagenet")
# The local path to our target image
img_path = keras.utils.get_file("elephant.jpg", "https://i.imgur.com/Bvro0YD.png")
display(Image(img_path))

```

<div class="k-default-codeblock">
```
Downloading data from https://i.imgur.com/Bvro0YD.png
4218880/4217496 [==============================] - 0s 0us/step

```
</div>
![jpeg](/img/examples/vision/integrated_gradients/integrated_gradients_3_1.jpeg)
![jpeg](/img/examples/vision/integrated_gradients/integrated_gradients_3_0.jpg)


---
## Integrated Gradients algorithm



```python

def get_img_array(img_path, size=(299, 299)):
Expand Down Expand Up @@ -213,14 +206,12 @@ def random_baseline_integrated_gradients(
integrated_grads = tf.convert_to_tensor(integrated_grads)
return tf.reduce_mean(integrated_grads, axis=0)


```

---
## Helper class for visualizing gradients and integrated gradients



```python

class GradVisualizer:
Expand Down Expand Up @@ -307,7 +298,10 @@ class GradVisualizer:
return opened

def draw_outlines(
self, attributions, percentage=90, connected_component_structure=np.ones((3, 3))
self,
attributions,
percentage=90,
connected_component_structure=np.ones((3, 3)),
):
# 1. Binarize the attributions.
attributions = self.binarize(attributions)
Expand All @@ -316,7 +310,7 @@ class GradVisualizer:
attributions = ndimage.binary_fill_holes(attributions)

# 3. Compute connected components
connected_components, num_comp = ndimage.measurements.label(
connected_components, num_comp = ndimage.label(
attributions, structure=connected_component_structure
)

Expand Down Expand Up @@ -469,12 +463,10 @@ class GradVisualizer:
ax[2].set_title("Integrated gradients")
plt.show()


```

---
## Let's test-drive it
You can try the model on [Hugging Face Spaces](https://huggingface.co/spaces/keras-io/integrated_gradients).


```python
Expand Down Expand Up @@ -519,17 +511,26 @@ vis.visualize(
morphological_cleanup=True,
outlines=True,
)

```

<div class="k-default-codeblock">
```
1/1 ━━━━━━━━━━━━━━━━━━━━ 5s 5s/step
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1699486705.534012 86541 device_compiler.h:187] Compiled cluster using XLA! This line is logged at most once for the lifetime of the process.
Predicted: tf.Tensor(386, shape=(), dtype=int64) [('n02504458', 'African_elephant', 0.8871446)]
```
</div>
![png](/img/examples/vision/integrated_gradients/integrated_gradients_9_1.png)

![png](/img/examples/vision/integrated_gradients/integrated_gradients_9_3.png)





![png](/img/examples/vision/integrated_gradients/integrated_gradients_9_4.png)


![png](/img/examples/vision/integrated_gradients/integrated_gradients_9_2.png)

0 comments on commit 26caa15

Please sign in to comment.