Skip to main content

torchconvview

Latest Version pytest CC BY-SA 4.0

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}
}

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).

Release files for torchconvview 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torchconvview 0.2.0
File Size Uploaded
torchconvview-0.2.0.tar.gz 13.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for torchconvview 0.2.0
File Interpreter ABI Platform
torchconvview-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 26.9 kB

Release files / torchconvview-0.2.0.tar.gz

Download URL torchconvview-0.2.0.tar.gz
Size 13.2 kB
Tags Source
SHA-256 checksum
How to use checksums
804aecf351a39d1b2020e6a0ee749a1890a38d19ebdbc03fa9cf446579ae7487
BLAKE2b-256 checksum
How to use checksums
7bcdb568a9041b3f0c8ad7e608cc1890a0af8f2227dd096421debe395bbbcd5c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / torchconvview-0.2.0-py3-none-any.whl

Download URL torchconvview-0.2.0-py3-none-any.whl
Size 13.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
77eb5eff85e24f3fc855923a2bad373ecbe33529b5689e1e456c73e5f16088e7
BLAKE2b-256 checksum
How to use checksums
9ab3559faec966011780a96be4a4a3d1a242092ae9ea600709223d0a1e64877d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page