How to run Captum?

username: sampa
cooked: <pre><code class="lang-auto">import os

import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from torchvision import datasets, transforms, models
from captum.insights import AttributionVisualizer, Batch
from captum.insights.features import ImageFeature

data_dir = "./BW"
test_dir=data_dir + '/Test'

def get_classes():
    classes = [
        "B",
        "M",
    ]
    return classes

# Define functions for classification classes and pretrained model.
def get_pretrained_model():
    model = models.resnet18(pretrained=True)
    num_ftrs = model.fc.in_features
    model.fc = nn.Linear(num_ftrs, 2)
    model.load_state_dict(torch.load('checkpoint_ResnetBW.pt'))

def baseline_func(input):
    return input * 0

def formatted_data_iter():
    dataset = datasets.ImageFolder(test_dir, transform=transforms.ToTensor()
    )
    dataloader = iter(
        torch.utils.data.DataLoader(dataset, batch_size=4, shuffle=False, num_workers=2)
    )
    while True:
        images, labels = next(dataloader)
        yield Batch(inputs=images, labels=labels)

# Run the visualizer and render inside notebook for interactive debugging.


normalize = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
model = get_pretrained_model()
visualizer = AttributionVisualizer(
    models=[model],
    score_func=lambda o: torch.nn.functional.softmax(o, 1),
    classes=get_classes(),
    features=[
        ImageFeature(
            "Photo",
            baseline_transforms=[baseline_func],
            input_transforms=[normalize],
        )
    ],
    dataset=formatted_data_iter(),
)
visualizer.render()
</code></pre>
<p>I am getting output like</p>
<pre><code class="lang-auto">Fetch data and view Captum Insights at http://localhost:64328/

&lt;IPython.lib.display.IFrame object at 0x0000022B8CEA3248&gt;

</code></pre>
<p>The link is like <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/c/2c095b750ea695bf480e5ff1b97515003c83005b.png" data-download-href="https://discuss.pytorch.org/uploads/default/2c095b750ea695bf480e5ff1b97515003c83005b" title="Capture"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/c/2c095b750ea695bf480e5ff1b97515003c83005b_2_690x139.png" alt="Capture" data-base62-sha1="6hz3vhI455fYVWLKoC1xxme5ldF" width="690" height="139" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/c/2c095b750ea695bf480e5ff1b97515003c83005b_2_690x139.png, https://discuss.pytorch.org/uploads/default/optimized/3X/2/c/2c095b750ea695bf480e5ff1b97515003c83005b_2_1035x208.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/c/2c095b750ea695bf480e5ff1b97515003c83005b_2_1380x278.png 2x" data-dominant-color="F7F7F7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Capture</span><span class="informations">1917×388 28.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Narine_Kokhlikyan
cooked: <p>Hi <a class="mention" href="/u/sampa">@sampa</a>, you can use captum with and without Insights (interactive tool).<br>
If you want to use it without interactivity, then you can try our tutorials and examples on the README page: <a href="https://github.com/pytorch/captum" rel="nofollow noopener">https://github.com/pytorch/captum</a></p>
<p>For the insights, I believe that there used to be an option <code>visualizer.render(debug=True)</code>. This will help you to see the error log.</p>
<p>Also, in which environment are you trying to run insights ?</p>

username: sampa
cooked: <p>Hello <a class="mention" href="/u/narine_kokhlikyan">@Narine_Kokhlikyan</a>.  Thanks for your reply. I saw your tutorials.I gave the snapshot of my code. Can you please check it and tell me where is my mistake?</p>
<p>I am using Python 3.7 and Anaconda.<br>
I want the result like this<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/1/e13b7d8765ac0af47834e75b9c7ae464f2c2a79d.png" data-download-href="https://discuss.pytorch.org/uploads/default/e13b7d8765ac0af47834e75b9c7ae464f2c2a79d" title="captum_insights_screenshot"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/1/e13b7d8765ac0af47834e75b9c7ae464f2c2a79d_2_690x400.png" alt="captum_insights_screenshot" data-base62-sha1="w8uT4KqQCMeGBa17zob2OYj7eih" width="690" height="400" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/1/e13b7d8765ac0af47834e75b9c7ae464f2c2a79d_2_690x400.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/1/e13b7d8765ac0af47834e75b9c7ae464f2c2a79d_2_1035x600.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/1/e13b7d8765ac0af47834e75b9c7ae464f2c2a79d.png 2x" data-dominant-color="E7E9EB"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">captum_insights_screenshot</span><span class="informations">1274×739 116 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Narine_Kokhlikyan
cooked: <p>Hi <a class="mention" href="/u/sampa">@sampa</a>, did you try <code>visualizer.render(debug=True)</code> ?  Did you see any error logs ? You might also try the latest version.</p>

username: bilalsal
cooked: <p>Hi <a class="mention" href="/u/sampa">@sampa</a>. As <a class="mention" href="/u/narine_kokhlikyan">@Narine_Kokhlikyan</a> mentioned, we can help you more if we see your error logs.<br>
Maybe you can check if your browser ran into an error while loading Captum Insights (just guessing from the screenshot in your first post)?<br>
To check about this, right-click the webpage in Chrome or Firefox, and click Inspect, then check the Console tab for errors or warnings.</p>
<p>Hope this helps</p>

username: edwardio
cooked: <p>One issue I’ve noticed is that your <code>get_pretrained_model</code> function does not return any model.</p>
<p>Like <a class="mention" href="/u/narine_kokhlikyan">@Narine_Kokhlikyan</a> mentioned, you should change your last line to be <code>visualizer.render(debug=True)</code>  to see the Python errors. In the next version of Captum this will be turned on by default.</p>

