Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GradCAM Heatmap Inverse? #300

Open
Boraly opened this issue Jan 15, 2022 · 1 comment
Open

GradCAM Heatmap Inverse? #300

Boraly opened this issue Jan 15, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@Boraly
Copy link

Boraly commented Jan 15, 2022

Hey,

I trained a model to classify polyps. Now I tried to visualize using GradCAM. I copied the code from the tutorial, but I think the heatmap is inverted. I will add a example Image.

My Code:

``directory = '/content/polyp_paris_export/images/'
fnames = list(fname for fname in listdir(directory) if fname.endswith('.png'))
cfg = AutoTransform.from_name('efficientnet_b0')
norm = cfg.transforms[-1]
postprocessing = Normalize(-norm.mean / norm.std, (1.0 / norm.std))
model.eval()
for i in fnames:
img = directory + i
img_1 = Image.open(img)

batch_1 = cfg(img_1).unsqueeze(0)

plt.imshow(img_1)

sa_1 = model.interpret(batch_1, using=SaliencyMap()).show()

grad_1 = model.interpret(batch_1, using=GradCam(), postprocessing=postprocessing).show()
plt.show()
sleep(1)
clear_output(wait=True)
input("Press Enter to continue...")``

image

You can cleary see, that the SaliencyMap recognize the polyps, but the GradCAM just focused on the area around them.

I tried to just invert the Image by using 1-img. But the results doesn't look clear.

How can I fix that?

@Boraly Boraly added the bug Something isn't working label Jan 15, 2022
@FrancescoSaverioZuppichini
Copy link
Owner

Hey @Boraly

Very cool project, I don't know actually the GradCam has always worked for me, I was working on an integration with torchcam[https://github.com/frgfm/torch-cam] but it's not there yet. I would recommend to try with the GradCam there and seeing if there is a problem.

When you create a cam using it you can all pass a reference to the layer, that should make using glasses super easy

from torchcam.methods import GradCAM

cam_extractor = GradCAM(model, [model.encoder])

Let me know, feel free to create a jupyter-notebook sharing your code/issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants