# Captum


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

In all this notebook, we will use the following data:

``` python
from fastai.vision.all import *
```

``` python
path = untar_data(URLs.PETS)/'images'
fnames = get_image_files(path)
def is_cat(x): return x[0].isupper()
dls = ImageDataLoaders.from_name_func(
    path, fnames, valid_pct=0.2, seed=42,
    label_func=is_cat, item_tfms=Resize(128))
```

``` python
from random import randint
```

``` python
learn = vision_learner(dls, resnet34, metrics=error_rate)
learn.fine_tune(1)
```

# Captum Interpretation

The Distill Article
[here](https://distill.pub/2020/attribution-baselines/) provides a good
overview of what baseline image to choose. We can try them one by one.

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/callback/captum.py#L32"
target="_blank" style="float:right; font-size:smaller">source</a>

### CaptumInterpretation

``` python

def CaptumInterpretation(
    learn, cmap_name:str='custom blue', colors:NoneType=None, N:int=256,
    methods:tuple=('original_image', 'heat_map'), signs:tuple=('all', 'positive'), outlier_perc:int=1
):

```

*Captum Interpretation for Resnet*

## Interpretation

``` python
captum=CaptumInterpretation(learn)
idx=randint(0,len(fnames))
captum.visualize(fnames[idx])
```

![](70c_callback.captum_files/figure-commonmark/cell-7-output-1.png)

``` python
captum.visualize(fnames[idx],baseline_type='uniform')
```

![](70c_callback.captum_files/figure-commonmark/cell-8-output-1.png)

``` python
captum.visualize(fnames[idx],baseline_type='gauss')
```

![](70c_callback.captum_files/figure-commonmark/cell-9-output-1.png)

``` python
captum.visualize(fnames[idx],metric='NT',baseline_type='uniform')
```

![](70c_callback.captum_files/figure-commonmark/cell-10-output-1.png)

``` python
captum.visualize(fnames[idx],metric='Occl',baseline_type='gauss')
```

![](70c_callback.captum_files/figure-commonmark/cell-11-output-1.png)

## Captum Insights Callback

``` python
@patch
def _formatted_data_iter(x: CaptumInterpretation,dl,normalize_func):
    dl_iter=iter(dl)
    while True:
        images,labels=next(dl_iter)
        images=normalize_func.decode(images).to(dl.device)
        yield Batch(inputs=images, labels=labels)
```

------------------------------------------------------------------------

<a
href="https://github.com/fastai/fastai/blob/main/fastai/callback/captum.py#L96"
target="_blank" style="float:right; font-size:smaller">source</a>

### CaptumInterpretation.insights

``` python

def insights(
    x:CaptumInterpretation, inp_data, debug:bool=True
):

```

*Call self as a function.*

``` python
captum=CaptumInterpretation(learn)
captum.insights(fnames)
```

    CaptumInsights(insights_config={'classes': ['False', 'True'], 'methods': ['Deconvolution', 'Deep Lift', 'Guide…

    Output()