username: sampa
cooked: <p>Thank you <a class="mention" href="/u/bilalsal">@bilalsal</a> <a class="mention" href="/u/narine_kokhlikyan">@Narine_Kokhlikyan</a> <a class="mention" href="/u/edwardio">@edwardio</a>   for your kind reply. It is working without any error. But every time It is predicted as first class. Here is the latest  snapshot of my code</p>
<pre><code class="lang-auto">
import os
from captum.insights import AttributionVisualizer, Batch
from captum.insights.features import ImageFeature
from torchvision import datasets, transforms, models
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
test_transforms = transforms.Compose([
                    transforms.Resize((224,224)),
                    transforms.ToTensor(),
                    ])

def get_classes():
    classes = [
        "B",
        "M",
    ]
    return classes


def get_pretrained_model():
    net= models.resnet18(pretrained=True)
    num_ftrs = net.fc.in_features
    net.fc = nn.Linear(num_ftrs, 2)
    pt_path = os.path.abspath(
        os.path.dirname(__file__) + "/models/checkpoint.pt"
    )
    net.load_state_dict(torch.load(pt_path))
    return net


def baseline_func(input):
    return input * 0


def formatted_data_iter():
    dataset = datasets.ImageFolder(
        root="data/test", transform=test_transforms
    )
    dataloader = iter(
        torch.utils.data.DataLoader(dataset, batch_size=4, shuffle=False, num_workers=2)
    )
    while True:
        images, labels = next(dataloader)
        yield Batch(inputs=images, labels=labels)


if __name__ == "__main__":
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    model = get_pretrained_model()
    visualizer = AttributionVisualizer(
        models=[model],
        score_func=lambda o: torch.nn.functional.softmax(o, 1),
        classes=get_classes(),
        features=[
            ImageFeature(
                "Photo",
                baseline_transforms=[baseline_func],
                input_transforms=[normalize],
            )
        ],
        dataset=formatted_data_iter(),
    )

    visualizer.render(debug=True)

</code></pre>
<p>In data/test folder I have 2 sub-folders name B and M with corresponding images.</p>

username: nareshr8
cooked: <p>When I try Captum Insights, It seems not to render like:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/6/d6b2761646f1a26b900c2de38c7829e03cb26e2c.png" data-download-href="https://discuss.pytorch.org/uploads/default/d6b2761646f1a26b900c2de38c7829e03cb26e2c" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/6/d6b2761646f1a26b900c2de38c7829e03cb26e2c_2_690x73.png" alt="image" data-base62-sha1="uDiwS5pfE0Ds7Bg1DX7fBd4M5Kk" width="690" height="73" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/6/d6b2761646f1a26b900c2de38c7829e03cb26e2c_2_690x73.png, https://discuss.pytorch.org/uploads/default/optimized/3X/d/6/d6b2761646f1a26b900c2de38c7829e03cb26e2c_2_1035x109.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/6/d6b2761646f1a26b900c2de38c7829e03cb26e2c.png 2x" data-dominant-color="F8F8F8"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1165×124 7.02 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>When I tried to look at the browser console, It shows this error:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/9/1/91bd69e240ac6a296663a5c07d6f120661702b70.png" alt="image" data-base62-sha1="kNh36cnvgQpFLVuEf4sbpqXBtAY" width="555" height="340"></p>
<p>I tried in Vivaldi and Firefox. Both yield the same result. Can someone help…</p>

username: nareshr8
cooked: <p>I was able to resolve this issue. I actually was running each environment as a kernel in Jupyter notebook so as to easily switch environment. I had to install captum in the base environment as well to make this work.</p>

username: marie
cooked: <p>If I run my Code, I get the Output…</p>
<p>Captum Insights widget created.<br>
CaptumInsights(insights_config={‘classes’: [‘Class1’, ‘Class2’], ‘methods’: [‘Deconvolution’, ‘Deep Lift’, ‘Guided Backprop’, ‘Input X Gradient’, ‘Integrated Gradients’, ‘Saliency’, ‘Feature Ablation’, ‘Occlusion’], ‘method_arguments’: {‘Integrated Gradients’: {‘n_steps’: {‘value’: 25, ‘limit’: (2, None), ‘type’: ‘number’}, ‘method’: {‘value’: ‘gausslegendre’, ‘limit’: [‘riemann_left’, ‘riemann_right’, ‘riemann_middle’, ‘riemann_trapezoid’, ‘gausslegendre’], ‘type’: ‘enum’}}, ‘Feature Ablation’: {‘perturbations_per_eval’: {‘value’: 1, ‘limit’: (1, 100), ‘type’: ‘number’}}, ‘Occlusion’: {‘sliding_window_shapes’: {‘value’: ‘’, ‘type’: ‘string’}, ‘strides’: {‘value’: ‘’, ‘type’: ‘string’}, ‘perturbations_per_eval’: {‘value’: 1, ‘limit’: (1, 100), ‘type’: ‘number’}}}, ‘selected_method’: ‘Integrated Gradients’})<br>
Output()</p>
<p>… but I don’t get a link to display the Widget in my browser. Any ideas? I’m using Python 3.7 with Anaconda.<br>
In the GitHub ReadMe it says:</p>
<p>To build the widget from a checkout in a conda environment run</p>
<pre><code class="lang-auto">conda install -c conda-forge yarn
BUILD_INSIGHTS=1 python setup.py develop
</code></pre>
<p>… but I can’t run the “BUILD_INSIGHTS=1 python setup.py develop”. Maybe that’s the problem, but I still don’t know how to resolve it.</p>
<p>Thanks in advance <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>