Skip to content

Commit

Permalink
use plt.imshow rather than cv2_imshow (better compat. with jupyter)
Browse files Browse the repository at this point in the history
  • Loading branch information
qualiaMachine authored Nov 26, 2024
1 parent 0210add commit ccfec0a
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions episodes/5d-gradcam.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ exercises: 0
import requests

# Packages to view and process images
import matplotlib.pyplot as plt
import cv2
import numpy as np
from PIL import Image
from google.colab.patches import cv2_imshow

# Packages to load the model
import torch
Expand All @@ -39,7 +39,7 @@ from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
from pytorch_grad_cam.utils.image import show_cam_on_image, preprocess_image
```
```python
device = 'gpu' if torch.cuda.is_available() else 'cpu'
device = 'cpu' # we're using the CPU only version of this workshop
```
##### Load Model

Expand Down Expand Up @@ -112,22 +112,23 @@ visualized_class_id = 245
```python
def viz_gradcam(model, target_layers, class_id):

if class_id is None:
targets = None
else:
targets = [ClassifierOutputTarget(class_id)]
if class_id is None:
targets = None
else:
targets = [ClassifierOutputTarget(class_id)]

cam_algorithm = GradCAM
with cam_algorithm(model=model, target_layers=target_layers) as cam:
grayscale_cam = cam(input_tensor=input_tensor,
targets=targets)
cam_algorithm = GradCAM
with cam_algorithm(model=model, target_layers=target_layers) as cam:
grayscale_cam = cam(input_tensor=input_tensor, targets=targets)

grayscale_cam = grayscale_cam[0, :]
grayscale_cam = grayscale_cam[0, :]

cam_image = show_cam_on_image(rgb_image, grayscale_cam, use_rgb=True)
cam_image = cv2.cvtColor(cam_image, cv2.COLOR_RGB2BGR)
cam_image = show_cam_on_image(rgb_image, grayscale_cam, use_rgb=True)
cam_image = cv2.cvtColor(cam_image, cv2.COLOR_RGB2BGR)

cv2_imshow(cam_image)
plt.imshow(cam_image)
plt.axis("off")
plt.show()
```
Finally, we can start visualizing! Let's begin by seeing what parts of the image the model looks at to make its most confident prediction.
```python
Expand Down Expand Up @@ -162,4 +163,4 @@ viz_gradcam(model=model, target_layers=target_layers, class_id=537)
```
Explaining model predictions though visualization techniques like this can be very subjective and prone to error. However, this still provides some degree of insight a completely black box model would not provide.

Spend some time playing around with different classes and seeing which part of the image the model looks at. Feel free to play around with other base images as well. Have fun!
Spend some time playing around with different classes and seeing which part of the image the model looks at. Feel free to play around with other base images as well. Have fun!

0 comments on commit ccfec0a

Please sign in to comment.