How to use the saliency.SaliencyMask function in saliency

To help you get started, we’ve selected a few saliency examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github PAIR-code / saliency / guided_backprop.py View on Github external
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Utilites to computed GuidedBackprop SaliencyMasks"""

from saliency import SaliencyMask
import tensorflow as tf

class GuidedBackprop(SaliencyMask):
  """A SaliencyMask class that computes saliency masks with GuidedBackProp.

  This implementation copies the TensorFlow graph to a new graph with the ReLU
  gradient overwritten as in the paper:
  https://arxiv.org/abs/1412.6806
  """

  GuidedReluRegistered = False

  def __init__(self, graph, session, y, x):
    """Constructs a GuidedBackprop SaliencyMask."""
    super(GuidedBackprop, self).__init__(graph, session, y, x)

    self.x = x

    if GuidedBackprop.GuidedReluRegistered is False:
github PAIR-code / saliency / occlusion.py View on Github external
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Utilities to compute an Occlusion SaliencyMask."""

import numpy as np
from saliency import SaliencyMask
import tensorflow as tf

class Occlusion(SaliencyMask):
  """A SaliencyMask class that computes saliency masks by occluding the image.

  This method slides a window over the image and computes how that occlusion
  affects the class score. When the class score decreases, this is positive
  evidence for the class, otherwise it is negative evidence.
  """

  def __init__(self, graph, session, y, x):
    super(Occlusion, self).__init__(graph, session, y, x)

  def GetMask(self, x_value, feed_dict = {}, size = 15, value = 0):
    """Returns an occlusion mask."""
    occlusion_window = np.array([size, size, x_value.shape[2]])
    occlusion_window.fill(value)

    occlusion_scores = np.zeros_like(x_value)
github PAIR-code / saliency / saliency.py View on Github external
Args:
      x_value: Input value, not batched.
      feed_dict: (Optional) feed dictionary to pass to the session.run call.
    """
    stdev = stdev_spread * (np.max(x_value) - np.min(x_value))

    total_gradients = np.zeros_like(x_value)
    for i in range(nsamples):
      noise = np.random.normal(0, stdev, x_value.shape)
      x_plus_noise = x_value + noise

      total_gradients += self.GetMask(x_plus_noise, feed_dict)

    return total_gradients / nsamples

class GradientSaliency(SaliencyMask):
  r"""A SaliencyMask class that computes saliency masks with a gradient."""

  def __init__(self, graph, session, y, x):
    super(GradientSaliency, self).__init__(graph, session, y, x)
    self.gradients_node = tf.gradients(y, x)[0]

  def GetMask(self, x_value, feed_dict={}):
    """Returns a vanilla gradient mask.

    Args:
      x_value: Input value, not batched.
      feed_dict: (Optional) feed dictionary to pass to the session.run call.
    """
    feed_dict[self.x] = [x_value]
    return self.session.run(self.gradients_node, feed_dict=feed_dict)[0]