class_docstr: Implementation of the DPatch attack.
  
  | Paper link: https://arxiv.org/abs/1806.02299v4
class_docstr_tok: ["Implementation","of","the","DPatch","attack",".","|","Paper","link",":","https",":","\/\/arxiv.org\/abs\/1806.02299v4"]
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.
  :param random_location: True if patch location should be random.
  :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.
  :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",".",":","param","random_location",":","True","if","patch","location","should","be","random",".",":","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",".",":","return",":","The","patched","images","."]
code: def apply_patch(
      self,
      x: np.ndarray,
      patch_external: Optional[np.ndarray] = None,
      random_location: bool = False,
      mask: 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.
      :param random_location: True if patch location should be random.
      :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.
      :return: The patched images.
      """
      if patch_external is not None:
          patch_local = patch_external
      else:
          patch_local = self._patch
  
      patched_images, _ = self._augment_images_with_patch(
          x=x,
          patch=patch_local,
          random_location=random_location,
          channels_first=self.estimator.channels_first,
          mask=mask,
      )
  
      return patched_images
  
file_path: adversarial-robustness-toolbox/art/attacks/evasion/dpatch.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
label_desc: Use image node_id 4 for calling the DPatch obj's underlying member method code with example usage: obj.apply_patch(x, patch_external, random_location, mask) and returns: patched_images