Model Understanding with Captum

PyTorch · Beginner ·🛠️ AI Tools & Apps ·5y ago

Key Takeaways

The video demonstrates model interpretability using Captum, an open-source library built on PyTorch, and its various methods for explaining model output and layer activity, including feature attribution and layer attribution, with tools like Captum Insights for visualization.

Full Transcript

welcome to the next video in the pytorch training series this video gives an overview of captain pi torches toolset for model interpretability in this video we'll discuss the basic concepts of captain that we'll be covering attributions attribution algorithms and visualizations we'll demonstrate how to perform and visualize feature attributions for a computer vision classifier we'll apply layer attribution to the same classifier to examine the activity of a model's hidden layers and finally we'll look at captive insights an api for creating visualization widgets for images text and other features captain provides a deep set of tools for explaining the behavior of your pi torch models this video and the accompanying interactive notebook provide only an overview of core features the website at captain ai contains more in-depth tutorials documentation and an api reference to run the interactive notebook associated with this video you'll want to install python version 3.6 or higher flask 1.1 or higher and the latest versions of pi torch torch vision and captain captain can be easily installed with pip or with anaconda by specifying the pi torch channel to start with we're going to take a pre-trained image classifier resnet trained against the imagenet dataset and we're going to use the tools within captum to gain insight into how the model responds to a particular input image to give its prediction this first cells a bunch of imports including attribution methods and visualization tools from captain which we'll examine shortly next we'll get our pre-trained model then we'll pull up an image to work with wherever you've got this video in the interactive notebook should also include a folder of images for use in this tutorial in our case it's going to be a cat next we'll define some image transforms to prepare the image for consumption by the model and bring in the human readable labels of the thousand imagenet classes now let's see what the model thinks this is and thinks our cat is a cat but why does the model think this is a picture of a cat for the answer to that we can look under the hood of the model with captain the core abstraction in captain is the attribution that is a quantitative method of attributing a particular output or activity of a model with its input the first kind of attribution is feature attribution this lets us ask which parts of the input were most important in determining a model's prediction it lets us find answers to questions like which words in this input question were most significant in deciding the answer which pixels in this input image drove the model's classification of the image which features of the input data were most significant to my regression model's prediction feature attribution just covers inputs and outputs though what if we want to see what's happening inside the model for that we have layer attribution this attributes the activity of a hidden layer of a model to the model's input it lets us answer questions like which neurons in this layer were most active given this input which neurons in this layer were most important to how the input influenced a particular output neuron how is the activation map output by this convolutional layer correlated to my input image finally there's neuron attribution this is similar to layer attribution but goes down to the level of individual neurons in the model in this tutorial we're going to look at feature attribution and layer attribution first feature attribution attributions are realized by an attribution algorithm a particular method of mapping model activity to inputs the first feature attribution algorithm we'll look at is called integrated gradients this algorithm numerically approximates the integral of the gradients of the model's output with respect to its inputs essentially finding the most important paths through the model for a given input output pair we'll go ahead and create an integrated gradients object initializing it with our model then we'll call the attribute method on it we'll feed it our input our output label and an optional number of steps to run note that running this cell can take a couple of minutes the process of integrating the gradients is computationally intensive once that cell finishes running we have a sort of numerical importance map of the cat image with respect to the cat label generated by the model for a simple regression model with few output categories we might just print that out as a table but for a more complicated cv model with a large input like an image it would help to be able to relate the importance map to the image visually captain's got you covered the visualization module gives you tools for exactly that here we're going to make two calls to visualize image adder the first displays the original image first we need to make some adjustments to the image we call squeeze to remove the batch dimension on the image we make sure we're running on cpu we detach the image tensor from computation history otherwise the image tension will keep tracking its computation history unnecessarily and finally we make it a numpy array and switch the dimensions around and put the color channels last the first argument of this method would normally be the attributions but for this call we're going to make that none we're just displaying the original image the second argument is our transformed image the third argument is a visualization method a string that indicates how you want the visualization to work here we told captain we just want to display the original image finally we give our visualization an instructive title the second call will make a visual mapping of the important regions of our image the first argument is the attributions we got from integrated gradients and the second is our transformed image for a method we'll specify heat map where color intensity maps to the importance of an image region captain allows you to use custom color maps from matplotlib and we've made one here that will slightly enhance the contrast of our heat map we specify sine as positive we're only looking at positive attributions running the cell we can see that the model is paying attention to the outline of the cat as well as the region around the center of the cat's face let's try another feature attribution algorithm next we'll try occlusion integrated gradients was a gradient-based attribution algorithm occlusion is different it's a based method that involves screening out portions of the image and seeing how that affects the output as before we're going to specify our input image and our output labeled the attribution algorithm for occlusion we're going to specify a few more items the first are the sliding window and the stride length and these are analogous to similar configuration options in a convolutional neural network we're also going to set our baseline that is our representation of an occluded image cell is zero depending on how your data are normalized you may wish to specify a different baseline but for zero centered data it makes sense to use zero we'll run the attribute call and give it a minute and in the next cell we're doing something new we're calling visualize image adder multiple to show multiple visualizations of the occlusion attribution besides the original image we'll show three visualizations the first two are heat maps of both positive and negative attributions you can see that we're providing a list of methods with heat map being the second and third we're also specifying a sign for each visualization and here you can see that we've asked for positive attributions on one heat map and negative on the other these indicate which for our final visualization we'll use the mask method this uses positive attributions to selectively screen the original image giving a striking visual representation of the areas of the image the model paid most attention to for this input output pair running the cell you can see that this maps well to what we learned from integrated gradients most of the activities around the cat's outline and the center of its face what about what the model is doing under the hood let's use a layer attribution algorithm to check the activity of one of the hidden layers grad cam is another gradient-based attribution algorithm designed for confidence it computes the gradients of the output with respect to the specified model layer averages the gradients for each channel and multiplies this average by the layer activations and uses this as a measure of the importance of a layer's output to get started with layer attribution we'll create a layer gradcam object and initialize it with our model and the layer we wish to examine then we'll give it the input output pair and ask it to do attribution we can visualize this with a heat map as we did before in this way you can visually examine which areas of a confidence activation map relate to your output we can do better than this though since the output of a convolutional layer is usually spatially correlated to the input we can take advantage of that by up-sampling that activation map and comparing it directly with the input the layer attribution parent class has a convenience method for upsampling the lower resolution confident activation map up to the input size we'll do that with the interpolate method here and ask the visualizer for a blended heat map showing the original image with a heat map superimposed and a masked image visualizations like this can give you insight into how hidden layers contribute to a particular output from your model captain comes with an advanced visualization tool called captain insights which lets you put together multiple visualizations in an in-browser widget that lets you configure the attribution algorithm and its parameters captain insights lets you visualize text image and arbitrary data we're going to try three images now the cat a teapot and a trilobite fossil again these images should be available wherever you've got the interactive notebook that goes with this video first we'll query the model to see what it thinks each of these are and it seems to be doing okay now let's set up captain insights we're going to use the attribution visualizer object and we'll configure it with our model a scoring function for the model's outputs here softmax a list of the classes the model recognizes here i'm stripping out an ordered list of the imagenet class names we'll tell it that we're looking at image features captive insights also handles text and arbitrary data as well and we'll give it a data set which is just an iterable that returns a batch of images and labels note that we haven't specified an algorithm or a visualization method these are things that you set up in the in browser widget now we ask the visualizer to render it starts off empty but we can set up configuration parameters and ask it to fetch our visualized attributions with the fetch button i'm going to leave things at the default setting for integrated gradients captain needs a few minutes to generate the attributions but now we can see that it ranks the first few predictions for each image with their probabilities and provides heat map attribution for the important regions of the image in this way captain insights lets you experiment with attribution methods and understand the activity that led to your model's predictions both correct and incorrect and lets you do it visually with minimal code finally don't forget to look at captain.ai for documentation tutorials an api reference and access to the source on github you

