Coder Social home page Coder Social logo

paulgavrikov / torchconvview Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 405 KB

A library for PyTorch convolution layer visualizations.

License: Creative Commons Attribution Share Alike 4.0 International

Python 100.00%
cnn python pytorch torch torchvision computer-vision

torchconvview's Introduction

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

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

torchconvview's People

Contributors

paulgavrikov avatar

Stargazers

Giovanni Ballarin avatar Manav Madan avatar

Watchers

 avatar Kostas Georgiou avatar

torchconvview's Issues

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.