captum icon indicating copy to clipboard operation
captum copied to clipboard

Bug: error with retrieving input dim in guided_grad_cam.py?

Open ybzhaoplus opened this issue 3 years ago • 5 comments

https://github.com/pytorch/captum/blob/43acfcd320c44e8e469bf5276af9d55cf62145aa/captum/attr/_core/guided_grad_cam.py#L211

If I understand corretly, the parameter here should be: inputs[i].shape[1:], rather then inputs[i].shape[2:]

For inputs, the shape is N-C-H-W, For inputs[i], the shape is C-H-W, thus its dim (H-W) could be inputs[i].shape[1:]

As when I run with Guided GradCAM, it keeps warning: "Couldn't appropriately interpolate GradCAM attributions for some input tensors, returning empty tensor for corresponding attributions."

ybzhaoplus avatar May 08 '21 02:05 ybzhaoplus

Hi @ybzhaoplus,

thank you for commenting on the implementation!

The current implementation should correctly index the H and W dimensions, because an input tensor is first converted into a tuple: https://github.com/pytorch/captum/blob/43acfcd320c44e8e469bf5276af9d55cf62145aa/captum/_utils/common.py#L153

Is it possible that some of the tensors you are passing have a dimensionality less than 4? Or maybe Hope this helps

bilalsal avatar May 09 '21 01:05 bilalsal

Thanks for getting back.

I checked the inputs, and the dimensionality is 4.

I bypass this step by implementing guided_grad_cam by myself with guided_backprop and gradcam in captum. I use inputs.shape[2:], and it works.

ybzhaoplus avatar May 09 '21 01:05 ybzhaoplus

Thanks for the clarification, @ybzhaoplus.

We are interested in understanding this issue further. Do you mind sharing a Colab notebook illustrating the exception and the warning you got?

Thanks!

bilalsal avatar May 09 '21 22:05 bilalsal

@ybzhaoplus is this still an open issue ?

NarineK avatar Jul 09 '21 05:07 NarineK

I can't get Guided GradCAM to work in Captum 0.6.0 either, although GradCAM itself works perfectly fine. I tried different input shapes (with and without batch dimension) and interpolation modes, but it always results in the following error: UserWarning: Couldn't appropriately interpolate GradCAM attributions for some input tensors, returning empty tensor for corresponding attributions.

My network is composed of 3 conv3d+maxpool+relu+batchnorm blocks followed some linear layers.

Tianmaru avatar Jan 28 '23 22:01 Tianmaru