Skip to content

Commit

Permalink
Add grad cam example
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdangerw committed Nov 10, 2023
1 parent ea7f24e commit d4bf745
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 15 deletions.
8 changes: 6 additions & 2 deletions examples/vision/grad_cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
Date created: 2020/04/26
Last modified: 2021/03/07
Description: How to obtain a class activation heatmap for an image classification model.
Accelerator: NONE
Accelerator: GPU
"""
"""
Adapted from Deep Learning with Python (2017).
## Setup
"""

import os

os.environ["KERAS_BACKEND"] = "tensorflow"

import numpy as np
import tensorflow as tf
from tensorflow import keras
import keras

# Display
from IPython.display import Image, display
Expand Down
Binary file modified examples/vision/img/grad_cam/grad_cam_10_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/vision/img/grad_cam/grad_cam_12_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/vision/img/grad_cam/grad_cam_14_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/vision/img/grad_cam/grad_cam_4_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/vision/img/grad_cam/grad_cam_8_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions examples/vision/ipynb/grad_cam.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@
},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"KERAS_BACKEND\"] = \"tensorflow\"\n",
"\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"import keras\n",
"\n",
"# Display\n",
"from IPython.display import Image, display\n",
Expand Down Expand Up @@ -319,7 +323,7 @@
}
],
"metadata": {
"accelerator": "NONE",
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"name": "grad_cam",
Expand Down
38 changes: 27 additions & 11 deletions examples/vision/md/grad_cam.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ Adapted from Deep Learning with Python (2017).


```python
import os

os.environ["KERAS_BACKEND"] = "tensorflow"

import numpy as np
import tensorflow as tf
from tensorflow import keras
import keras

# Display
from IPython.display import Image, display
Expand Down Expand Up @@ -54,9 +58,15 @@ display(Image(img_path))

```

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

![jpeg](/img/examples/vision/grad_cam/grad_cam_4_0.jpg)
![jpeg](/img/examples/vision/grad_cam/grad_cam_4_1.jpg)



Expand Down Expand Up @@ -142,13 +152,14 @@ plt.show()

<div class="k-default-codeblock">
```
1/1 [==============================] - 1s 543ms/step
Predicted: [('n02504458', 'African_elephant', 9.86239)]
1/1 ━━━━━━━━━━━━━━━━━━━━ 3s 3s/step
Predicted: [('n02504458', 'African_elephant', 9.860664)]
```
</div>

![png](/img/examples/vision/grad_cam/grad_cam_8_1.png)
![png](/img/examples/vision/grad_cam/grad_cam_8_3.png)



Expand Down Expand Up @@ -194,7 +205,6 @@ save_and_display_gradcam(img_path, heatmap)

<div class="k-default-codeblock">
```
/var/folders/8n/8w8cqnvj01xd4ghznl11nyn000_93_/T/ipykernel_7380/2203377560.py:10: MatplotlibDeprecationWarning: The get_cmap function was deprecated in Matplotlib 3.7 and will be removed two minor releases later. Use ``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap(obj)`` instead.
jet = cm.get_cmap("jet")
```
Expand Down Expand Up @@ -227,16 +237,22 @@ preds = model.predict(img_array)
print("Predicted:", decode_predictions(preds, top=2)[0])
```

<div class="k-default-codeblock">
```
Downloading data from https://storage.googleapis.com/petbacker/images/blog/2017/dog-and-cat-cover.jpg
72452/72452 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step
```
</div>

![jpeg](/img/examples/vision/grad_cam/grad_cam_12_0.jpg)
![jpeg](/img/examples/vision/grad_cam/grad_cam_12_1.jpg)



<div class="k-default-codeblock">
```
1/1 [==============================] - 0s 154ms/step
Predicted: [('n02112137', 'chow', 4.6112413), ('n02124075', 'Egyptian_cat', 4.3817368)]
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 11ms/step
Predicted: [('n02112137', 'chow', 4.610808), ('n02124075', 'Egyptian_cat', 4.3835773)]
```
</div>
Expand All @@ -251,7 +267,7 @@ save_and_display_gradcam(img_path, heatmap)

<div class="k-default-codeblock">
```
/var/folders/8n/8w8cqnvj01xd4ghznl11nyn000_93_/T/ipykernel_7380/2203377560.py:10: MatplotlibDeprecationWarning: The get_cmap function was deprecated in Matplotlib 3.7 and will be removed two minor releases later. Use ``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap(obj)`` instead.
/tmp/ipykernel_27472/2203377560.py:10: MatplotlibDeprecationWarning: The get_cmap function was deprecated in Matplotlib 3.7 and will be removed two minor releases later. Use ``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap(obj)`` instead.
jet = cm.get_cmap("jet")
```
Expand All @@ -272,7 +288,7 @@ save_and_display_gradcam(img_path, heatmap)

<div class="k-default-codeblock">
```
/var/folders/8n/8w8cqnvj01xd4ghznl11nyn000_93_/T/ipykernel_7380/2203377560.py:10: MatplotlibDeprecationWarning: The get_cmap function was deprecated in Matplotlib 3.7 and will be removed two minor releases later. Use ``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap(obj)`` instead.
/tmp/ipykernel_27472/2203377560.py:10: MatplotlibDeprecationWarning: The get_cmap function was deprecated in Matplotlib 3.7 and will be removed two minor releases later. Use ``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap(obj)`` instead.
jet = cm.get_cmap("jet")
```
Expand Down

0 comments on commit d4bf745

Please sign in to comment.