<jupyter_start><jupyter_text>Install the Library<jupyter_code># Install from PyPI
!pip install interpret-pytorch

# Or install from github
# !pip install git+https://github.com/ttumiel/interpret<jupyter_output><empty_output><jupyter_text>Quickstart<jupyter_code>from interpret import OptVis, ImageParam, denorm
import torchvision, torch

# Create model
network = torchvision.models.googlenet(pretrained=True)

# Create an OptVis object from a PyTorch model, selecting a particular layer
optvis = OptVis.from_layer(network, layer='inception4c/branch1/conv:96')

# Create visualisation
optvis.vis()<jupyter_output><empty_output><jupyter_text>Generate Visualisations from a Pretrained VGG<jupyter_code>network = torchvision.models.vgg11(pretrained=True)
network.to('cuda' if torch.cuda.is_available() else 'cpu');<jupyter_output><empty_output><jupyter_text>Class VisualisationsNow, we generate visualisations of the output classes. To change the class selected, simply change the value of `neuron`.We parameterise the input noise in the colour decorrelated, Fourier domain. This helps create better visualisations. For more, see: https://distill.pub/2017/feature-visualization/<jupyter_code># Select a layer from the network. Use get_layer_names() to see a list of layer names and sizes.
layer = 'classifier/6'
neuron = 888 # viaduct

# Create an OptVis object from a PyTorch model
optvis = OptVis.from_layer(network, layer=layer, neuron=neuron)

# Parameterise input noise in colour decorrelated Fourier domain
img_param = ImageParam(128, fft=True, decorrelate=True)

# Create visualisation
optvis.vis(img_param, thresh=(250, 500), transform=True, lr=0.05, wd=1e-2)<jupyter_output><empty_output><jupyter_text>Channel VisualisationsNow let's generate some visualisations of the channels of the convolutional layers of the network. We can see the names and number of channels of each layer by using the `get_layer_names()` method. We can then generate channel visualisations in the same way.<jupyter_code>from interpret import get_layer_names
get_layer_names(network)
# Select a layer from the network. Use get_layer_names() to see a list of layer names and sizes.
layer = 'features/16'

# Choose a channel that is within the size of the layer
channel = 32

# Create an OptVis object from a PyTorch model
optvis = OptVis.from_layer(network, layer=layer, channel=channel)

# Parameterise input noise in colour decorrelated Fourier domain
img_param = ImageParam(128, fft=True, decorrelate=True)

# Create visualisation
optvis.vis(img_param, thresh=(250, 500), transform=True, lr=0.05)<jupyter_output><empty_output><jupyter_text>Generate Attribution maps Using Grad-CAMGrad-CAM [1] is a technique that finds relevant features for a particular class. The method generates a heatmap over the input where the network identifies features of that particular class.[1] - https://arxiv.org/abs/1610.02391<jupyter_code>from interpret import Gradcam, norm
from PIL import Image
# Download an image to apply attribution to
!curl https://www.yourpurebredpuppy.com/dogbreeds/photos2-G/german-shepherd-05.jpg -o dog.jpg
img = Image.open("dog.jpg")
img
# Normalize the input image and turn it into a tensor
data = norm(img).to('cuda' if torch.cuda.is_available() else 'cpu')
class_number = 235 # German Shepherd
layer = 'features/20'

# Generate the gradcam attribution map for a particular class
attr = Gradcam(network, data, im_class=class_number, layer=layer);
attr.show()
# You can also show the heatmap without the original image to see the heatmap clearer
attr.show(show_image=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08421345191773208, 'fraction_numerical': 0.017509727626459144, 'mean_word_length': 6.329938900203666, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '578878', 'license': 'MIT', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1073, 'n_words': 367, 'path': 'nbs/Interpret-Intro.ipynb', 'repo_name': 'ttumiel/interpret', 'size': '3598'}