AttributeError: denorm (GradCam)

Hi there! Maybe someone can help me with this problem:

I want to dive into CNN activation visualizations, eg GradCam. I tried to code along lesson 6 of fastai v3 part 1, and I tried to use the code from @quan.tran

However, I always get the following error (fastai 1.0.60, torch1.4.0)

I checked the internal fastai method for gradcam, and it doesn’t seem to use denorm:

xb,_ =, detach=False, denorm=False)

However, I didn’t get this to run with my own or Quan’s method.

Any help is appreciated, total beginner here :slight_smile: Thank you!


1 Like

Hi @johannesstutz! Firstly thanks for trying out my code I appreciate it!

So the problem is the library version. My gradcam code was tested using fastai 1.0.55 where denorm was a function of fastai Dataset(?) object. In version 1.0.66, I believe it has been integrated as function argument for function one_item. My guess is you can change these lines from:

xb._ = data_cln.one_item(x)
xb_im = Image(data_cln.denorm(xb)[0])


xb._ = data_cln.one_item(x,denorm=False)
xb_im = Image(data_cln.one_item(x,denorm=True))

Haven’t tested it yet but I think it should at least solve the AttributeError issue.

1 Like

Well, thank you for providing the code!

I tried again today, and I’m a little confused but happy: your implementation is running fine now (still on fastai 1.0.60). Maybe I messed something else up the last time.

I’ll play around a little more. Do you know if there’s an easy way to visualize other layers than the last one? The goal being a higher res version of the heatmap.

Cheers! Johannes

1 Like

Glad it works!
About the visualization of other layers, I answered a similar question regarding to this here, you can take a look: GradCam and Guided Backprop intergration in Fastai library

In short, at least for resnet model, learn.model[0][-1][-1] (aka m[0][-1][-1] in the code) is the last pooling layer of the model’s body to perform the gradcam on, so the heatmap produced at this layer should highlight the best part of your image for classification task (though I am not sure if this is equivalent to having the higher res version). To visualize other layers, you can play with x and y in m[0][x][y]