MCPcopy Index your code
hub / github.com/utkuozbulak/pytorch-cnn-visualizations

github.com/utkuozbulak/pytorch-cnn-visualizations @main

repository ↗ · DeepWiki ↗ · + Follow
94 symbols 340 edges 17 files 47 documented · 50%
README

Convolutional Neural Network Visualizations

This repository contains a number of convolutional neural network visualization techniques implemented in PyTorch.

Note: I removed cv2 dependencies and moved the repository towards PIL. A few things might be broken (although I tested all methods), I would appreciate if you could create an issue if something does not work.

Note: The code in this repository was tested with torch version 0.4.1 and some of the functions may not work as intended in later versions. Although it shouldn't be too much of an effort to make it work, I have no plans at the moment to make the code in this repository compatible with the latest version because I'm still using 0.4.1.

Implemented Techniques

General Information

Depending on the technique, the code uses pretrained AlexNet or VGG from the model zoo. Some of the code also assumes that the layers in the model are separated into two sections; features, which contains the convolutional layers and classifier, that contains the fully connected layer (after flatting out convolutions). If you want to port this code to use it on your model that does not have such separation, you just need to do some editing on parts where it calls model.features and model.classifier.

Every technique has its own python file (e.g. gradcam.py) which I hope will make things easier to understand. misc_functions.py contains functions like image processing and image recreation which is shared by the implemented techniques.

All images are pre-processed with mean and std of the ImageNet dataset before being fed to the model. None of the code uses GPU as these operations are quite fast for a single image (except for deep dream because of the example image that is used for it is huge). You can make use of gpu with very little effort. The example pictures below include numbers in the brackets after the description, like Mastiff (243), this number represents the class id in the ImageNet dataset.

I tried to comment on the code as much as possible, if you have any issues understanding it or porting it, don't hesitate to send an email or create an issue.

Below, are some sample results for each operation.

Gradient Visualization

Target class: King Snake (56) Target class: Mastiff (243) Target class: Spider (72)
Original Image
Colored Vanilla Backpropagation
Vanilla Backpropagation Saliency
Colored Guided Backpropagation (GB)
Guided Backpropagation Saliency (GB)
Guided Backpropagation Negative Saliency (GB)
Guided Backpropagation Positive Saliency (GB)
Gradient-weighted Class Activation Map (Grad-CAM)
Gradient-weighted Class Activation Heatmap (Grad-CAM)
Gradient-weighted Class Activation Heatmap on Image (Grad-CAM)
Score-weighted Class Activation Map (Score-CAM)
Score-weighted Class Activation Heatmap (Score-CAM)
Score-weighted Class Activation Heatmap on Image (Score-CAM)
Colored Guided Gradient-weighted Class Activation Map (Guided-Grad-CAM)
Guided Gradient-weighted Class Activation Map Saliency (Guided-Grad-CAM)
Integrated Gradients (without image multiplication)
Layerwise Relevance (LRP) - Layer 7

Core symbols most depended-on inside this repo

save_gradient_images
called by 16
src/misc_functions.py
get_example_params
called by 12
src/misc_functions.py
save_image
called by 11
src/misc_functions.py
convert_to_grayscale
called by 7
src/misc_functions.py
recreate_image
called by 6
src/misc_functions.py
preprocess_image
called by 5
src/misc_functions.py
generate_gradients
called by 4
src/guided_backprop.py
alpha_norm
called by 3
src/inverted_representation.py

Shape

Method 64
Class 16
Function 14

Languages

Python100%

Modules by API surface

src/misc_functions.py11 symbols
src/layercam.py8 symbols
src/layer_activation_with_guided_backprop.py8 symbols
src/guided_backprop.py8 symbols
src/gradcam.py8 symbols
src/scorecam.py7 symbols
src/inverted_representation.py7 symbols
src/integrated_gradients.py7 symbols
src/cnn_layer_visualization.py6 symbols
src/vanilla_backprop.py5 symbols
src/deep_dream.py5 symbols
src/LRP.py5 symbols

For agents

$ claude mcp add pytorch-cnn-visualizations \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact