Skip to main content

A CNN visualization library for pytorch

Project description

Title icon

forks forks pip downloads MIT License python_version

Introduction

CNNs do a great job at recognizing images (when appropriately trained).

Problems arise when it comes to interpret the network: although one coded the network in question and knows all the tips and tricks necessary to train it efficiently, one might ignore how it generates the output from a given image.

Torchlurk aims at helping the user in that sense: it provides an interface to visualize a Pytorch network in an efficient yet simple manner, similarly to Microscope.

All you need is the trained Pytorch network and its training set. That's it.

Installation ☕

Torchlurk is available on pip! Just run:
pip install torchlurk

Overview ☝

demo gif


Documentation 📚

Torchlurk has an online documentation which gets regularly updated.

Quick Start ⌛

Your training set should follow the following structure in order for the lurker to load properly your datas:

.
├── src                  
│   ├── name_class1
│   │   ├── class1id_1.jpg
│   │   ├── class1id_2.jpg
│   │   ├── ...
│   ├── name_class2
│   │   ├── class2id_1.jpg
│   │   ├── class2id_2.jpg
│   │   ├── ...
│   ├── ...

1. Instanciation

import torchlurk
import torch

# load the trained model
your_model = ModelClass() 
your_model.load_state_dict(torch.load(PATH))

# the preprocess used for the training
preprocess = transforms.Compose(...)

# and instanciate a lurker
lurker = Lurk(your_model,
          preprocess,
          save_gen_imgs_dir='save/dir',
          save_json_path='save/dir',
          imgs_src_dir=".source/dir",
          side_size=224)

2. Layer Visualization

The layer visualization is an artificial image generated by gradient descent which aims at maximizing the acivation of a given filter: this gives useful insights on the type of texture/colors the filter in question is looking at in inputs images.

# compute the layer visualisation for a given set of layers/filters
lurker.compute_layer_viz(layer_indx = 12,filter_indexes=[7])
# OR compute it for the whole network
lurker.compute_viz()
# plot the filters
lurker.plot_filter_viz(layer_indx=12,filt_indx=7)


3. Max Activation

The max activation represents the top N images activating a given filter the most in terms of average or max score.

#compute the top activation images
lurker.compute_top_imgs(compute_max=True,compute_avg=True)
# plot them
lurker.plot_top("avg",layer_indx=12,filt_indx=7)


3.1 Deconvolution

# plot the max activating images along with their cropped areas
lurker.plot_crop(layer_indx=2,filt_indx=15)


4. Gradients

Guided GRAD CAM is another way to check what a given filter is looking at: it relies in particular in isolating a specific location in the image that excites our neurons. For more information, check this article.

#compute the gradients
lurker.compute_grads()
# plot them
lurker.plot_top("avg",layer_indx=12,filt_indx=7,plot_imgs=False,plot_grads=True)


5. Histograms

Torchlurk allows you to visualize the most activating classes of the training using histograms: a very peaked distribution is often asssociated with a specialized filter.

# display the 
lurker.plot_hist(layer_indx=12,filt_indx=7,hist_type="max",num_classes=12)


6. Serving

Torchlurk is equiped with a live update tool which allows you to visualize your computed results while coding.

#serve the application on port 5001
lurker.serve(port=5001)

lurker.end_serve()

Happy Lurking!

🕵

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchlurk-0.1.3.1.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torchlurk-0.1.3.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file torchlurk-0.1.3.1.tar.gz.

File metadata

  • Download URL: torchlurk-0.1.3.1.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.6.9

File hashes

Hashes for torchlurk-0.1.3.1.tar.gz
Algorithm Hash digest
SHA256 90ee3bbf495743432bd09469155196a3b87e4041fb5caaff883e86adea36399b
MD5 78e52a0d8ae54b0c82387aa8d5954136
BLAKE2b-256 e1833d8664ccefd831bf5752ed4079495d7cd754ededd1623368a0da8e876b25

See more details on using hashes here.

File details

Details for the file torchlurk-0.1.3.1-py3-none-any.whl.

File metadata

  • Download URL: torchlurk-0.1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.6.9

File hashes

Hashes for torchlurk-0.1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 810d01f92f83e1ca0a4c0566f060e651c293e3c543a59412968e96a266834b2b
MD5 e5732cf680dcc0b328bb5da611daaf75
BLAKE2b-256 b0c62fbd32d2fee054cb5d6cb44419fec2ebe1950bafc9b790d3f912db2d0af1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page