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: A function to apply the learned adversarial patch to images or videos.
  
  :param x: Instances to apply randomly transformed patch.
  :param scale: Scale of the applied patch in relation to the estimator input shape.
  :param patch_external: External patch to apply to images `x`.
  :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 samples.
docstr_tok: ["A","function","to","apply","the","learned","adversarial","patch","to","images","or","videos",".",":","param","x",":","Instances","to","apply","randomly","transformed","patch",".",":","param","scale",":","Scale","of","the","applied","patch","in","relation","to","the","estimator","input","shape",".",":","param","patch_external",":","External","patch","to","apply","to","images","`","x","`",".",":","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","samples","."]
code: def apply_patch(
      self,
      x: np.ndarray,
      scale: float,
      patch_external: Optional[np.ndarray] = None,
      mask: Optional[np.ndarray] = None,
  ) -> np.ndarray:
      """
      A function to apply the learned adversarial patch to images or videos.
  
      :param x: Instances to apply randomly transformed patch.
      :param scale: Scale of the applied patch in relation to the estimator input shape.
      :param patch_external: External patch to apply to images `x`.
      :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 samples.
      """
      import torch
  
      if mask is not None:
          mask = mask.copy()
      mask = self._check_mask(mask=mask, x=x)
      x_tensor = torch.Tensor(x).to(self.estimator.device)
      if mask is not None:
          mask_tensor = torch.Tensor(mask).to(self.estimator.device)
      else:
          mask_tensor = None
      if isinstance(patch_external, np.ndarray):
          patch_tensor = torch.Tensor(patch_external).to(
              self.estimator.device
          )
      else:
          patch_tensor = self._patch
      return (
          self._random_overlay(
              images=x_tensor,
              patch=patch_tensor,
              scale=scale,
              mask=mask_tensor,
          )
          .detach()
          .cpu()
          .numpy()
      )
  
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 9 for calling the AdversarialPatchPyTorch obj's underlying member method code with example usage: obj.apply_patch(x, scale, patch_external, mask) and returns: self