Heatmap feature missing in interp.plot_top_losses() fastai v2

Hi, Does anyone know how I can get back the heatmap feature in fastai 2. It seems to have been removed on the latest version of fastai v2
Example on fastai v1 : interp.plot_top_losses(6,heatmap=True,return_fig = True)

Hi Marvin !
Do you refer to visualization techniques like GradCAM?
I’m new to fastai and I don’t know about this specific function: interp.plot_top_losses(6,heatmap=True,return_fig = True).

Have you watched this post?

Hi Pcamellon,
I did go through the post you have linked and it looks like they are referring to fastai v1 and not related to fastai v2.
But I did see someone sharing a link in that post to a notebook using an external library to get Gradcam visualization.

Still not sure if it’s removed or moved to a different API.

Anyways thanks for trying to help :slightly_smiling_face:

Glad to help!
No need to use an external library. You could try this:

fns = get_image_files(path)
img = fns[1]

show_image(Image.open(img))

img = PILImage.create('data/img.png')
x, = first(dls.test_dl([img]))

class Hook():
    def hook_func(self, m, i, o): self.stored = o.detach().clone()

hook_output = Hook()
hook = learn.model[0].register_forward_hook(hook_output.hook_func)

with torch.no_grad():
    output = learn.model.eval()(x)

act = hook_output.stored[0]

cam_map = torch.einsum('ck,hij->cij', learn.model[1][-1].weight, act)

x_dec = dls.train.decode((x,))[0][0]
_,ax = plt.subplots()
x_dec.show(ctx=ax)
ax.imshow(cam_map[1].detach().cpu(), alpha=0.6, extent=(0,224,224,0), interpolation='bilinear', cmap='magma');

I adapted the code I used for an specific application. Please let me know any errors.

1 Like

Thanks, Pcamellon.
If I’m not wrong this code will only have 1 output of image specified.
how can I get a grid plot of multiple images with top loss or failed prediction? I guess one way could be writing a for loop myself? or is there other fastai api’s I could use?

Thanks

You’re right!

I don’t know much about fastai. I would try with a loop with some images. You could read more about customizing show_image() from fastai or use subplot() from matplotlib/pyplot. Let me know what you tried.

Sure will !!!
Thanks :slight_smile: