class_docstr: This class implements a model-specific object detector using DEtection TRansformer (DETR)
  and PyTorch following the input and output formats of torchvision.
class_docstr_tok: ["This","class","implements","a","model-specific","object","detector","using","DEtection","TRansformer","(","DETR",")","and","PyTorch","following","the","input","and","output","formats","of","torchvision","."]
docstr: Initialization.
  
  :param model: DETR model. The output of the model is `List[Dict[Tensor]]`, one for each input image. The
                fields of the Dict are as follows:
  
                - boxes (FloatTensor[N, 4]): the predicted boxes in [x1, y1, x2, y2] format, with values                         between 0 and H and 0 and W
                - labels (Tensor[N]): the predicted labels for each image
                - scores (Tensor[N]): the scores or each prediction
  :param input_shape: Tuple of the form `(height, width)` of ints representing input image height and width
  :param clip_values: Tuple of the form `(min, max)` of floats or `np.ndarray` representing the minimum and
         maximum values allowed for features. If floats are provided, these will be used as the range of all
         features. If arrays are provided, each value will be considered the bound for a feature, thus
         the shape of clip values needs to match the total number of features.
  :param channels_first: Set channels first or last.
  :param preprocessing_defences: Preprocessing defence(s) to be applied by the estimator.
  :param postprocessing_defences: Postprocessing defence(s) to be applied by the estimator.
  :param preprocessing: Tuple of the form `(subtrahend, divisor)` of floats or `np.ndarray` of values to be
         used for data preprocessing. The first value will be subtracted from the input. The input will then
         be divided by the second one.
  :param device_type: Type of device to be used for model and tensors, if `cpu` run on CPU, if `gpu` run on GPU
                      if available otherwise run on CPU.
