Please help with implementing GradCAM

Hi everyone,

I have been trying to implement the PyTorch GradCAM here

My code is below:

# Construct the CAM object once, and then re-use it on many images:
cam = GradCAM(model=effnet_model, target_layers=target_layers, use_cuda=True)

# In this example, we want to generate a GradCAM visualization for each image in the input tensor,
# so we set targets to be a list of length 64, where each element is a BinaryClassifierOutputTarget with
# a target class of 0
targets = [BinaryClassifierOutputTarget(1) for _ in range(64)]

grayscale_cam = cam(input_tensor=input_tensor, targets=targets)

# In this example, grayscale_cam has 64 images in the batch, one for each image in the input tensor
# We will iterate over each image and generate a visualization for it
for i in range(grayscale_cam.shape[0]):
    grayscale_cam_i = grayscale_cam[i, :]
    visualization = show_cam_on_image(rgb_img[i], grayscale_cam_i, use_rgb=True)

I get the following error:

RuntimeError: grad can be implicitly created only for scalar outputs

My input tensor code looks this way:

from pytorch_grad_cam import GradCAM, HiResCAM, ScoreCAM, GradCAMPlusPlus, AblationCAM, XGradCAM, EigenCAM, FullGrad
from pytorch_grad_cam.utils.model_targets import BinaryClassifierOutputTarget
from pytorch_grad_cam.utils.image import show_cam_on_image

target_layers = [effnet_model.features[-1]]
input_tensor,label = next(iter(train_loader))
input_tensor,label = torch.tensor(input_tensor), torch.tensor(label),

Please any thoughts on the problem? Thanks

Just wanted to bump this in case anyone had ideas. Thanks