jacobgil / pytorch-grad-cam

Advanced AI Explainability for computer vision. Support for CNNs, Vision Transformers, Classification, Object detection, Segmentation, Image similarity and more.
https://jacobgil.github.io/pytorch-gradcam-book
MIT License
10.06k stars 1.52k forks source link

Tuple Index Out of Range #116

Closed juanpabloalfonzo closed 3 years ago

juanpabloalfonzo commented 3 years ago

Hello, when I try to run the script bellow I get an IndexError: tuple index out of range and I am not quite sure why.

`from pytorch_grad_cam import GradCAM, ScoreCAM, GradCAMPlusPlus, AblationCAM, XGradCAM, EigenCAM from pytorch_grad_cam.utils.image import show_cam_on_image

trans=transforms.ToTensor()

model = model target_layer = model.fc

input_tensor =trans(resize_image(image[14][0]))

input_tensor=input_tensor.unsqueeze(0) input_tensor=input_tensor.to('cuda')

cam = GradCAM(model=model, target_layer=target_layer, use_cuda='args.use_cuda')

target_category = None

grayscale_cam = cam(input_tensor=input_tensor, target_category=target_category)

grayscale_cam = grayscale_cam[0,:] visualization = show_cam_on_image(rgb_img, grayscale_cam)`

I then get the following error traceback:

`IndexError: tuple index out of range

IndexError Traceback (most recent call last)

in 22 23 # You can also pass aug_smooth=True and eigen_smooth=True, to apply smoothing. ---> 24 grayscale_cam = cam(input_tensor=input_tensor, target_category=target_category) 25 26 # In this example grayscale_cam has only one image in the batch: ~/anaconda3/lib/python3.7/site-packages/pytorch_grad_cam/base_cam.py in __call__(self, input_tensor, target_category, aug_smooth, eigen_smooth) 127 128 return self.forward(input_tensor, --> 129 target_category, eigen_smooth) ~/anaconda3/lib/python3.7/site-packages/pytorch_grad_cam/base_cam.py in forward(self, input_tensor, target_category, eigen_smooth) 75 76 cam = self.get_cam_image(input_tensor, target_category, ---> 77 activations, grads, eigen_smooth) 78 79 cam = np.maximum(cam, 0) ~/anaconda3/lib/python3.7/site-packages/pytorch_grad_cam/base_cam.py in get_cam_image(self, input_tensor, target_category, activations, grads, eigen_smooth) 44 grads, 45 eigen_smooth=False): ---> 46 weights = self.get_cam_weights(input_tensor, target_category, activations, grads) 47 weighted_activations = weights[:, :, None, None] * activations 48 if eigen_smooth: ~/anaconda3/lib/python3.7/site-packages/pytorch_grad_cam/grad_cam.py in get_cam_weights(self, input_tensor, target_category, activations, grads) 14 activations, 15 grads): ---> 16 return np.mean(grads, axis=(2, 3)) <__array_function__ internals> in mean(*args, **kwargs) ~/anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py in mean(a, axis, dtype, out, keepdims) 3333 3334 return _methods._mean(a, axis=axis, dtype=dtype, -> 3335 out=out, **kwargs) 3336 3337 ~/anaconda3/lib/python3.7/site-packages/numpy/core/_methods.py in _mean(a, axis, dtype, out, keepdims) 136 137 is_float16_result = False --> 138 rcount = _count_reduce_items(arr, axis) 139 # Make this warning show up first 140 if rcount == 0: ~/anaconda3/lib/python3.7/site-packages/numpy/core/_methods.py in _count_reduce_items(arr, axis) 55 items = 1 56 for ax in axis: ---> 57 items *= arr.shape[ax] 58 return items 59 IndexError: tuple index out of range` The image that I am feeding into it is 3,128,128 in dimension and I have added a 4th dimension with tensor.unsqueeze(0) as it would not be fed into the model properly without this pseudo "batch index". I do not understand which tuple it is finding to be out of range.
jacobgil commented 3 years ago

Hi, I think the issue is target_layer = model.fc The target layer output should have spatial dimensions. The gradient then is 1d and np.mean(grads, axis=(2,3)) fails because axis 2 and 3 (X and Y) don't exist.

juanpabloalfonzo commented 3 years ago

Ah I see yea seems I was trying to pass it a linear layer at the end of the model rather than a convolution layer. Thanks!