项目作者: PAIR-code

项目描述 :
TensorFlow implementation for SmoothGrad, Grad-CAM, Guided backprop, Integrated Gradients and other saliency techniques
高级语言: Jupyter Notebook
项目地址: git://github.com/PAIR-code/saliency.git
创建时间: 2017-06-09T22:07:35Z
项目社区:https://github.com/PAIR-code/saliency

开源协议:Apache License 2.0

下载


Saliency Library

Updates

🔴 Now framework-agnostic! (Example core notebook) 🔴

🔗 For further explanation of the methods and more examples of the resulting maps, see our Github Pages website 🔗

If upgrading from an older version, update old imports to import saliency.tf1 as saliency. We provide wrappers to make the framework-agnostic version compatible with TF1 models. (Example TF1 notebook)

🔴 Added Performance Information Curve (PIC) - a human
independent metric for evaluating the quality of saliency methods.
(Example notebook) 🔴

Saliency Methods

This repository contains code for the following saliency techniques:

*Developed by PAIR.

This list is by no means comprehensive. We are accepting pull requests to add
new methods!

Evaluation of Saliency Methods

The repository provides an implementation of Performance Information Curve (PIC) -
a human independent metric for evaluating the quality of saliency methods
(paper,
poster,
code,
notebook).

Download

  1. # To install the core subpackage:
  2. pip install saliency
  3. # To install core and tf1 subpackages:
  4. pip install saliency[tf1]

or for the development version:

  1. git clone https://github.com/pair-code/saliency
  2. cd saliency

Usage

The saliency library has two subpackages:

  • core uses a generic call_model_function which can be used with any ML
    framework.
  • tf1 accepts input/output tensors directly, and sets up the necessary
    graph operations for each method.

Core

Each saliency mask class extends from the CoreSaliency base class. This class
contains the following methods:

  • GetMask(x_value, call_model_function, call_model_args=None): Returns a mask
    of
    the shape of non-batched x_value given by the saliency technique.
  • GetSmoothedMask(x_value, call_model_function, call_model_args=None, stdev_spread=.15, nsamples=25, magnitude=True):
    Returns a mask smoothed of the shape of non-batched x_value with the
    SmoothGrad technique.

The visualization module contains two methods for saliency visualization:

  • VisualizeImageGrayscale(image_3d, percentile): Marginalizes across the
    absolute value of each channel to create a 2D single channel image, and clips
    the image at the given percentile of the distribution. This method returns a
    2D tensor normalized between 0 to 1.
  • VisualizeImageDiverging(image_3d, percentile): Marginalizes across the
    value of each channel to create a 2D single channel image, and clips the
    image at the given percentile of the distribution. This method returns a
    2D tensor normalized between -1 to 1 where zero remains unchanged.

If the sign of the value given by the saliency mask is not important, then use
VisualizeImageGrayscale, otherwise use VisualizeImageDiverging. See
the SmoothGrad paper for more details on which visualization method to use.

call_model_function

call_model_function is how we pass inputs to a given model and receive the outputs
necessary to compute saliency masks. The description of this method and expected
output format is in the CoreSaliency description, as well as separately for each method.

Examples

This example iPython notebook
showing these techniques is a good starting place.

Here is a condensed example of using IG+SmoothGrad with TensorFlow 2:

  1. import saliency.core as saliency
  2. import tensorflow as tf
  3. ...
  4. # call_model_function construction here.
  5. def call_model_function(x_value_batched, call_model_args, expected_keys):
  6. tape = tf.GradientTape()
  7. grads = np.array(tape.gradient(output_layer, images))
  8. return {saliency.INPUT_OUTPUT_GRADIENTS: grads}
  9. ...
  10. # Load data.
  11. image = GetImagePNG(...)
  12. # Compute IG+SmoothGrad.
  13. ig_saliency = saliency.IntegratedGradients()
  14. smoothgrad_ig = ig_saliency.GetSmoothedMask(image,
  15. call_model_function,
  16. call_model_args=None)
  17. # Compute a 2D tensor for visualization.
  18. grayscale_visualization = saliency.VisualizeImageGrayscale(
  19. smoothgrad_ig)

TF1

Each saliency mask class extends from the TF1Saliency base class. This class
contains the following methods:

  • __init__(graph, session, y, x): Constructor of the SaliencyMask. This can
    modify the graph, or sometimes create a new graph. Often this will add nodes
    to the graph, so this shouldn’t be called continuously. y is the output
    tensor to compute saliency masks with respect to, x is the input tensor
    with the outer most dimension being batch size.
  • GetMask(x_value, feed_dict): Returns a mask of the shape of non-batched
    x_value given by the saliency technique.
  • GetSmoothedMask(x_value, feed_dict): Returns a mask smoothed of the shape
    of non-batched x_value with the SmoothGrad technique.

The visualization module contains two visualization methods:

  • VisualizeImageGrayscale(image_3d, percentile): Marginalizes across the
    absolute value of each channel to create a 2D single channel image, and clips
    the image at the given percentile of the distribution. This method returns a
    2D tensor normalized between 0 to 1.
  • VisualizeImageDiverging(image_3d, percentile): Marginalizes across the
    value of each channel to create a 2D single channel image, and clips the
    image at the given percentile of the distribution. This method returns a
    2D tensor normalized between -1 to 1 where zero remains unchanged.

If the sign of the value given by the saliency mask is not important, then use
VisualizeImageGrayscale, otherwise use VisualizeImageDiverging. See
the SmoothGrad paper for more details on which visualization method to use.

Examples

This example iPython notebook shows
these techniques is a good starting place.

Another example of using GuidedBackprop with SmoothGrad from TensorFlow:

  1. from saliency.tf1 import GuidedBackprop
  2. from saliency.tf1 import VisualizeImageGrayscale
  3. import tensorflow.compat.v1 as tf
  4. ...
  5. # Tensorflow graph construction here.
  6. y = logits[5]
  7. x = tf.placeholder(...)
  8. ...
  9. # Compute guided backprop.
  10. # NOTE: This creates another graph that gets cached, try to avoid creating many
  11. # of these.
  12. guided_backprop_saliency = GuidedBackprop(graph, session, y, x)
  13. ...
  14. # Load data.
  15. image = GetImagePNG(...)
  16. ...
  17. smoothgrad_guided_backprop =
  18. guided_backprop_saliency.GetMask(image, feed_dict={...})
  19. # Compute a 2D tensor for visualization.
  20. grayscale_visualization = visualization.VisualizeImageGrayscale(
  21. smoothgrad_guided_backprop)

Conclusion/Disclaimer

If you have any questions or suggestions for improvements to this library,
please contact the owners of the PAIR-code/saliency repository.

This is not an official Google product.