class_docstr: Implementation of the adversarial patch attack for square and rectangular images and videos in TensorFlow v2.
  
  | Paper link: https://arxiv.org/abs/1712.09665
code: def _random_overlay(
      self,
      images: Union[np.ndarray, "tf.Tensor"],
      patch: Union[np.ndarray, "tf.Variable"],
      scale: Optional[float] = None,
      mask: Optional[Union[np.ndarray, "tf.Tensor"]] = None,
  ) -> "tf.Tensor":
      import tensorflow as tf
      import tensorflow_addons as tfa
  
      nb_samples = images.shape[0]
  
      image_mask = self._get_circular_patch_mask(nb_samples=nb_samples)
      image_mask = tf.cast(image_mask, images.dtype)
  
      smallest_image_edge = np.minimum(
          self.image_shape[self.i_h], self.image_shape[self.i_w]
      )
  
      image_mask = tf.image.resize(
          image_mask,
          size=(smallest_image_edge, smallest_image_edge),
          method=tf.image.ResizeMethod.BILINEAR,
          preserve_aspect_ratio=False,
          antialias=False,
          name=None,
      )
  
      pad_h_before = int(
          (
              self.image_shape[self.i_h]
              - image_mask.shape.as_list()[self.i_h_patch + 1]
          )
          / 2
      )
      pad_h_after = int(
          self.image_shape[self.i_h]
          - pad_h_before
          - image_mask.shape[self.i_h_patch + 1]
      )
  
      pad_w_before = int(
          (
              self.image_shape[self.i_w]
              - image_mask.shape.as_list()[self.i_w_patch + 1]
          )
          / 2
      )
      pad_w_after = int(
          self.image_shape[self.i_w]
          - pad_w_before
          - image_mask.shape[self.i_w_patch + 1]
      )
  
      image_mask = tf.pad(  # pylint: disable=E1123
          image_mask,
          paddings=tf.constant(
              [
                  [0, 0],
                  [pad_h_before, pad_h_after],
                  [pad_w_before, pad_w_after],
                  [0, 0],
              ]
          ),
          mode="CONSTANT",
          constant_values=0,
          name=None,
      )
  
      image_mask = tf.cast(image_mask, images.dtype)
  
      patch = tf.cast(patch, images.dtype)
      padded_patch = tf.stack([patch] * nb_samples)
  
      padded_patch = tf.image.resize(
          padded_patch,
          size=(smallest_image_edge, smallest_image_edge),
          method=tf.image.ResizeMethod.BILINEAR,
          preserve_aspect_ratio=False,
          antialias=False,
          name=None,
      )
  
      padded_patch = tf.pad(  # pylint: disable=E1123
          padded_patch,
          paddings=tf.constant(
              [
                  [0, 0],
                  [pad_h_before, pad_h_after],
                  [pad_w_before, pad_w_after],
                  [0, 0],
              ]
          ),
          mode="CONSTANT",
          constant_values=0,
          name=None,
      )
  
      padded_patch = tf.cast(padded_patch, images.dtype)
  
      transform_vectors = []
      translation_vectors = []
  
      for i_sample in range(nb_samples):
          if scale is None:
              im_scale = np.random.uniform(
                  low=self.scale_min, high=self.scale_max
              )
          else:
              im_scale = scale
  
          if mask is None:
              padding_after_scaling_h = (
                  self.image_shape[self.i_h]
                  - im_scale
                  * padded_patch.shape.as_list()[self.i_h + 1]
              ) / 2.0
              padding_after_scaling_w = (
                  self.image_shape[self.i_w]
                  - im_scale
                  * padded_patch.shape.as_list()[self.i_w + 1]
              ) / 2.0
              x_shift = np.random.uniform(
                  -padding_after_scaling_w, padding_after_scaling_w
              )
              y_shift = np.random.uniform(
                  -padding_after_scaling_h, padding_after_scaling_h
              )
          else:
              mask_2d = mask[i_sample, :, :]
  
              edge_x_0 = (
                  int(im_scale * padded_patch.shape[self.i_w + 1]) // 2
              )
              edge_x_1 = (
                  int(im_scale * padded_patch.shape[self.i_w + 1])
                  - edge_x_0
              )
              edge_y_0 = (
                  int(im_scale * padded_patch.shape[self.i_h + 1]) // 2
              )
              edge_y_1 = (
                  int(im_scale * padded_patch.shape[self.i_h + 1])
                  - edge_y_0
              )
  
              mask_2d[0:edge_x_0, :] = False
              if edge_x_1 > 0:
                  mask_2d[-edge_x_1:, :] = False
              mask_2d[:, 0:edge_y_0] = False
              if edge_y_1 > 0:
                  mask_2d[:, -edge_y_1:] = False
  
              num_pos = np.argwhere(mask_2d).shape[0]
              pos_id = np.random.choice(num_pos, size=1)
              pos = np.argwhere(mask_2d)[pos_id[0]]
              x_shift = pos[1] - self.image_shape[self.i_w] // 2
              y_shift = pos[0] - self.image_shape[self.i_h] // 2
  
          phi_rotate = (
              float(
                  np.random.uniform(
                      -self.rotation_max, self.rotation_max
                  )
              )
              / 180.0
              * math.pi
          )
  
          # Rotation
          rotation_matrix = np.array(
              [
                  [math.cos(-phi_rotate), -math.sin(-phi_rotate)],
                  [math.sin(-phi_rotate), math.cos(-phi_rotate)],
              ]
          )
  
          # Scale
          xform_matrix = rotation_matrix * (1.0 / im_scale)
          a_0, a_1 = xform_matrix[0]
          b_0, b_1 = xform_matrix[1]
  
          x_origin = float(self.image_shape[self.i_w]) / 2
          y_origin = float(self.image_shape[self.i_h]) / 2
  
          x_origin_shifted, y_origin_shifted = np.matmul(
              xform_matrix, np.array([x_origin, y_origin])
          )
  
          x_origin_delta = x_origin - x_origin_shifted
          y_origin_delta = y_origin - y_origin_shifted
  
          # Run translation in a second step to position patch exactly inside of the mask
          transform_vectors.append(
              [a_0, a_1, x_origin_delta, b_0, b_1, y_origin_delta, 0, 0]
          )
          translation_vectors.append(
              [1, 0, -x_shift, 0, 1, -y_shift, 0, 0]
          )
  
      image_mask = tfa.image.transform(
          image_mask,
          transform_vectors,
          "BILINEAR",
      )
      padded_patch = tfa.image.transform(
          padded_patch,
          transform_vectors,
          "BILINEAR",
      )
  
      image_mask = tfa.image.transform(
          image_mask,
          translation_vectors,
          "BILINEAR",
      )
      padded_patch = tfa.image.transform(
          padded_patch,
          translation_vectors,
          "BILINEAR",
      )
  
      if self.nb_dims == 4:
          image_mask = tf.stack([image_mask] * images.shape[1], axis=1)
          image_mask = tf.cast(image_mask, images.dtype)
  
          padded_patch = tf.stack(
              [padded_patch] * images.shape[1], axis=1
          )
          padded_patch = tf.cast(padded_patch, images.dtype)
  
      inverted_mask = (
          tf.constant(1, dtype=image_mask.dtype) - image_mask
      )
  
      return images * inverted_mask + padded_patch * image_mask
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  import math
  from typing import 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.estimators.classification.classifier import ClassifierMixin
  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 6 for calling the AdversarialPatchTensorFlowV2 obj's underlying member method code with example usage: obj._random_overlay(images, patch, scale, mask) and returns: unknown