https://github.com/clement-w/activation-maps-visualiser-pt
Activation maps visualizer for PyTorch
https://github.com/clement-w/activation-maps-visualiser-pt
activation-maps cnn python3 pytorch
Last synced: about 1 month ago
JSON representation
Activation maps visualizer for PyTorch
- Host: GitHub
- URL: https://github.com/clement-w/activation-maps-visualiser-pt
- Owner: Clement-W
- License: mit
- Created: 2021-02-21T18:27:26.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-11-21T21:32:41.000Z (over 4 years ago)
- Last Synced: 2025-03-13T03:19:16.581Z (over 1 year ago)
- Topics: activation-maps, cnn, python3, pytorch
- Language: Python
- Homepage:
- Size: 44.6 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Activation maps visualizer for PyTorch
## About The Project
I've created a little PyTorch script to display the activation maps of a specific, or all the CNN's layers.
Since I'm learning PyTorch, I thought it would be interesting to do this little project to get used to handling torch objects and tensors.
### Built With
* [Python 3.8](https://www.python.org/)
* [PyTorch](https://pytorch.org/)
* [Matplotlib](https://matplotlib.org)
## Getting Started
To get a local copy up and running, follow these simple example steps.
### Prerequisites
* Python ⩾ 3.8
```sh
sudo apt install python3 python3-pip
```
### Installation
1. Clone the repo
```sh
git clone https://github.com/Clement-W/Activation-Maps-Visualiser-PT.git
cd Activation-Maps-Visualiser-PT/
```
3. Create and activate a virtual environment
```sh
pip3 install virtualenv --upgrade
virtualenv venv
source venv/bin/activate
```
4. Install the requirements
```sh
pip3 install -r requirements.txt
```
## Usage
### Use ActivationMapExtractor.py
There is two main ways to use this python script :
* Call the function **show_activation_maps(model, layer_name, image)**. This function shows the activation maps for a specific layer in the model.
* Call the function **save_all_activation_maps(model, image,path_to_directory)**. This function saves the activation maps of every layers of the model in the specified directory.
Check the next section to see an example.
### Demo with RestNet-18
Import modules :
```py
from torchvision import models, transforms, datasets
import torch
from PIL import Image
from ActivationMapsExtractor import save_all_activation_maps, show_activation_maps
```
Set torch device :
```py
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
```
Load pretrained RestNet-18 model from PyTorch :
```py
model = models.resnet18(pretrained=True)
```
Load an image to feed the ResNet-18 model :
```py
image = Image.open("imageNet/sample.JPEG")
transform = transforms.Compose([transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor()])
image = transform(image).to(device)
```
If you want to see the activation maps of a specific layer by it's name for that input image :
```py
show_activation_maps(model,"conv1",image)
```
If you want to save the activation maps of every layers for that input image :
```py
save_all_activation_maps(model, image,"./ResNet-activation-maps")
```
## Contributing
I'm still learning PyTorch, so feel free to use Issues or PR to report errors and/or propose additions or corrections to my code. Any contributions you make are **greatly appreciated**.
## License
Distributed under the MIT License. See `LICENSE` for more information.
## Todo list
- [ ] Add a Jupyter notebook demo