Git Product home page Git Product logo

activation-maps-visualiser-pt's Introduction


Activation maps visualizer for PyTorch

example

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

Getting Started

To get a local copy up and running, follow these simple example steps.

Prerequisites

  • Python ⩾ 3.8
    sudo apt install python3 python3-pip

Installation

  1. Clone the repo
    git clone https://github.com/Clement-W/Activation-Maps-Visualiser-PT.git
    cd Activation-Maps-Visualiser-PT/
  2. Create and activate a virtual environment
    pip3 install virtualenv --upgrade
    virtualenv venv
    source venv/bin/activate
  3. Install the requirements
    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 :

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 :

device = torch.device("cuda:0"  if torch.cuda.is_available() else  "cpu")

Load pretrained RestNet-18 model from PyTorch :

model = models.resnet18(pretrained=True)

Load an image to feed the ResNet-18 model :

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 :

show_activation_maps(model,"conv1",image)

If you want to save the activation maps of every layers for that input image :

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

activation-maps-visualiser-pt's People

Contributors

clement-w avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.