code: def _apply(self, adversary, max_pixels=1000, isPreprocessed=False):
      if not self.support_targeted:
          if adversary.is_targeted_attack:
              raise ValueError(
                  "This attack method doesn't support targeted attack!"
              )
  
      min_, max_ = self.model.bounds()
  
      # 强制拷贝 避免针对adv_img的修改也影响adversary.original
      adv_img = np.copy(adversary.original)
  
      """
          adversary.original  原始数据
          adversary.original_label  原始数据的标签
          adversary.target_label       定向攻击的目标值
          adversary.__adversarial_example 保存生成的对抗样本
          adversary.adversarial_label  对抗样本的标签
          """
  
      axes = [
          i
          for i in range(adversary.original.ndim)
          if i != self.model.channel_axis()
      ]
  
      # 输入的图像必须具有长和宽属性
      assert len(axes) == 2
  
      h = adv_img.shape[axes[0]]
      w = adv_img.shape[axes[1]]
  
      # print("w={0},h={1}".format(w,h))
  
      # max_pixel为攻击点的最多个数 从原始图像中随机选择max_pixel个进行攻击
  
      pixels = np.random.permutation(h * w)
      pixels = pixels[:max_pixels]
  
      for i, pixel in enumerate(pixels):
          x = pixel % w
          y = pixel // w
  
          location = [x, y]
  
          logging.info("Attack x={0} y={1}".format(x, y))
  
          location.insert(self.model.channel_axis(), slice(None))
          location = tuple(location)
  
          if not isPreprocessed:
              # logger.info("value in [min_={0}, max_={1}]".format(min_, max_))
              # 图像没有经过预处理 取值为整数 范围为0-255
              for value in [min_, max_]:
                  perturbed = np.copy(adv_img)
                  # 针对图像的每个信道的点[x,y]同时进行修改
                  perturbed[location] = value
  
                  f = self.model.predict(perturbed)
                  adv_label = np.argmax(f)
  
                  # if adversary.try_accept_the_example(adv_img, adv_label):
                  if adversary.try_accept_the_example(
                      perturbed, adv_label
                  ):
                      return adversary
          else:
              # 图像经过预处理 取值为整数 通常范围为0-1
              for value in np.linspace(min_, max_, num=256):
                  # logger.info("value in [min_={0}, max_={1},step num=256]".format(min_, max_))
                  perturbed = np.copy(adv_img)
                  # 针对图像的每个信道的点[x,y]同时进行修改
                  perturbed[location] = value
  
                  f = self.model.predict(perturbed)
                  adv_label = np.argmax(f)
  
                  # if adversary.try_accept_the_example(adv_img, adv_label):
                  if adversary.try_accept_the_example(
                      perturbed, adv_label
                  ):
                      return adversary
  
      return adversary
  
file_path: AdvBox/adversarialbox/attacks/localsearch.py
imports: from __future__ import division
  from builtins import zip
  from builtins import str
  from builtins import range
  import logging
  from collections import Iterable
  import numpy
  from .base import Attack
label_desc: Use image node_id 2 for calling the SinglePixelAttack obj's underlying member method code with example usage: obj._apply(adversary, max_pixels, isPreprocessed) and returns: adversary, adversary, adversary