- [2024.06.17] The detailed usage and examples on more datasets are updated!
- [2024.04.18] The implementation code has been uploaded!
- [2024.04.06] The paper has been accepted by IEEE Transactions on Pattern Analysis and Machine Intelligence.
- Saliency Our code is built under the framework of Saliency project by PAIR. Shout out to PAIR-code!
- SHAP An explanation framework for machine learning models.
- Visualizing the Impact of Feature Attribution Baselines A blog carefully introduces the IG (Integrated Gradients) method and its baseline choice.
Install the Saliency. Please refer to saliency repositories for the detailed descriptions.
pip install saliency
Our method is implemented by the class IG2, in file ig2.py. This class contains the following methods (the default parameters are suggested for ImageNet samples):
- Get_GradPath(x_value, baselines, call_model_function, call_model_args=None, steps=201, step_sizes=256.0, clip_min_max=[0,255]): Iteratively searchs the counterfactuals based on gradinet descent, building GradPath for integration.
- GetMask(x_value, baselines, call_model_function, call_model_args=None, steps=201, step_sizes=256.0, clip_min_max=[0,255]): Integrates the gradients on the GradPath, returns a saliency mask.
This example iPython notebook showing IG2 example for attritbuting the features of ImageNet samples with Pytorch.
import ig2
from saliency.core.base import ...
...
# Calculate the gradients of representation distance (MSE) between the explained image and searched path points.
def call_model_function(x_value_batched, call_model_args, expected_keys):
elif REP_DISTANCE_GRADIENTS in expected_keys:
loss_fn = torch.nn.MSELoss()
baseline_conv = call_model_args['layer_baseline']
input_conv = rep_layer_outputs[REP_LAYER_VALUES]
loss = -1 * loss_fn(input_conv, baseline_conv)
loss.backward()
grads = images.grad.data
grads = torch.movedim(grads, 1, 3)
gradients = grads.cpu().detach().numpy()
return {REP_DISTANCE_GRADIENTS: gradients,
'loss':loss}
...
# Load explained sample and references. (You can custom your own datasets here.)
rnd_idx = np.random.choice(all_references.shape[0],replace=False, size=n_reference)
references = all_references[rnd_idx]
# Compute IG2.
explainer = ig2.IG2()
ig2_mask = explainer.GetMask(im,references,
call_model_function,call_model_args,steps=201,step_size=256.0,clip_min_max=[0,255],)
# Compute a 2D tensor for visualization.
mask_grayscale = vis.VisualizeImageGrayscale(ig2_mask)
f, ax = plt.subplots()
ShowGrayscaleImage(mask_grayscale, ax)
If you find our work useful, please cite:
@ARTICLE{10497902,
author={Zhuo, Yue and Ge, Zhiqiang},
journal={IEEE Transactions on Pattern Analysis and Machine Intelligence},
title={IG2: Integrated Gradient on Iterative Gradient Path for Feature Attribution},
year={2024},
volume={46},
number={11},
pages={7173-7190},
doi={10.1109/TPAMI.2024.3388092}}