Skip to content

Commit

Permalink
Retain model outputs during CAM inference (#419)
Browse files Browse the repository at this point in the history
* Retain model outputs

* Update README.md

Added retrieving model outputs in example
  • Loading branch information
JustasB committed Dec 9, 2023
1 parent 82c88cc commit a797af2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ grayscale_cam = cam(input_tensor=input_tensor, targets=targets)
# In this example grayscale_cam has only one image in the batch:
grayscale_cam = grayscale_cam[0, :]
visualization = show_cam_on_image(rgb_img, grayscale_cam, use_rgb=True)

# You can also get the model outputs without having to re-inference
model_outputs = cam.outputs
```

----------
Expand Down
3 changes: 2 additions & 1 deletion pytorch_grad_cam/base_cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def forward(self,
input_tensor = torch.autograd.Variable(input_tensor,
requires_grad=True)

outputs = self.activations_and_grads(input_tensor)
self.outputs = outputs = self.activations_and_grads(input_tensor)

if targets is None:
target_categories = np.argmax(outputs.cpu().data.numpy(), axis=-1)
targets = [ClassifierOutputTarget(
Expand Down

0 comments on commit a797af2

Please sign in to comment.