captum icon indicating copy to clipboard operation
captum copied to clipboard

RuntimeError with GuidedGradCam

Open Mark-Dou opened this issue 3 years ago • 2 comments

Hi, I want to apply the GuidedGradCam with ResNet50: model = models.resnet50(pretrained=True) model.eval() input = torch.rand(2, 3, 224, 224) ig = GuidedGradCam(model, model.layer4) attribution = ig.attribute(input, 1) but get this error: File "/home/hanyiwang/tools/visualization/captum/captum/_utils/common.py", line 463, in _run_forward else inputs File "/home/hanyiwang/.conda/envs/pre/lib/python3.6/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl return forward_call(*input, **kwargs) File "/home/hanyiwang/.conda/envs/pre/lib/python3.6/site-packages/torchvision/models/resnet.py", line 249, in forward return self._forward_impl(x) File "/home/hanyiwang/.conda/envs/pre/lib/python3.6/site-packages/torchvision/models/resnet.py", line 234, in _forward_impl x = self.relu(x) File "/home/hanyiwang/.conda/envs/pre/lib/python3.6/site-packages/torch/nn/modules/module.py", line 1120, in call_impl result = forward_call(*input, **kwargs) File "/home/hanyiwang/.conda/envs/pre/lib/python3.6/site-packages/torch/nn/modules/activation.py", line 98, in forward return F.relu(input, inplace=self.inplace) File "/home/hanyiwang/.conda/envs/pre/lib/python3.6/site-packages/torch/nn/functional.py", line 1297, in relu result = torch.relu(input)

RuntimeError: Output 0 of BackwardHookFunctionBackward is a view and is being modified inplace. This view was created inside a custom Function (or because an input was returned as-is) and the autograd logic to handle view+inplace would override the custom backward associated with the custom Function, leading to incorrect gradients. This behavior is forbidden. You can fix this by cloning the output of the custom Function.

Maybe the error of ReLu in target, but i don't know how to rectify it, could you give me some advice? Thanks!

Mark-Dou avatar Jun 11 '22 09:06 Mark-Dou

Recently I have changed torchvision from 0.8.2 to 0.12.0 and torch from 1.7.1 to 1.11.0 Gradcam and backdrop were working fine with previous versions, but I am also experiencing the same issue with new updates. I see some discussions on this issue https://discuss.pytorch.org/t/getting-this-warning-output-0-of-backwardhookfunctionbackward-is-a-view-and-is-being-modified-inplace/122766

There should be some better way to resolve it. It will be great if the captum team provides any ideas!

vadthyavath avatar Jul 05 '22 07:07 vadthyavath

https://github.com/pytorch/captum/blob/95ac7988641594a5bc4a7eb55344d4c7867cbf53/tutorials/LRP_TorchVision_Tutorial.ipynb As mentioned in the above notebook, making inplace to false like the following resolved my issue!

for module in model.modules():
    if isinstance(module, torch.nn.ReLU):
        module.inplace=False

vadthyavath avatar Jul 05 '22 09:07 vadthyavath