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.79k stars 1.52k forks source link

[Feature Request] Counter Factual GCAM #512

Open MoH-assan opened 6 days ago

MoH-assan commented 6 days ago

Hi, It would be nice to support counterfactual grad-cams from section 3.3 in the original paper. https://arxiv.org/pdf/1610.02391

I guess it is a matter of changing this https://github.com/jacobgil/pytorch-grad-cam/blob/1ff3f58818baa2889f3f51d0b9759783b4333ba0/pytorch_grad_cam/base_cam.py#L141-L142 to this

            cam = self.get_cam_image(input_tensor, target_layer, targets, layer_activations, layer_grads, eigen_smooth)
            if counter_factual: #bkr
                cam = cam * -1

and passing counter_factual through call all the way to compute_cam_per_layer

I can do a pull request, but I am not very familiar with the entire code base.

Thanks,