How to run Captum?

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()

I am getting output like

Fetch data and view Captum Insights at http://localhost:64328/

<IPython.lib.display.IFrame object at 0x0000022B8CEA3248>

The link is like

Hi @sampa, you can use captum with and without Insights (interactive tool).
If you want to use it without interactivity, then you can try our tutorials and examples on the README page: https://github.com/pytorch/captum

For the insights, I believe that there used to be an option visualizer.render(debug=True). This will help you to see the error log.

Also, in which environment are you trying to run insights ?

1 Like

Hello @Narine_Kokhlikyan. 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?

I am using Python 3.7 and Anaconda.
I want the result like this

Hi @sampa, did you try visualizer.render(debug=True) ? Did you see any error logs ? You might also try the latest version.

Hi @sampa. As @Narine_Kokhlikyan mentioned, we can help you more if we see your error logs.
Maybe you can check if your browser ran into an error while loading Captum Insights (just guessing from the screenshot in your first post)?
To check about this, right-click the webpage in Chrome or Firefox, and click Inspect, then check the Console tab for errors or warnings.

Hope this helps

1 Like

One issue I’ve noticed is that your get_pretrained_model function does not return any model.

Like @Narine_Kokhlikyan mentioned, you should change your last line to be visualizer.render(debug=True) to see the Python errors. In the next version of Captum this will be turned on by default.

1 Like

Thank you @bilalsal @Narine_Kokhlikyan @edwardio 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


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)

In data/test folder I have 2 sub-folders name B and M with corresponding images.

When I try Captum Insights, It seems not to render like:

When I tried to look at the browser console, It shows this error:
image

I tried in Vivaldi and Firefox. Both yield the same result. Can someone help…

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.

If I run my Code, I get the Output…

Captum Insights widget created.
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’})
Output()

… but I don’t get a link to display the Widget in my browser. Any ideas? I’m using Python 3.7 with Anaconda.
In the GitHub ReadMe it says:

To build the widget from a checkout in a conda environment run

conda install -c conda-forge yarn
BUILD_INSIGHTS=1 python setup.py develop

… 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.

Thanks in advance :slight_smile: