def apply_patch(
        self,
        x: np.ndarray,
        scale: float,
        patch_external: np.ndarray | None = None,
        mask: np.ndarray | None = 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: A 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()
        )

        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: A 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.
        
art/attacks/evasion/adversarial_patch/adversarial_patch_pytorch.py