docstr_tok: ["Initialization",".",":","param","model",":","DETR","model",".","The","output","of","the","model","is","`","List","[","Dict","[","Tensor","]","]","`",",","one","for","each","input","image",".","The","fields","of","the","Dict","are","as","follows",":","-","boxes","(","FloatTensor","[","N",",","4","]",")",":","the","predicted","boxes","in","[","x1",",","y1",",","x2",",","y2","]","format",",","with","values","between","0","and","H","and","0","and","W","-","labels","(","Tensor","[","N","]",")",":","the","predicted","labels","for","each","image","-","scores","(","Tensor","[","N","]",")",":","the","scores","or","each","prediction",":","param","input_shape",":","Tuple","of","the","form","`","(","height",",","width",")","`","of","ints","representing","input","image","height","and","width",":","param","clip_values",":","Tuple","of","the","form","`","(","min",",","max",")","`","of","floats","or","`","np.ndarray","`","representing","the","minimum","and","maximum","values","allowed","for","features",".","If","floats","are","provided",",","these","will","be","used","as","the","range","of","all","features",".","If","arrays","are","provided",",","each","value","will","be","considered","the","bound","for","a","feature",",","thus","the","shape","of","clip","values","needs","to","match","the","total","number","of","features",".",":","param","channels_first",":","Set","channels","first","or","last",".",":","param","preprocessing_defences",":","Preprocessing","defence","(","s",")","to","be","applied","by","the","estimator",".",":","param","postprocessing_defences",":","Postprocessing","defence","(","s",")","to","be","applied","by","the","estimator",".",":","param","preprocessing",":","Tuple","of","the","form","`","(","subtrahend",",","divisor",")","`","of","floats","or","`","np.ndarray","`","of","values","to","be","used","for","data","preprocessing",".","The","first","value","will","be","subtracted","from","the","input",".","The","input","will","then","be","divided","by","the","second","one",".",":","param","device_type",":","Type","of","device","to","be","used","for","model","and","tensors",",","if","`","cpu","`","run","on","CPU",",","if","`","gpu","`","run","on","GPU","if","available","otherwise","run","on","CPU","."]
code: def __init__(
      self,
      model: Optional["torch.nn.Module"] = None,
      input_shape: Tuple[int, ...] = (3, 800, 800),
      clip_values: Optional["CLIP_VALUES_TYPE"] = None,
      channels_first: Optional[bool] = True,
      preprocessing_defences: Union[
          "Preprocessor", List["Preprocessor"], None
      ] = None,
      postprocessing_defences: Union[
          "Postprocessor", List["Postprocessor"], None
      ] = None,
      preprocessing: "PREPROCESSING_TYPE" = None,
      attack_losses: Tuple[str, ...] = (
          "loss_ce",
          "loss_bbox",
          "loss_giou",
      ),
      device_type: str = "gpu",
  ):
      """
          Initialization.
  
          :param model: DETR model. The output of the model is `List[Dict[Tensor]]`, one for each input image. The
                        fields of the Dict are as follows:
  
                        - boxes (FloatTensor[N, 4]): the predicted boxes in [x1, y1, x2, y2] format, with values \
                          between 0 and H and 0 and W
                        - labels (Tensor[N]): the predicted labels for each image
                        - scores (Tensor[N]): the scores or each prediction
          :param input_shape: Tuple of the form `(height, width)` of ints representing input image height and width
          :param clip_values: Tuple of the form `(min, max)` of floats or `np.ndarray` representing the minimum and
                 maximum values allowed for features. If floats are provided, these will be used as the range of all
                 features. If arrays are provided, each value will be considered the bound for a feature, thus
                 the shape of clip values needs to match the total number of features.
          :param channels_first: Set channels first or last.
          :param preprocessing_defences: Preprocessing defence(s) to be applied by the estimator.
          :param postprocessing_defences: Postprocessing defence(s) to be applied by the estimator.
          :param preprocessing: Tuple of the form `(subtrahend, divisor)` of floats or `np.ndarray` of values to be
                 used for data preprocessing. The first value will be subtracted from the input. The input will then
                 be divided by the second one.
          :param device_type: Type of device to be used for model and tensors, if `cpu` run on CPU, if `gpu` run on GPU
                              if available otherwise run on CPU.
          """
      import torch
      from art.estimators.object_detection.detr import (
          HungarianMatcher,
          SetCriterion,
          grad_enabled_forward,
      )
  
      if model is None:
          model = torch.hub.load(
              "facebookresearch/detr", "detr_resnet50", pretrained=True
          )
  
      func_type = type(model.forward)
      model.forward = func_type(grad_enabled_forward, model)  # type: ignore
  
      super().__init__(
          model=model,
          clip_values=clip_values,
          channels_first=channels_first,
          preprocessing_defences=preprocessing_defences,
          postprocessing_defences=postprocessing_defences,
          preprocessing=preprocessing,
          device_type=device_type,
      )
  
      # Check clip values
      if self.clip_values is not None:
          if not np.all(self.clip_values[0] == 0):
              raise ValueError(
                  "This estimator requires normalized input images with clip_vales=(0, 1)."
              )
          if not np.all(self.clip_values[1] == 1):  # pragma: no cover
              raise ValueError(
                  "This estimator requires normalized input images with clip_vales=(0, 1)."
              )
  
      if self.postprocessing_defences is not None:
          raise ValueError(
              "This estimator does not support `postprocessing_defences`."
          )
  
      self._input_shape = input_shape
      cost_class = 1.0
      cost_bbox = 5.0
      cost_giou = 2.0
      bbox_loss_coef = 5.0
      giou_loss_coef = 2.0
      eos_coef = 0.1
      self.max_norm = 0.1
      num_classes = 91
  
      matcher = HungarianMatcher(
          cost_class=cost_class,
          cost_bbox=cost_bbox,
          cost_giou=cost_giou,
      )
      self.weight_dict = {
          "loss_ce": 1,
          "loss_bbox": bbox_loss_coef,
          "loss_giou": giou_loss_coef,
      }
      losses = ["labels", "boxes", "cardinality"]
      self.criterion = SetCriterion(
          num_classes,
          matcher=matcher,
          weight_dict=self.weight_dict,
          eos_coef=eos_coef,
          losses=losses,
      )
  
      self._model.to(self._device)
      self._model.eval()
      self.attack_losses: Tuple[str, ...] = attack_losses
  
file_path: adversarial-robustness-toolbox/art/estimators/object_detection/pytorch_detection_transformer.py
imports: import logging
  from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union, Any
  import numpy
  from art.estimators.object_detection.object_detector import ObjectDetectorMixin
  from art.estimators.pytorch import PyTorchEstimator
label_desc: Use image node_id 1 to create a new PyTorchDetectionTransformer object from inherited base classes: ObjectDetectorMixin, PyTorchEstimator with example: obj = PyTorchDetectionTransformer(model, input_shape, clip_values, channels_first, preprocessing_defences, postprocessing_defences, preprocessing, attack_losses, device_type)