class_docstr: Implementation of the BadDet Global Misclassification Attack.
  
  | Paper link: https://arxiv.org/abs/2205.14497
docstr: Generate poisoning examples by inserting the backdoor onto the input `x` and changing the classification
  for labels `y`.
  
  :param x: Sample images of shape `NCHW` or `NHWC` or a list of sample images of any size.
  :param y: True labels of type `List[Dict[np.ndarray]]`, one dictionary per input image. The keys and values
            of the dictionary are:
  
            - boxes [N, 4]: the boxes in [x1, y1, x2, y2] format, with 0 <= x1 < x2 <= W and 0 <= y1 < y2 <= H.
            - labels [N]: the labels for each image.
  :return: An tuple holding the `(poisoning_examples, poisoning_labels)`.
code: def poison(  # pylint: disable=W0221
      self,
      x: Union[np.ndarray, List[np.ndarray]],
      y: List[Dict[str, np.ndarray]],
      **kwargs,
  ) -> Tuple[
      Union[np.ndarray, List[np.ndarray]], List[Dict[str, np.ndarray]]
  ]:
      """
      Generate poisoning examples by inserting the backdoor onto the input `x` and changing the classification
      for labels `y`.
  
      :param x: Sample images of shape `NCHW` or `NHWC` or a list of sample images of any size.
      :param y: True labels of type `List[Dict[np.ndarray]]`, one dictionary per input image. The keys and values
                of the dictionary are:
  
                - boxes [N, 4]: the boxes in [x1, y1, x2, y2] format, with 0 <= x1 < x2 <= W and 0 <= y1 < y2 <= H.
                - labels [N]: the labels for each image.
      :return: An tuple holding the `(poisoning_examples, poisoning_labels)`.
      """
      if isinstance(x, np.ndarray):
          x_ndim = len(x.shape)
      else:
          x_ndim = len(x[0].shape) + 1
  
      if x_ndim != 4:
          raise ValueError(
              "Unrecognized input dimension. BadDet GMA can only be applied to image data."
          )
  
      # copy images
      x_poison: Union[np.ndarray, List[np.ndarray]]
      if isinstance(x, np.ndarray):
          x_poison = x.copy()
      else:
          x_poison = [x_i.copy() for x_i in x]
  
      # copy labels
      y_poison: List[Dict[str, np.ndarray]] = []
      for y_i in y:
          target_dict = {k: v.copy() for k, v in y_i.items()}
          y_poison.append(target_dict)
  
      # select indices of samples to poison
      all_indices = np.arange(len(x))
      num_poison = int(self.percent_poison * len(all_indices))
      selected_indices = np.random.choice(
          all_indices, num_poison, replace=False
      )
  
      for i in tqdm(
          selected_indices,
          desc="BadDet GMA iteration",
          disable=not self.verbose,
      ):
          image = x_poison[i]
          labels = y_poison[i]["labels"]
  
          if self.channels_first:
              image = np.transpose(image, (1, 2, 0))
  
          # insert backdoor into the image
          # add an additional dimension to create a batch of size 1
          poisoned_input, _ = self.backdoor.poison(
              image[np.newaxis], labels
          )
          image = poisoned_input[0]
  
          # replace the original image with the poisoned image
          if self.channels_first:
              image = np.transpose(image, (2, 0, 1))
          x_poison[i] = image
  
          # change all labels to the target label
          y_poison[i]["labels"] = np.full(
              labels.shape, self.class_target
          )
  
      return x_poison, y_poison
  
file_path: adversarial-robustness-toolbox/art/attacks/poisoning/bad_det/bad_det_gma.py
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  from typing import Dict, List, Tuple, Union
  import numpy
  from tqdm.auto import tqdm
  from art.attacks.attack import PoisoningAttackObjectDetector
  from art.attacks.poisoning.backdoor_attack import PoisoningAttackBackdoor
label_desc: Use image node_id 2 for calling the BadDetGlobalMisclassificationAttack obj's underlying member method code with example usage: obj.poison(x, y) and returns: x_poison,  y_poison