diff --git a/examples/vision/img/integrated_gradients/integrated_gradients_3_1.jpeg b/examples/vision/img/integrated_gradients/integrated_gradients_3_1.jpeg deleted file mode 100644 index 424de243fa..0000000000 Binary files a/examples/vision/img/integrated_gradients/integrated_gradients_3_1.jpeg and /dev/null differ diff --git a/examples/vision/img/integrated_gradients/integrated_gradients_9_1.png b/examples/vision/img/integrated_gradients/integrated_gradients_9_1.png deleted file mode 100644 index d9885ba951..0000000000 Binary files a/examples/vision/img/integrated_gradients/integrated_gradients_9_1.png and /dev/null differ diff --git a/examples/vision/img/integrated_gradients/integrated_gradients_9_2.png b/examples/vision/img/integrated_gradients/integrated_gradients_9_2.png deleted file mode 100644 index 200d3fa5d3..0000000000 Binary files a/examples/vision/img/integrated_gradients/integrated_gradients_9_2.png and /dev/null differ diff --git a/examples/vision/img/integrated_gradients/integrated_gradients_9_3.png b/examples/vision/img/integrated_gradients/integrated_gradients_9_3.png new file mode 100644 index 0000000000..da339ee38c Binary files /dev/null and b/examples/vision/img/integrated_gradients/integrated_gradients_9_3.png differ diff --git a/examples/vision/img/integrated_gradients/integrated_gradients_9_4.png b/examples/vision/img/integrated_gradients/integrated_gradients_9_4.png new file mode 100644 index 0000000000..e30b82e07d Binary files /dev/null and b/examples/vision/img/integrated_gradients/integrated_gradients_9_4.png differ diff --git a/examples/vision/integrated_gradients.py b/examples/vision/integrated_gradients.py index 1efe9f6da9..e7d012a8b4 100644 --- a/examples/vision/integrated_gradients.py +++ b/examples/vision/integrated_gradients.py @@ -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 """ """ @@ -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) @@ -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) @@ -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 ) @@ -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 diff --git a/examples/vision/ipynb/integrated_gradients.ipynb b/examples/vision/ipynb/integrated_gradients.ipynb index 38f43cd924..d1ce118c0b 100644 --- a/examples/vision/ipynb/integrated_gradients.ipynb +++ b/examples/vision/ipynb/integrated_gradients.ipynb @@ -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)" ] }, { @@ -65,7 +65,7 @@ "colab_type": "text" }, "source": [ - "## Setup\n" + "## Setup" ] }, { @@ -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", @@ -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))" ] }, { @@ -104,7 +105,7 @@ "colab_type": "text" }, "source": [ - "## Integrated Gradients algorithm\n" + "## Integrated Gradients algorithm" ] }, { @@ -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" + "" ] }, { @@ -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" ] }, { @@ -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", @@ -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", @@ -498,7 +502,7 @@ " ax[1].set_title(\"Normal gradients\")\n", " ax[2].set_title(\"Integrated gradients\")\n", " plt.show()\n", - "\n" + "" ] }, { @@ -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" ] }, { @@ -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", diff --git a/examples/vision/md/integrated_gradients.md b/examples/vision/md/integrated_gradients.md index 6febfa3392..b1496cc3b4 100644 --- a/examples/vision/md/integrated_gradients.md +++ b/examples/vision/md/integrated_gradients.md @@ -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) @@ -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)) - ``` -
-``` -Downloading data from https://i.imgur.com/Bvro0YD.png -4218880/4217496 [==============================] - 0s 0us/step -``` -
-![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)): @@ -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: @@ -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) @@ -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 ) @@ -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 @@ -519,17 +511,26 @@ vis.visualize( morphological_cleanup=True, outlines=True, ) - ```
``` + 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)] ```
-![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) \ No newline at end of file