class_docstr: Implements the Saliency Map Attack.
  The Jacobian-based Saliency Map Approach (Papernot et al. 2016).
  Paper link: https://arxiv.org/pdf/1511.07528.pdf
class_docstr_tok: ["Implements","the","Saliency","Map","Attack",".","The","Jacobian-based","Saliency","Map","Approach","(","Papernot","et","al",".","2016",")",".","Paper","link",":","https",":","\/\/arxiv.org\/pdf\/1511.07528.pdf"]
docstr: Get pixel location with highest influence on class.
  Args:
      image(numpy.ndarray): Image with shape (height, width, channels).
      target(int): The target label.
      labels(int): The number of classes of the output label.
      mask(list): Each modified pixel with border value is set to zero in mask.
      fast(bool): Whether evaluate the pixel influence on sum of residual classes.
      two_pix(bool): Whether to select two pixels from salience map which 
          is used in original paper.
  Return:
      idx: The index of optimal pixel (idx = idx1 * input_dimension +
          idx2 in two pix setting)
      pix_sign: The direction of perturbation
docstr_tok: ["Get","pixel","location","with","highest","influence","on","class",".","Args",":","image","(","numpy.ndarray",")",":","Image","with","shape","(","height",",","width",",","channels",")",".","target","(","int",")",":","The","target","label",".","labels","(","int",")",":","The","number","of","classes","of","the","output","label",".","mask","(","list",")",":","Each","modified","pixel","with","border","value","is","set","to","zero","in","mask",".","fast","(","bool",")",":","Whether","evaluate","the","pixel","influence","on","sum","of","residual","classes",".","two_pix","(","bool",")",":","Whether","to","select","two","pixels","from","salience","map","which","is","used","in","original","paper",".","Return",":","idx",":","The","index","of","optimal","pixel","(","idx","=","idx1","*","input_dimension","+","idx2","in","two","pix","setting",")","pix_sign",":","The","direction","of","perturbation"]
code: def _saliency_map(
      self, image, target, labels, mask, fast=False, two_pix=True
  ):
      """
      Get pixel location with highest influence on class.
      Args:
          image(numpy.ndarray): Image with shape (height, width, channels).
          target(int): The target label.
          labels(int): The number of classes of the output label.
          mask(list): Each modified pixel with border value is set to zero in mask.
          fast(bool): Whether evaluate the pixel influence on sum of residual classes.
          two_pix(bool): Whether to select two pixels from salience map which
              is used in original paper.
      Return:
          idx: The index of optimal pixel (idx = idx1 * input_dimension +
              idx2 in two pix setting)
          pix_sign: The direction of perturbation
      """
      # pixel influence on target class
      alphas = self.model.gradient(image, target) * mask
  
      # pixel influence on sum of residual classes(don't evaluate if fast == True)
      if fast:
          betas = -np.ones_like(alphas)
      else:
          betas = np.sum(
              [
                  self.model.gradient(image, label) * mask - alphas
                  for label in labels
              ],
              0,
          )
  
      if two_pix:
          # construct two pix matrix to treat it as one pix
          alphas_col = np.expand_dims(alphas, axis=0)
          alphas_mat = alphas_col.transpose(1, 0) + alphas_col
          # we want to select two different piexl
          np.fill_diagonal(alphas_mat, 0)
          alphas_mat = alphas_mat.reshape(-1)
          betas_col = np.expand_dims(betas, axis=0)
          betas_mat = betas_col.transpose(1, 0) + betas_col
          # we want to select two different piexl
          np.fill_diagonal(betas_mat, 0)
          betas_mat = betas_mat.reshape(-1)
          # compute saliency map (take into account both pos. & neg.
          # perturbations)
          sal_map = (
              np.abs(alphas_mat)
              * np.abs(betas_mat)
              * np.sign(alphas_mat * betas_mat)
          )
          # find optimal pixel & direction of perturbation
          idx = np.argmin(sal_map)
          idx = np.unravel_index(idx, sal_map.shape)
          pix_sign = np.sign(alphas_mat)[idx]
      else:
          # compute saliency map (take into account both pos. & neg.
          # perturbations)
          sal_map = (
              np.abs(alphas) * np.abs(betas) * np.sign(alphas * betas)
          )
          # find optimal pixel & direction of perturbation
          idx = np.argmin(sal_map)
          idx = np.unravel_index(idx, mask.shape)
          pix_sign = np.sign(alphas)[idx]
  
      return idx, pix_sign
  
file_path: AdvBox/adversarialbox/attacks/saliency.py
imports: from __future__ import division
  from builtins import range
  import logging
  import random
  import numpy
  from .base import Attack
label_desc: Use image node_id 3 for calling the SaliencyMapAttack obj's underlying member method code with example usage: obj._saliency_map(image, target, labels, mask, fast, two_pix) and returns: idx,  pix_sign