Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeepExplainer fails for PyTorch Pretrained Alexnet Model #593

Closed
kracekumar opened this issue May 16, 2019 · 3 comments
Closed

DeepExplainer fails for PyTorch Pretrained Alexnet Model #593

kracekumar opened this issue May 16, 2019 · 3 comments
Labels
stale Indicates that there has been no recent activity on an issue

Comments

@kracekumar
Copy link

from torchvision import transforms
import torchvision.models as models

from torch.utils.data.dataset import Dataset
from torch.utils.data import DataLoader

alexnet = models.alexnet(pretrained=True).eval()
transforms = transforms.Compose([
            transforms.ToTensor(),
            #transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
        ])
dataloader = DataLoader(ImageDataset('images', transforms=transforms), batch_size=16)
batch = next(iter(dataloader))
images, _ = batch
e = shap.DeepExplainer(alexnet, images[0:1])
shap_values = e.shap_values(images[1:])

The output of the e.shap_values call is below

Warning: unrecognized nn.Module: <class 'type'>
Warning: unrecognized nn.Module: <class 'type'>
Warning: unrecognized nn.Module: <class 'type'>
...

The architecture of alexnet

print(alexnet)
AlexNet(
  (features): Sequential(
    (0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))
    (1): ReLU(inplace)
    (2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
    (3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
    (4): ReLU(inplace)
    (5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
    (6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (7): ReLU(inplace)
    (8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (9): ReLU(inplace)
    (10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (11): ReLU(inplace)
    (12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(6, 6))
  (classifier): Sequential(
    (0): Dropout(p=0.5)
    (1): Linear(in_features=9216, out_features=4096, bias=True)
    (2): ReLU(inplace)
    (3): Dropout(p=0.5)
    (4): Linear(in_features=4096, out_features=4096, bias=True)
    (5): ReLU(inplace)
    (6): Linear(in_features=4096, out_features=1000, bias=True)
  )
)
# Custom dataset definition
from PIL import Image
class ImageDataset(Dataset):
    def __init__(self, path, transforms):
        self.path = Path(path)
        self.items = list(self.path.glob('*.jpg'))
        self.transforms = transforms

    def __getitem__(self, index):
        path = self.items[index]
        im = Image.open(path).convert('RGB')
        im = np.asarray(im.resize((227,227)))
        return self.transforms(im), path.name
    
    def __len__(self):
        return len(self.items)

transforms = transforms.Compose([
            transforms.ToTensor(),
            #transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
        ])

Maybe AdaptiveAvgPool2d used in alexnet architecture is missing op_handlers. This happens for pre trained models like vgg16, resnet, inception_v3.

What could be the reason for the failure?

@gabrieltseng
Copy link
Collaborator

Hi!

As you noted, the warning you are getting is to let you know one of the modules is not explicitly supported by the deep explainer. The warning had a typo that was fixed, and should now let you know which module caused the error. When this happens, the default is to not change the gradient calculated by PyTorch.

The warning will be thrown by the adaptive average pooling, as you pointed out, but also for the features and classifier modules.

Note that the warning is intentional, and doesn't necessarily mean the deep explainer failed:

For the features and classifier, the warning isn't a problem since the deep explainer will correctly backpropagate though their child modules (and PyTorch will just take that correctly calculated gradient).

I'm not familiar with adaptive average pooling, but the correct way for the deep explainer to backpropagate through an average pooling layer also to leave the gradient unchanged, since it is linear. This means the default behaviour might lead to correct shap values, despite the warnings.

You are welcome to add support for adaptive average pooling! If not, I can do it so that it is explicitly supported.

Copy link

This issue has been inactive for two years, so it's been automatically marked as 'stale'.

We value your input! If this issue is still relevant, please leave a comment below. This will remove the 'stale' label and keep it open.

If there's no activity in the next 90 days the issue will be closed.

@github-actions github-actions bot added the stale Indicates that there has been no recent activity on an issue label Dec 18, 2023
Copy link

This issue has been automatically closed due to lack of recent activity.

Your input is important to us! Please feel free to open a new issue if the problem persists or becomes relevant again.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Indicates that there has been no recent activity on an issue
Projects
None yet
Development

No branches or pull requests

2 participants