Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

gradcam.py have a bug #11895

Description

@whl33886

Error Message:

conv_out should be multiplied by the average of the conv_out_grad in the reference paper. but conv_out be multiplied by the average of the imggrad in your gradcam.py.

What have you tried to solve it?

def get_cam(conv_out_grad, conv_out):
"""Compute CAM. Refer section 3 of https://arxiv.org/abs/1610.02391 for details"""
weights = np.mean(conv_out_grad, axis=(1, 2))
cam = np.ones(conv_out.shape[1:], dtype=np.float32)
for i, w in enumerate(weights):
cam += w * conv_out[i, :, :]

#=============================
# Put this line of code below
#cam = cv2.resize(cam, (imggrad.shape[1], imggrad.shape[2]))
#=============================

cam = np.maximum(cam, 0)
cam = (cam - np.min(cam)) / (np.max(cam) - np.min(cam)) 
cam = np.uint8(cam * 255)
return cam

def visualize(net, preprocessed_img, orig_img, conv_layer_name):
# Returns grad-cam heatmap, guided grad-cam, guided grad-cam saliency
imggrad = get_image_grad(net, preprocessed_img)
conv_out, conv_out_grad = get_conv_out_grad(net, preprocessed_img, conv_layer_name=conv_layer_name)

#===========================
#imggrad replaced by conv_out_grad
cam = get_cam(conv_out_grad, conv_out)
#Add this line
cam = cv2.resize(cam, (imggrad.shape[1], imggrad.shape[2]))
#===================================

ggcam = get_guided_grad_cam(cam, imggrad)
img_ggcam = grad_to_image(ggcam)

img_heatmap = get_img_heatmap(orig_img, cam)

ggcam_gray = to_grayscale(ggcam)
img_ggcam_gray = np.squeeze(grad_to_image(ggcam_gray))

return img_heatmap, img_ggcam, img_ggcam_gray

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions