Skip to content

Fix GradCAM ReLU placement per original paper#809

Open
Mr-Neutr0n wants to merge 2 commits intosalesforce:mainfrom
Mr-Neutr0n:fix/gradcam-relu-placement
Open

Fix GradCAM ReLU placement per original paper#809
Mr-Neutr0n wants to merge 2 commits intosalesforce:mainfrom
Mr-Neutr0n:fix/gradcam-relu-placement

Conversation

@Mr-Neutr0n
Copy link

Summary

  • Fixes incorrect ReLU placement in GradCAM computation
  • Per the original GradCAM paper, ReLU should be applied to the final weighted combination of feature maps, not to intermediate gradients

Changes

  • Removed .clamp(0) from gradient computation on line 178
  • Applied .clamp(min=0) to the final gradcams = (cams * grads).clamp(min=0) result

Background

The GradCAM paper (Selvaraju et al., 2017) specifies that ReLU is applied to the final linear combination of weighted feature maps to obtain the class-discriminative localization map. Applying ReLU to gradients prematurely removes negative gradient information that may be relevant for the weighted combination.

Test plan

  • Review GradCAM paper to confirm correct ReLU placement
  • Verify the fix aligns with standard GradCAM implementations

Fixes #789

🤖 Generated with Claude Code

Move ReLU (clamp) from intermediate gradients to the final
gradcam computation. Per the original GradCAM paper, ReLU
should be applied to the weighted combination of feature maps
(cams * grads), not to the gradients alone.

Fixes salesforce#789

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@salesforce-cla
Copy link

salesforce-cla bot commented Feb 5, 2026

Thanks for the contribution! Before we can merge this, we need @Mr-Neutr0n to sign the Salesforce Inc. Contributor License Agreement.

@Mr-Neutr0n
Copy link
Author

I have signed the CLA. Please recheck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

difference between paper and implementation in gradcam calculation

1 participant