Skip to main content

This repository implements the faithfulness metrics mentionned in the paper --Computing and evaluating saliency maps for image classification: a tutorial-- in Pytorch.

Project description

Faithfulness metrics for saliency maps

TODO: explain the role of each script

This repository implements the faithfulness metrics mentionned in the paper "Computing and evaluating saliency maps for image classification: a tutorial" in Pytorch. This can be used to compute the following metrics:

alt text

Single step metrics

This section covers the use of the Increase In Confidence (IIC), Average Drop (AD) and Average Drop in Deletion (ADD) metrics. First, generate the saliency map of the image however you want. The only constraint is that the map should be a tensor of size (Nx1xH'xW'):

saliency_map = gradcam.attribute(img,class_ind)

Then, compute the metric. In this example, we use the class IIC_AD of this library to compute both the AD and IIC metric, as they require similar computations:

iic_ad = IIC_AD()
metric_dict = iic_ad(model,data,explanations,class_to_explain)
mean_iic = metric_dict["iic"]
mean_ad = metric_dict["ad"]

The resulting dictionary has two entries ic and ad which correspond to the mean value of the two metrics.

The __call__() method for all the metrics requires the following arguments :

  • model: a torch.nn.Module that outputs a score tensor of shape (NxC), on which a softmax activation has been applied.
  • data: the input image tensor of shape (Nx3xHxW).
  • explanations: the saliency maps tensor of shape (Nx1xH'xW')
  • class_to_explain: The index of the class to explain for each input image. The shape shoud be (N).

The ADD class is used similarly:

add = ADD()
metric_dict = add(model,data,explanations,class_to_explain)
mean = metric_dict["add"]

This resulting dictionary has only one entry add which correspond to the mean value of the ADD metric.

Multi-step metrics

This section covers the use of the DAUC, IAUC, DC and IC metrics. These metrics work similarly but some argument have to be passed to the constructor:

deletion = Deletion(data_shape,explanation_shape,bound_max_step=True)

Where data_shape and explanation_shape are the shape of the image and saliency map tensor. A high resolution saliency map of size 56x56 would require approximately 3k inferences. To prevent too much computation, you can set the bound_max_step argument to True to limits the amount of computation that can be computed. More precisly, this argument forces to mask/reveal several pixels before computing a new inference if the resolution of the saliency map is superior to 14x14. The call method returns a dictionary with two entries: dauc (which correspond to the original Deletion metric by Petsiuk et al.) and dc (the variant proposed by Gomez et al.).

result_dic = deletion(model,data,explanations,class_to_explain)
dauc = result_dic["dauc"]
dc = result_dic["dc"]

The Insertion metric is computed similarly:

insertion = Insertion(data_shape,explanation_shape,bound_max_step=True)
result_dic = insertion(model,data,explanations,class_to_explain)
iauc = result_dic["iauc"]
ic = result_dic["ic"]

Changing the filling method

These metrics work by removing parts of the image and replacing it with something else, for e.g. black pixels (Deletion, AD, IIC, ADD) or a blurred version of the input image (Insertion). The default replacing method can be changed with the data_replace_method argument passed to the constructor:

insertion = Insertion(data_shape,explanation_shape,bound_max_step=True,data_replace_method="black")
add = ADD(data_replace_method="blur")

Currently, the autorized values are:

  • "black": replaces with black pixels
  • "blur": replaces with a blurred version of the input image

Demonstration

Look at the demo.ipynb script for a demonstration. If you want to re-run the demo, you should download the model's weights pretrained on the CUB dataset and put it on the project's root. You should also download the CUB test dataset and put it in a "data" folder located at the project's root. The dataset should be formated as expected by the torchvision.datasets.ImageFolder class from torchvision.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

saliency_maps_metrics-0.0.8.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

saliency_maps_metrics-0.0.8-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file saliency_maps_metrics-0.0.8.tar.gz.

File metadata

  • Download URL: saliency_maps_metrics-0.0.8.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.5

File hashes

Hashes for saliency_maps_metrics-0.0.8.tar.gz
Algorithm Hash digest
SHA256 17cb61102363ed78c12b0c993d8e609dea63f2e9cfecb535edda21a27460327f
MD5 a2b05ba22da2073d261e17fa261157a1
BLAKE2b-256 1dd83b7d88ad331cf4f50a42b700441e3eedc5068f8e23534fe7098e708626cd

See more details on using hashes here.

File details

Details for the file saliency_maps_metrics-0.0.8-py3-none-any.whl.

File metadata

File hashes

Hashes for saliency_maps_metrics-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 e77f898b33f290431dde27ff7639ac3725a0dcca5484a1daf2f7e0f41a14fa02
MD5 03ae1097b7eab21721d34e4e0f0ee9e5
BLAKE2b-256 a23b100179391b9e8ff581b3991b040aed6677e222f4a3f6c0891e6c9109956f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page