Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

pytorch-cnn-visualizations

> 开发工具
Open source

Pytorch implementation of convolutional neural network visualization techniques

8.2K stars0 likes1 views
WebsiteGitHub

About

Pytorch implementation of convolutional neural network visualization techniques

# 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 * [Gradient visualization with vanilla backpropagation](#gradient-visualization) * [Gradient visualization with guided backpropagation](#gradient-visualization) [1] * [Gradient visualization with saliency maps](#gradient-visualization) [4] * [Gradient-weighted class activation mapping](#gradient-visualization) [3] (Generalization of [2]) * [Guided, gradient-weighted class activation mapping](#gradient-visualization) [3] * [Score-weighted class activation mapping](#gradient-visualization) [15] (Gradient-free generalization of [2]) * [Element-wise gradient-weighted class activation mapping](#hierarchical-gradient-visualization) [16] * [Smooth grad](#smooth-grad) [8] * [CNN filter visualization](#convolutional-neural-network-filter-visualization) [9] * [Inverted image representations](#inverted-image-representations) [5] * [Deep dream](#deep-dream) [10] * [Class specific image generation](#class-specific-image-generation) [4] [14] * [Grad times image](#grad-times-image) [12] * [Integrated gradients](#gradient-visualization) [13] * [Layerwise relevance propagation](#gradient-visualization) [17] ## 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
Layerwise Relevance
(LRP) - Layer 1
## Hierarchical Gradient Visualization LayerCAM [16] is a simple modification of Grad-CAM [3], which can generate reliable class activation maps from different layers. For the examples provided below, a pre-trained **VGG16** was used.
Class Activation Map Class Activation HeatMap Class Activation HeatMap on Image
LayerCAM
(Layer 9)
LayerCAM
(Layer 16)
LayerCAM
(Layer 23)
LayerCAM
(Layer 30)
## Grad Times Image Another technique that is proposed is simply multiplying the gradients with the image itself. Results obtained with the usage of multiple gradient techniques are below.
Vanilla Grad
X
Image
Guided Grad
X
Image
Integrated Grad
X
Image
## Smooth Grad Smooth grad is adding some Gaussian noise to the original image and calculating gradients multiple times and averaging the results [8]. There are two examples at the bottom which use _vanilla_ and _guided_ backpropagation to calculate the gradients. Number of images (_n_) to average over is selected as 50. _σ_ is shown at the bottom of the images.
Vanilla Backprop
Guided Backprop
## Convolutional Neural Network Filter Visualization CNN filters can be visualized when we optimize the input image with respect to output of the specific convolution operation. For this example I used a pre-trained **VGG16**. Visualizations of layers start with basic color and direction filters at lower levels. As we approach towards the final layer the complexity of the filters also increase. If you employ external techniques like blurring, gradient clipping etc. you will probably produce better images.
Layer 2
(Conv 1-2)
Layer 10
(Conv 2-1)
Layer 17
(Conv 3-1)
Layer 24
(Conv 4-1)
Another way to visualize CNN layers is to to visualize activations for a specific

Issues· 2 open

View all issuesOpen on GitHub
  • #40

    image size problem

    Updated Feb 19, 2021

> Tags

Pythoncamcnn-visualizationdeep-dreamgrad-cam

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 1, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具