class_docstr: Implementation of a particular variation of the DPatch attack.
  It follows Lee & Kolter (2019) in using sign gradients with expectations over transformations.
  The particular transformations supported in this implementation are cropping, rotations by multiples of 90 degrees,
  and changes in the brightness of the image.
  
  | Paper link (original DPatch): https://arxiv.org/abs/1806.02299v4
  | Paper link (physical-world patch from Lee & Kolter): https://arxiv.org/abs/1906.11897
class_docstr_tok: ["Implementation","of","a","particular","variation","of","the","DPatch","attack",".","It","follows","Lee","&","Kolter","(","2019",")","in","using","sign","gradients","with","expectations","over","transformations",".","The","particular","transformations","supported","in","this","implementation","are","cropping",",","rotations","by","multiples","of","90","degrees",",","and","changes","in","the","brightness","of","the","image",".","|","Paper","link","(","original","DPatch",")",":","https",":","\/\/arxiv.org\/abs\/1806.02299v4","|","Paper","link","(","physical-world","patch","from","Lee","&","Kolter",")",":","https",":","\/\/arxiv.org\/abs\/1906.11897"]
docstr: Apply the adversarial patch to images.
  
  :param x: Images to be patched.
  :param patch_external: External patch to apply to images `x`. If None the attacks patch will be applied.
  :return: The patched images.
docstr_tok: ["Apply","the","adversarial","patch","to","images",".",":","param","x",":","Images","to","be","patched",".",":","param","patch_external",":","External","patch","to","apply","to","images","`","x","`",".","If","None","the","attacks","patch","will","be","applied",".",":","return",":","The","patched","images","."]
code: def apply_patch(
      self, x: np.ndarray, patch_external: Optional[np.ndarray] = None
  ) -> np.ndarray:
      """
      Apply the adversarial patch to images.
  
      :param x: Images to be patched.
      :param patch_external: External patch to apply to images `x`. If None the attacks patch will be applied.
      :return: The patched images.
      """
  
      x_patch = x.copy()
  
      if patch_external is not None:
          patch_local = patch_external.copy()
      else:
          patch_local = self._patch.copy()
  
      if self.estimator.channels_first:
          x_patch = np.transpose(x_patch, (0, 2, 3, 1))
          patch_local = np.transpose(patch_local, (1, 2, 0))
  
      # Apply patch:
      x_1, y_1 = self.patch_location
      x_2, y_2 = x_1 + patch_local.shape[0], y_1 + patch_local.shape[1]
  
      if (
          x_2 > x_patch.shape[1] or y_2 > x_patch.shape[2]
      ):  # pragma: no cover
          raise ValueError(
              "The patch (partially) lies outside the image."
          )
  
      x_patch[:, x_1:x_2, y_1:y_2, :] = patch_local
  
      if self.estimator.channels_first:
          x_patch = np.transpose(x_patch, (0, 3, 1, 2))
  
      return x_patch
  
file_path: adversarial-robustness-toolbox/art/attacks/evasion/dpatch_robust.py
imports: import logging
  import math
  import random
  from typing import Dict, List, Optional, Tuple, Union, TYPE_CHECKING
  import numpy
  from tqdm.auto import trange
  from art.attacks.attack import EvasionAttack
  from art.estimators.estimator import BaseEstimator, LossGradientsMixin
  from art.estimators.object_detection.object_detector import ObjectDetectorMixin
  from art import config
  from art.summary_writer import SummaryWriter
label_desc: Use image node_id 5 for calling the RobustDPatch obj's underlying member method code with example usage: obj.apply_patch(x, patch_external) and returns: x_patch