Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scorecam update: nan fix for batch size less than 4 #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sgsangodkar
Copy link

NAN issue with batch_size <=4 for scorecam.

Steps to reproduce the issue are as follows:
In cam.py,
#model = models.resnet50(pretrained=True)
model = models.vgg16(pretrained=True)

#target_layers = [model.layer4[-1]]
target_layers = [model.features[-1]]

#cam.batch_size = 32
cam.batch_size = 4

The output is as shown here:
https://github.com/sgsangodkar/pytorch-grad-cam/blob/scorecam_update/bsize4_before_scorecam_cam.jpg
image

Observation:
Output is not proper since there are 'nan's in the cam weights due to min max normalisation in scorecam.py file.

Possible Fix is as follows:
#maxs, mins = maxs[:, :, None, None], mins[:, :, None, None]
#upsampled = (upsampled - mins) / (maxs - mins)

        mask = maxs!=mins
        maxs, mins = maxs[:, :, None, None], mins[:, :, None, None]
        upsampled[mask,:] = (upsampled[mask,:] - mins[mask,:]) / (maxs[mask] - mins[mask])

Output after the fix is as shown here:
https://github.com/sgsangodkar/pytorch-grad-cam/blob/scorecam_update/bsize4_after_scorecam_cam.jpg
image

Regards

Sagar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant