class_docstr: Implementation of the adversarial patch attack for square and rectangular images and videos in PyTorch.
  
  | Paper link: https://arxiv.org/abs/1712.09665
docstr: Generate an adversarial patch and return the patch and its mask in arrays.
  
  :param x: An array with the original input images of shape NCHW or input videos of shape NFCHW.
  :param y: An array with the original true labels.
  :param mask: An boolean array of shape equal to the shape of a single samples (1, H, W) or the shape of `x`
               (N, H, W) without their channel dimensions. Any features for which the mask is True can be the
               center location of the patch during sampling.
  :type mask: `np.ndarray`
  :return: An array with adversarial patch and an array of the patch mask.
docstr_tok: ["Generate","an","adversarial","patch","and","return","the","patch","and","its","mask","in","arrays",".",":","param","x",":","An","array","with","the","original","input","images","of","shape","NCHW","or","input","videos","of","shape","NFCHW",".",":","param","y",":","An","array","with","the","original","true","labels",".",":","param","mask",":","An","boolean","array","of","shape","equal","to","the","shape","of","a","single","samples","(","1",",","H",",","W",")","or","the","shape","of","`","x","`","(","N",",","H",",","W",")","without","their","channel","dimensions",".","Any","features","for","which","the","mask","is","True","can","be","the","center","location","of","the","patch","during","sampling",".",":","type","mask",":","`","np.ndarray","`",":","return",":","An","array","with","adversarial","patch","and","an","array","of","the","patch","mask","."]
code: def generate(  # type: ignore
      self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs
  ) -> Tuple[np.ndarray, np.ndarray]:
      """
      Generate an adversarial patch and return the patch and its mask in arrays.
  
      :param x: An array with the original input images of shape NCHW or input videos of shape NFCHW.
      :param y: An array with the original true labels.
      :param mask: An boolean array of shape equal to the shape of a single samples (1, H, W) or the shape of `x`
                   (N, H, W) without their channel dimensions. Any features for which the mask is True can be the
                   center location of the patch during sampling.
      :type mask: `np.ndarray`
      :return: An array with adversarial patch and an array of the patch mask.
      """
      import torch
  
      shuffle = kwargs.get("shuffle", True)
      mask = kwargs.get("mask")
      if mask is not None:
          mask = mask.copy()
      mask = self._check_mask(mask=mask, x=x)
  
      if self.patch_location is not None and mask is not None:
          raise ValueError(
              "Masks can only be used if the `patch_location` is `None`."
          )
  
      if y is None:  # pragma: no cover
          logger.info(
              "Setting labels to estimator predictions and running untargeted attack because `y=None`."
          )
          y = to_categorical(
              np.argmax(self.estimator.predict(x=x), axis=1),
              nb_classes=self.estimator.nb_classes,
          )
  
      if hasattr(self.estimator, "nb_classes"):
          y = check_and_transform_label_format(
              labels=y, nb_classes=self.estimator.nb_classes
          )
  
          # check if logits or probabilities
          y_pred = self.estimator.predict(x=x[[0]])
  
          if is_probability(y_pred):
              self.use_logits = False
          else:
              self.use_logits = True
  
      if isinstance(y, np.ndarray):
          x_tensor = torch.Tensor(x)
          y_tensor = torch.Tensor(y)
  
          if mask is None:
              dataset = torch.utils.data.TensorDataset(
                  x_tensor, y_tensor
              )
              data_loader = torch.utils.data.DataLoader(
                  dataset=dataset,
                  batch_size=self.batch_size,
                  shuffle=shuffle,
                  drop_last=False,
              )
          else:
              mask_tensor = torch.Tensor(mask)
              dataset = torch.utils.data.TensorDataset(
                  x_tensor, y_tensor, mask_tensor
              )
              data_loader = torch.utils.data.DataLoader(
                  dataset=dataset,
                  batch_size=self.batch_size,
                  shuffle=shuffle,
                  drop_last=False,
              )
      else:
  
          class ObjectDetectionDataset(torch.utils.data.Dataset):
              """
              Object detection dataset in PyTorch.
              """
  
              def __init__(self, x, y):
                  self.x = x
                  self.y = y
  
              def __len__(self):
                  return self.x.shape[0]
  
              def __getitem__(self, idx):
                  img = torch.from_numpy(self.x[idx])
  
                  target = {}
                  target["boxes"] = torch.from_numpy(
                      self.y[idx]["boxes"]
                  )
                  target["labels"] = torch.from_numpy(
                      self.y[idx]["labels"]
                  )
                  target["scores"] = torch.from_numpy(
                      self.y[idx]["scores"]
                  )
  
                  return img, target
  
          class ObjectDetectionDatasetMask(torch.utils.data.Dataset):
              """
              Object detection dataset in PyTorch.
              """
  
              def __init__(self, x, y, mask):
                  self.x = x
                  self.y = y
                  self.mask = mask
  
              def __len__(self):
                  return self.x.shape[0]
  
              def __getitem__(self, idx):
                  img = torch.from_numpy(self.x[idx])
  
                  target = {}
                  target["boxes"] = torch.from_numpy(
                      self.y[idx]["boxes"]
                  )
                  target["labels"] = torch.from_numpy(
                      self.y[idx]["labels"]
                  )
                  target["scores"] = torch.from_numpy(
                      self.y[idx]["scores"]
                  )
                  mask_i = torch.from_numpy(self.mask[idx])
  
                  return img, target, mask_i
  
          dataset_object_detection: Union[
              ObjectDetectionDataset, ObjectDetectionDatasetMask
          ]
          if mask is None:
              dataset_object_detection = ObjectDetectionDataset(x, y)
          else:
              dataset_object_detection = ObjectDetectionDatasetMask(
                  x, y, mask
              )
  
          data_loader = torch.utils.data.DataLoader(
              dataset=dataset_object_detection,
              batch_size=self.batch_size,
              shuffle=shuffle,
              drop_last=False,
          )
  
      for i_iter in trange(
          self.max_iter,
          desc="Adversarial Patch PyTorch",
          disable=not self.verbose,
      ):
          if mask is None:
              for images, target in data_loader:
                  images = images.to(self.estimator.device)
                  if isinstance(target, torch.Tensor):
                      target = target.to(self.estimator.device)
                  else:
                      targets = []
                      for idx in range(target["boxes"].shape[0]):
                          targets.append(
                              {
                                  "boxes": target["boxes"][idx].to(
                                      self.estimator.device
                                  ),
                                  "labels": target["labels"][idx].to(
                                      self.estimator.device
                                  ),
                                  "scores": target["scores"][idx].to(
                                      self.estimator.device
                                  ),
                              }
                          )
                      target = targets
                  _ = self._train_step(
                      images=images, target=target, mask=None
                  )
          else:
              for images, target, mask_i in data_loader:
                  images = images.to(self.estimator.device)
                  if isinstance(target, torch.Tensor):
                      target = target.to(self.estimator.device)
                  else:
                      targets = []
                      for idx in range(target["boxes"].shape[0]):
                          targets.append(
                              {
                                  "boxes": target["boxes"][idx].to(
                                      self.estimator.device
                                  ),
                                  "labels": target["labels"][idx].to(
                                      self.estimator.device
                                  ),
                                  "scores": target["scores"][idx].to(
                                      self.estimator.device
                                  ),
                              }
                          )
                      target = targets
                  mask_i = mask_i.to(self.estimator.device)
                  _ = self._train_step(
                      images=images, target=target, mask=mask_i
                  )
  
          # Write summary
          if self.summary_writer is not None:  # pragma: no cover
              x_patched = (
                  self._random_overlay(
                      images=torch.from_numpy(x).to(
                          self.estimator.device
                      ),
                      patch=self._patch,
                      mask=mask,
                  )
                  .detach()
                  .cpu()
                  .numpy()
              )
  
              self.summary_writer.update(
                  batch_id=0,
                  global_step=i_iter,
                  grad=None,
                  patch=self._patch,
                  estimator=self.estimator,
                  x=x_patched,
                  y=y,
                  targeted=self.targeted,
              )
  
      if self.summary_writer is not None:
          self.summary_writer.reset()
  
      return (
          self._patch.detach().cpu().numpy(),
          self._get_circular_patch_mask(nb_samples=1).cpu().numpy()[0],
      )
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  import math
  from typing import Any, Optional, Tuple, Union, TYPE_CHECKING
  import numpy
  from tqdm.auto import trange
  from art.attacks.attack import EvasionAttack
  from art.attacks.evasion.adversarial_patch.utils import insert_transformed_patch
  from art.estimators.estimator import BaseEstimator, NeuralNetworkMixin
  from art.utils import check_and_transform_label_format, is_probability, to_categorical
  from art.summary_writer import SummaryWriter
label_desc: Use image node_id 7 for calling the AdversarialPatchPyTorch obj's underlying member method code with example usage: obj.generate(x, y) and returns: self,  self, self, img,  target, self, img,  target,  mask_i