A library for PyTorch convolution layer visualizations.
Project description
torchconvview
A library for PyTorch convolution layer visualizations via matplotlib plots.
Installation
To install published releases from PyPi execute:
pip install torchconvview
To update torchconvquality to the latest available version, add the --upgrade
flag to the above commands.
If you want the latest (potentially unstable) features you can also directly install from the github main branch:
pip install git+https://github.com/paulgavrikov/torchconvview
Usage
from torchconvvision import plot_conv, plot_conv_rgb, PCAView
import matplotlib.pyplot as plt
# Replace this with your own model. As an example,
# we will use an ImageNet pretrained ResNet-18.
import torchvision
model = torchvision.models.resnet18(pretrained=True)
General
All plot_...
functions return a tuple of the matplotlib figure and axes which allow you to customize the plot to your needs. Also most of these functions accept the img_scale
argument which allows you to specify a multiplier to the resolution.
Visualize kernels in the convolution layers
Just pass the convolution weight as tensor or numpy into plot_conv
and you'll get a matplotlib figure of the kernels! Each column is one channel/filter, i.e. this stack of kernels generates a feature-map from all input maps.
plot_conv(model.layer1[1].conv2.weight)
plt.show()
Visualize the first layer
If you have a convolution layer with RGB input (e.g. often the first layer), the you can visualize entire filters. This function maps all kernels to their respective color. Note that this only work on convolution layers with 3 input channels and only produces meaningfull results if these channels are R, G, B feature-maps!
plot_conv_rgb(model.conv1.weight)
plt.show()
PCA of convolution weights
You can also compute the eigenimages/basis vectors of the kernels by using the PCAView
class. Under the hood it will do a PCA for you. Note, that currently this requires the scikit-learn
module.
pcaview = PCAView(model.conv1.weight)
pcaview.plot_conv()
plt.show()
And to get a handy barplot of the explained variance ratio:
pcaview.plot_variance_ratio()
plt.show()
Citation
Please consider citing our publication if this libary was helpfull to you.
@InProceedings{Gavrikov_2022_CVPR,
author = {Gavrikov, Paul and Keuper, Janis},
title = {CNN Filter DB: An Empirical Investigation of Trained Convolutional Filters},
booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
month = {June},
year = {2022},
pages = {19066-19076}
}
Legal
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Funded by the Ministry for Science, Research and Arts, Baden-Wuerttemberg, Germany Grant 32-7545.20/45/1 (Q-AMeLiA).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file torchconvview-0.2.0.tar.gz
.
File metadata
- Download URL: torchconvview-0.2.0.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 804aecf351a39d1b2020e6a0ee749a1890a38d19ebdbc03fa9cf446579ae7487 |
|
MD5 | 49cc1e2881d634fa0cf15c7bd2bcdc62 |
|
BLAKE2b-256 | 7bcdb568a9041b3f0c8ad7e608cc1890a0af8f2227dd096421debe395bbbcd5c |
File details
Details for the file torchconvview-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: torchconvview-0.2.0-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77eb5eff85e24f3fc855923a2bad373ecbe33529b5689e1e456c73e5f16088e7 |
|
MD5 | 22d744b8054ff3891f73801e7dc94b8f |
|
BLAKE2b-256 | 9ab3559faec966011780a96be4a4a3d1a242092ae9ea600709223d0a1e64877d |