Skip to content

Commit

Permalink
docs: Updated notebook ref in READMEs (#102)
Browse files Browse the repository at this point in the history
* docs: Updated notebook ref in READMEs

* docs: Updated quicktour
  • Loading branch information
frgfm authored Oct 31, 2021
1 parent 0f10df0 commit d8d722d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# TorchCAM: class activation explorer

[![License: Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/25324db1064a4d52b3f44d657c430973)](https://www.codacy.com/gh/frgfm/torch-cam/dashboard?utm_source=github.com&utm_medium=referral&utm_content=frgfm/torch-cam&utm_campaign=Badge_Grade) ![Build Status](https://github.com/frgfm/torch-cam/workflows/tests/badge.svg) [![codecov](https://codecov.io/gh/frgfm/torch-cam/branch/master/graph/badge.svg)](https://codecov.io/gh/frgfm/torch-cam) [![Docs](https://img.shields.io/badge/docs-available-blue.svg)](https://frgfm.github.io/torch-cam) [![Pypi](https://img.shields.io/badge/pypi-v0.2.0-blue.svg)](https://pypi.org/project/torchcam/) [![Hugging Face Spaces](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue)](https://huggingface.co/spaces/frgfm/torch-cam) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/frgfm/torch-cam/blob/master/notebooks/quicktour.ipynb)
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/25324db1064a4d52b3f44d657c430973)](https://www.codacy.com/gh/frgfm/torch-cam/dashboard?utm_source=github.com&utm_medium=referral&utm_content=frgfm/torch-cam&utm_campaign=Badge_Grade) ![Build Status](https://github.com/frgfm/torch-cam/workflows/tests/badge.svg) [![codecov](https://codecov.io/gh/frgfm/torch-cam/branch/master/graph/badge.svg)](https://codecov.io/gh/frgfm/torch-cam) [![Docs](https://img.shields.io/badge/docs-available-blue.svg)](https://frgfm.github.io/torch-cam) [![Pypi](https://img.shields.io/badge/pypi-v0.2.0-blue.svg)](https://pypi.org/project/torchcam/) [![Hugging Face Spaces](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue)](https://huggingface.co/spaces/frgfm/torch-cam) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/frgfm/notebooks/blob/main/torch-cam/quicktour.ipynb)

Simple way to leverage the class-specific activation of convolutional layers in PyTorch.

Expand Down
45 changes: 28 additions & 17 deletions docs/source/notebooks/quicktour.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Latest stable release

.. code-block:: python
>>> !pip install torch-cam
>>> !pip install torchcam
From source
-----------
Expand All @@ -29,6 +29,20 @@ Now go to ``Runtime/Restart runtime`` for your changes to take effect!
Basic usage
===========

.. code-block:: python
>>> %matplotlib inline
>>> # All imports
>>> import matplotlib.pyplot as plt
>>> import torch
>>> from torch.nn.functional import softmax, interpolate
>>> from torchvision.io.image import read_image
>>> from torchvision.models import resnet18
>>> from torchvision.transforms.functional import normalize, resize, to_pil_image
>>>
>>> from torchcam.cams import SmoothGradCAMpp, LayerCAM
>>> from torchcam.utils import overlay_mask
.. code-block:: python
>>> # Download an image
Expand All @@ -40,24 +54,13 @@ Basic usage
.. code-block:: python
>>> # Instantiate your model here
>>> from torchvision.models import resnet18
>>> model = resnet18(pretrained=True).eval()
Illustrate your classifier capabilities
---------------------------------------

.. code-block:: python
>>> %matplotlib inline
>>> # All imports
>>> from torchvision.io.image import read_image
>>> from torchvision.transforms.functional import normalize, resize, to_pil_image
>>> import matplotlib.pyplot as plt
>>> from torchcam.cams import SmoothGradCAMpp, LayerCAM
>>> from torchcam.utils import overlay_mask
.. code-block:: python
>>> cam_extractor = SmoothGradCAMpp(model)
Expand All @@ -69,6 +72,7 @@ Illustrate your classifier capabilities
>>> out = model(input_tensor.unsqueeze(0))
>>> # Retrieve the CAM by passing the class index and the model output
>>> cams = cam_extractor(out.squeeze(0).argmax().item(), out)
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
.. code-block:: python
Expand Down Expand Up @@ -104,18 +108,14 @@ Advanced tricks
Extract localization cues
-------------------------
.. code-block::python
>>> import torch
>>> from torch.nn.functional import softmax, interpolate
.. code-block::python
>>> # Retrieve the CAM from several layers at the same time
>>> cam_extractor = LayerCAM(model)
>>> # Preprocess your data and feed it to the model
>>> out = model(input_tensor.unsqueeze(0))
>>> print(softmax(out, dim=1).max())
WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
tensor(0.9115, grad_fn=<MaxBackward1>)
Expand All @@ -135,6 +135,11 @@ Extract localization cues
>>> axes[1].imshow(seg); axes[1].axis('off'); axes[1].set_title(name)
>>> plt.show()
.. code-block:: python
>>> # Once you're finished, clear the hooks on your model
>>> cam_extractor.clear_hooks()
Fuse CAMs from multiple layers
------------------------------
Expand All @@ -153,6 +158,7 @@ Fuse CAMs from multiple layers
>>> # This time, there are several CAMs
>>> for cam in cams:
>>> print(cam.shape)
torch.Size([28, 28])
torch.Size([14, 14])
torch.Size([7, 7])
Expand All @@ -175,3 +181,8 @@ Fuse CAMs from multiple layers
>>> # Plot the overlayed version
>>> result = overlay_mask(to_pil_image(img), to_pil_image(fused_cam, mode='F'), alpha=0.5)
>>> plt.imshow(result); plt.axis('off'); plt.title(" + ".join(cam_extractor.target_names)); plt.show()
.. code-block:: python
>>> # Once you're finished, clear the hooks on your model
>>> cam_extractor.clear_hooks()
4 changes: 2 additions & 2 deletions notebooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ Here are some notebooks compiled for users to better leverage the library capabi

| Notebook | Description | |
|:----------|:-------------|------:|
| [Quicktour](https://github.com/frgfm/torch-cam/blob/master/notebooks/quicktour.ipynb) | A presentation of the main features of TorchCAM | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/frgfm/torch-cam/blob/master/notebooks/quicktour.ipynb) |
| [Latency benchmark](https://github.com/frgfm/torch-cam/blob/master/notebooks/latency_benchmark.ipynb) | How to benchmark the latency of a CAM method | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/frgfm/torch-cam/blob/master/notebooks/latency_benchmark.ipynb) |
| [Quicktour](https://github.com/frgfm/notebooks/blob/main/torch-cam/quicktour.ipynb) | A presentation of the main features of TorchCAM | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/frgfm/notebooks/blob/main/torch-cam/quicktour.ipynb) |
| [Latency benchmark](https://github.com/frgfm/notebooks/blob/main/torch-cam/latency_benchmark.ipynb) | How to benchmark the latency of a CAM method | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/frgfm/notebooks/blob/main/torch-cam/latency_benchmark.ipynb) |

0 comments on commit d8d722d

Please sign in to comment.