Original Description

Captum is an open source, extensible library for model interpretability built on PyTorch. This video covers the various methods of explaining model output and layer activity ("attribution"), and how to use Captum Insights, an interpretability visualization widget built on top of Captum to facilitate model understanding. Download all files and notebook here:https://pytorch-tutorial-assets.s3.amazonaws.com/youtube-series/video7.zip Download the Getting Started with Captum notebook here: https://pytorch-tutorial-assets.s3.amazonaws.com/youtube-series/video7/Getting-Started-with-Captum.ipynb
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from PyTorch · PyTorch · 0 of 60

← Previous Next →
1 What is PyTorch?
What is PyTorch?
PyTorch
2 PyTorch Tutorial: A Quick Preview
PyTorch Tutorial: A Quick Preview
PyTorch
3 PyTorch Summer Hackathon 2019
PyTorch Summer Hackathon 2019
PyTorch
4 Tips and Tricks on Hacking with PyTorch: A Quick Tutorial by Brad Heintz
Tips and Tricks on Hacking with PyTorch: A Quick Tutorial by Brad Heintz
PyTorch
5 PyTorch 1.2 and PyTorch Hub: A Quick Introduction by Soumith Chintala and Ailing Zhang
PyTorch 1.2 and PyTorch Hub: A Quick Introduction by Soumith Chintala and Ailing Zhang
PyTorch
6 Torchtext 0.4 with Supervised Learning Datasets: A Quick Introduction by George Zhang
Torchtext 0.4 with Supervised Learning Datasets: A Quick Introduction by George Zhang
PyTorch
7 Torchaudio 0.3 with Kaldi Compatibility, New Transforms: A Quick Introduction by Jason Lian
Torchaudio 0.3 with Kaldi Compatibility, New Transforms: A Quick Introduction by Jason Lian
PyTorch
8 Torchvision 0.4 with Support for Video: A Quick Introduction by Francisco Massa
Torchvision 0.4 with Support for Video: A Quick Introduction by Francisco Massa
PyTorch
9 Introduction to Machine Learning for Developers at F8 2019
Introduction to Machine Learning for Developers at F8 2019
PyTorch
10 Powered by PyTorch at F8 2019
Powered by PyTorch at F8 2019
PyTorch
11 Developing and Scaling AI Experiences at Facebook with PyTorch at F8 2019
Developing and Scaling AI Experiences at Facebook with PyTorch at F8 2019
PyTorch
12 New Approaches to Image and Video Reconstruction Using Deep Learning at Facebook at F8 2019
New Approaches to Image and Video Reconstruction Using Deep Learning at Facebook at F8 2019
PyTorch
13 PyTorch Developer Conference 2018: Recap
PyTorch Developer Conference 2018: Recap
PyTorch
14 PyTorch Developer Conference 2018: Keynote & Deep Dive
PyTorch Developer Conference 2018: Keynote & Deep Dive
PyTorch
15 PyTorch Developer Conference 2018: Production & Research Sessions
PyTorch Developer Conference 2018: Production & Research Sessions
PyTorch
16 PyTorch Developer Conference 2018: Cloud & Academia Sessions
PyTorch Developer Conference 2018: Cloud & Academia Sessions
PyTorch
17 PyTorch Developer Conference 2018: Enterprise, Education, & Future of AI Panel
PyTorch Developer Conference 2018: Enterprise, Education, & Future of AI Panel
PyTorch
18 PyTorch Developer Conference 2019 | Full Livestream
PyTorch Developer Conference 2019 | Full Livestream
PyTorch
19 PyTorch Developer Conference 2019: Recap
PyTorch Developer Conference 2019: Recap
PyTorch
20 PyTorch Developer Conference Keynote - Mike Schroepfer
PyTorch Developer Conference Keynote - Mike Schroepfer
PyTorch
21 What’s new in PyTorch 1.3 - Lin Qiao
What’s new in PyTorch 1.3 - Lin Qiao
PyTorch
22 PyTorch Front-End Features: Named Tensors and Type Promotion - Gregory Chanan
PyTorch Front-End Features: Named Tensors and Type Promotion - Gregory Chanan
PyTorch
23 Research to Production: PyTorch JIT/TorchScript Updates - Michael Suo
Research to Production: PyTorch JIT/TorchScript Updates - Michael Suo
PyTorch
24 Quantization - Dmytro Dzhulgakov
Quantization - Dmytro Dzhulgakov
PyTorch
25 PyTorch ONNX Export Support - Lara Haidar, Microsoft
PyTorch ONNX Export Support - Lara Haidar, Microsoft
PyTorch
26 Apex -  Michael Carilli, NVIDIA
Apex - Michael Carilli, NVIDIA
PyTorch
27 Dataloader Design for PyTorch - Tongzhou Wang, MIT
Dataloader Design for PyTorch - Tongzhou Wang, MIT
PyTorch
28 Linear Algebra in PyTorch - Vishwak Srinivasan, CMU
Linear Algebra in PyTorch - Vishwak Srinivasan, CMU
PyTorch
29 PyTorch Mobile - David Reiss
PyTorch Mobile - David Reiss
PyTorch
30 Model Interpretability with Captum - Narine Kokhilkyan
Model Interpretability with Captum - Narine Kokhilkyan
PyTorch
31 Detectron2 - Next Gen Object Detection Library - Yuxin Wu
Detectron2 - Next Gen Object Detection Library - Yuxin Wu
PyTorch
32 Speech Extensions to Fairseq - Dmytro Okhonko
Speech Extensions to Fairseq - Dmytro Okhonko
PyTorch
33 PyTorch on Google Cloud TPUs - Google, Salesforce, Facebook
PyTorch on Google Cloud TPUs - Google, Salesforce, Facebook
PyTorch
34 PyTorch Summer Hackathon Winners - Joe Spisak, Sebastien Arnold, Tristan Deleu
PyTorch Summer Hackathon Winners - Joe Spisak, Sebastien Arnold, Tristan Deleu
PyTorch
35 PyTorch in Robotics - Yisong Yue, Caltech
PyTorch in Robotics - Yisong Yue, Caltech
PyTorch
36 StanfordNLP - Yuhao Zhang, Stanford
StanfordNLP - Yuhao Zhang, Stanford
PyTorch
37 Sotabench for Reproducible Research - Robert Stojnic, Papers with Code
Sotabench for Reproducible Research - Robert Stojnic, Papers with Code
PyTorch
38 Collaborative Natural Language Inference - Sasha Rush, Cornell
Collaborative Natural Language Inference - Sasha Rush, Cornell
PyTorch
39 Privacy Preserving AI - Andrew Trask, OpenMined
Privacy Preserving AI - Andrew Trask, OpenMined
PyTorch
40 CrypTen - Laurens van der Maaten
CrypTen - Laurens van der Maaten
PyTorch
41 PyTorch at Uber - Sidney Zhang, Uber
PyTorch at Uber - Sidney Zhang, Uber
PyTorch
42 PyTorch at Tesla - Andrej Karpathy, Tesla
PyTorch at Tesla - Andrej Karpathy, Tesla
PyTorch
43 PyTorch at Microsoft - Saurabh Tiwary, Microsoft
PyTorch at Microsoft - Saurabh Tiwary, Microsoft
PyTorch
44 PyTorch at Dolby Labs - Vivek Kumar, Dolby Labs
PyTorch at Dolby Labs - Vivek Kumar, Dolby Labs
PyTorch
45 PyTorch Developer Conference 2019 - Panel Discussion
PyTorch Developer Conference 2019 - Panel Discussion
PyTorch
46 Using deep learning and PyTorch to power next gen aircraft at Caltech
Using deep learning and PyTorch to power next gen aircraft at Caltech
PyTorch
47 Named Tensors, Model Quantization, and the Latest PyTorch Features - Part 1
Named Tensors, Model Quantization, and the Latest PyTorch Features - Part 1
PyTorch
48 TorchScript and PyTorch JIT | Deep Dive
TorchScript and PyTorch JIT | Deep Dive
PyTorch
49 Announcing the PyTorch Global Summer Hackathon 2020
Announcing the PyTorch Global Summer Hackathon 2020
PyTorch
50 Opening Up the Black Box: Model Understanding with Captum and PyTorch
Opening Up the Black Box: Model Understanding with Captum and PyTorch
PyTorch
51 PyTorch Mobile Runtime for Android
PyTorch Mobile Runtime for Android
PyTorch
52 Torchvision in 5 minutes
Torchvision in 5 minutes
PyTorch
53 3D Deep Learning with PyTorch3D
3D Deep Learning with PyTorch3D
PyTorch
54 What is Torchtext?
What is Torchtext?
PyTorch
55 TorchAudio: A Quick Intro
TorchAudio: A Quick Intro
PyTorch
56 PyTorch Mobile Runtime for iOS
PyTorch Mobile Runtime for iOS
PyTorch
57 PySlowFast: Deep learning with Video
PySlowFast: Deep learning with Video
PyTorch
58 PyTorch Pruning | How it's Made by Michela Paganini
PyTorch Pruning | How it's Made by Michela Paganini
PyTorch
59 Measuring Fairness in Machine Learning Systems
Measuring Fairness in Machine Learning Systems
PyTorch
60 PyTorch for Hackathons
PyTorch for Hackathons
PyTorch

This video teaches model interpretability using Captum, a PyTorch library, and demonstrates how to use feature attribution, layer attribution, and Captum Insights for visualization, enabling users to understand and improve their models' performance.

Key Takeaways
  1. Import attribution methods and visualization tools from Captum
  2. Get a pre-trained image classifier ResNet trained against the ImageNet dataset
  3. Define image transforms to prepare the image for consumption by the model
  4. Create an Integrated Gradients object and call the attribute method
  5. Use Captum Insights to configure attribution algorithms and fetch visualized attributions
💡 Captum provides a powerful toolkit for model interpretability, enabling users to understand how their models make predictions and improve their performance with minimal code.

Related Reads

Up next
What is Telematics Explained with Examples
VLR Software Training
Watch →