jacobgil / pytorch-grad-cam

Advanced AI Explainability for computer vision. Support for CNNs, Vision Transformers, Classification, Object detection, Segmentation, Image similarity and more.
https://jacobgil.github.io/pytorch-gradcam-book
MIT License
9.82k stars 1.52k forks source link

NaNs in masks generated by ScoreCAM when using resnet50 #404

Closed marios1861 closed 1 year ago

marios1861 commented 1 year ago

I am getting NaNs in masks when using ScoreCAM in resnet50. I have no idea on what could be wrong because the same exact code works fine for vgg16 and vit_b_16. I added a warning on the output of the ScoreCAM method get_cam_weights if the weights variable contains NaNs. I then traced back the error to line 40 of score_cam.py, specifically in some cases $maxs = mins$. After further inspection, it looks like the upsampled activations (and probably the original activations) happen to have totally deactivated channels (all elements are zero in the spatial dimensions). I don't know how to proceed from here. I am using the resnet50 weights from torchvision, as in the example, and my data is from the imagenet evaluation dataset. Also, everything works fine for gradcam and gradcam++

YuboHe commented 1 year ago

same here, scorecam returns NaNs for resnet and densenet

mxmlnwbr commented 1 year ago

+1

marios1861 commented 1 year ago

Seems to be fixed for me. Must have been the pytorch 2.0 fallout.

mxmlnwbr commented 1 year ago

Seems to be fixed for me. Must have been the pytorch 2.0 fallout.

@marios1861 What did you do to fix it?

marziehoghbaie commented 11 months ago

I'm using torch1.12 but still getting the same issue