Project-MONAI / MONAI

AI Toolkit for Healthcare Imaging
https://monai.io/
Apache License 2.0
5.93k stars 1.09k forks source link

Gradcam for models that return a single value #5530

Closed wyli closed 1 year ago

wyli commented 2 years ago

Discussed in https://github.com/Project-MONAI/MONAI/discussions/5528

Originally posted by **rajanish4** November 15, 2022 Hi there, I am currently running Grad cam class for my binary classification model that returns a single value. I believe, the grad cam compute function requires multiple class labels for it to work. So, i would like to request a modification of this that can be used for such binary classification tasks. Thanks

(the class score function and the subclasses are not compatible with such input models

https://github.com/Project-MONAI/MONAI/blob/5e6f105ff1d40266debfbd9774a9a0bdbe4d817a/monai/visualize/class_activation_maps.py#L125-L126)

thatgeeman commented 2 years ago

Hello 👋 I'd like a go at it if no one else is working on this 🤗

wyli commented 2 years ago

sure, please go ahead, let me know if you need any help

thatgeeman commented 2 years ago

Finally got some time to work on this. Not sure if I've mistaken the intent here, but I'm not able to reproduce the issue. For a binary classifier, I'm setting the out_channels=1 below and everything seems to work.

from monai.networks.nets import DenseNet121
from monai.visualize import GradCAM 
model_2d = DenseNet121(spatial_dims=2, in_channels=1, out_channels=1)
cam = GradCAM(nn_module=model_2d, target_layers="class_layers.relu")
result = cam(x=torch.rand((1, 1, 64, 64)))
wyli commented 1 year ago

thanks for looking into this @thatgeeman, indeed it's already supported. I'm closing this and please help update one of the test cases for the single channel output if you are interested... https://github.com/Project-MONAI/MONAI/blob/7614aca16617a8f0350a206dd57f7a9bfca66fd4/tests/test_vis_gradcam.py#L103