class_docstr: Implementation of the adversarial patch attack for square and rectangular images and videos in Numpy.
  
  | Paper link: https://arxiv.org/abs/1712.09665
class_docstr_tok: ["Implementation","of","the","adversarial","patch","attack","for","square","and","rectangular","images","and","videos","in","Numpy",".","|","Paper","link",":","https",":","\/\/arxiv.org\/abs\/1712.09665"]
code: def _scale(self, x, scale):
      zooms = None
      height, width = x.shape[self.i_h], x.shape[self.i_w]
  
      if self.estimator.channels_first:
          if self.nb_dims == 3:
              zooms = (1.0, scale, scale)
          elif self.nb_dims == 4:
              zooms = (1.0, 1.0, scale, scale)
      elif not self.estimator.channels_first:
          if self.nb_dims == 3:
              zooms = (scale, scale, 1.0)
          elif self.nb_dims == 4:
              zooms = (1.0, scale, scale, 1.0)
  
      if scale < 1.0:
          scale_h = int(np.round(height * scale))
          scale_w = int(np.round(width * scale))
          top = (height - scale_h) // 2
          left = (width - scale_w) // 2
  
          x_out = np.zeros_like(x)
  
          if self.estimator.channels_first:
              if self.nb_dims == 3:
                  x_out[
                      :, top : top + scale_h, left : left + scale_w
                  ] = zoom(x, zoom=zooms, order=1)
              elif self.nb_dims == 4:
                  x_out[
                      :, :, top : top + scale_h, left : left + scale_w
                  ] = zoom(x, zoom=zooms, order=1)
          else:
              if self.nb_dims == 3:
                  x_out[
                      top : top + scale_h, left : left + scale_w, :
                  ] = zoom(x, zoom=zooms, order=1)
              elif self.nb_dims == 4:
                  x_out[
                      :, top : top + scale_h, left : left + scale_w, :
                  ] = zoom(x, zoom=zooms, order=1)
  
      elif scale > 1.0:
          scale_h = int(np.round(height / scale)) + 1
          scale_w = int(np.round(width / scale)) + 1
          top = (height - scale_h) // 2
          left = (width - scale_w) // 2
  
          if (
              scale_h <= height
              and scale_w <= width
              and top >= 0
              and left >= 0
          ):
              if self.estimator.channels_first:
                  if self.nb_dims == 3:
                      x_out = zoom(
                          x[
                              :,
                              top : top + scale_h,
                              left : left + scale_w,
                          ],
                          zoom=zooms,
                          order=1,
                      )
                  elif self.nb_dims == 4:
                      x_out = zoom(
                          x[
                              :,
                              :,
                              top : top + scale_h,
                              left : left + scale_w,
                          ],
                          zoom=zooms,
                          order=1,
                      )
              else:
                  if self.nb_dims == 3:
                      x_out = zoom(
                          x[
                              top : top + scale_h,
                              left : left + scale_w,
                              :,
                          ],
                          zoom=zooms,
                          order=1,
                      )
                  elif self.nb_dims == 4:
                      x_out = zoom(
                          x[
                              :,
                              top : top + scale_h,
                              left : left + scale_w,
                              :,
                          ],
                          zoom=zooms,
                          order=1,
                      )
  
          else:
              x_out = x
  
          cut_top = (x_out.shape[self.i_h] - height) // 2
          cut_left = (x_out.shape[self.i_w] - width) // 2
  
          if self.estimator.channels_first:
              if self.nb_dims == 3:
                  x_out = x_out[
                      :,
                      cut_top : cut_top + height,
                      cut_left : cut_left + width,
                  ]
              elif self.nb_dims == 4:
                  x_out = x_out[
                      :,
                      :,
                      cut_top : cut_top + height,
                      cut_left : cut_left + width,
                  ]
          else:
              if self.nb_dims == 3:
                  x_out = x_out[
                      cut_top : cut_top + height,
                      cut_left : cut_left + width,
                      :,
                  ]
              elif self.nb_dims == 4:
                  x_out = x_out[
                      :,
                      cut_top : cut_top + height,
                      cut_left : cut_left + width,
                      :,
                  ]
  
      else:
          x_out = x
  
      assert x.shape == x_out.shape
  
      return x_out
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  import math
  from typing import Optional, Union, Tuple, TYPE_CHECKING
  import random
  import numpy
  from scipy.ndimage import rotate, shift, zoom
  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
label_desc: Use image node_id 8 for calling the AdversarialPatchNumpy obj's underlying member method code with example usage: obj._scale(x, scale) and returns: x_out