ramprs / grad-cam

[ICCV 2017] Torch code for Grad-CAM
https://arxiv.org/abs/1610.02391
1.5k stars 226 forks source link

Can we use GradCAM to visualise the self-trained VGG, e.g., output classes num is 10? #7

Closed HAHA-DL closed 6 years ago

HAHA-DL commented 6 years ago

I want to visualize some interests in images using GradCAM for our self-trained VGG with 10 classes output. But when I converted my VGG weights to caffemodel and used GradCAM setting label -1, I found it predicts some label id out of 0-9, why? Did I use GradCAM mistakenly?

ramprs commented 6 years ago

I am not sure what you mean by setting label to be "-1". The target label that you set for getting Grad-CAM maps should be one among the n (in your case 10) classes that you trained the network for. Also Grad-CAM doesn't modify any part of the network during forward pass - so if your network was trained to classify among 10 classes, the predicted label will range from 0-9 depending on the input.

HAHA-DL commented 6 years ago

Thanks for your reply @ramprs . Setting label to be '-1' is on the command line -label -1 to let the model automatically predict a label for the input image as your design. I.e., the problem I meet is table.insert(model, {'fc8_test', nn.Linear(4096, 10)}) return model My model has the label prediction range in [0, 9], but when I print('shape of output', output:size()) in the classification.lua, the shape of output is '4096' not '10'.

Hopefully, my problem is clear to you now. Can we solve this problem in a way?

ramprs commented 6 years ago

Hi Darren, Sorry I misunderstood your issue. It looks like your model doesn't have a softmax - if that is the case you wouldn't need line 49, cnn:remove() which removes the last layer which is a softmax in the classification example we provided.

Please let me know if that solves your issue.

HAHA-DL commented 6 years ago

@ramprs Thanks a lot. This solves my previous problem then. But can I ask whether GradGAM is able to visualize the hinge loss pretrained model? I.e. the output is 1 instead of 10 or 1000. Can we?

ramprs commented 6 years ago

Yes, it should work for models trained with hinge loss. I think you can implement it by just removing the hinge loss layer and setting the gradient to 1 or -1 (depending on the target class) to the previous layer.