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: Get the loss tensor output of the model including all preprocessing.
  
  :param x: Samples of shape (nb_samples, nb_channels, height, width).
  :param y: Target values of format `List[Dict[Tensor]]`, one for each input image. The fields of the Dict are as
            follows:
            - boxes (FloatTensor[N, 4]): the boxes in [x1, y1, x2, y2] format, with 0 <= x1 < x2 <= W and
                                         0 <= y1 < y2 <= H.
            - labels (Int64Tensor[N]): the labels for each image
  :return: Loss gradients of the same shape as `x`.
docstr_tok: ["Get","the","loss","tensor","output","of","the","model","including","all","preprocessing",".",":","param","x",":","Samples","of","shape","(","nb_samples",",","nb_channels",",","height",",","width",")",".",":","param","y",":","Target","values","of","format","`","List","[","Dict","[","Tensor","]","]","`",",","one","for","each","input","image",".","The","fields","of","the","Dict","are","as","follows",":","-","boxes","(","FloatTensor","[","N",",","4","]",")",":","the","boxes","in","[","x1",",","y1",",","x2",",","y2","]","format",",","with","0","<","=","x1","<","x2","<","=","W","and","0","<","=","y1","<","y2","<","=","H.","-","labels","(","Int64Tensor","[","N","]",")",":","the","labels","for","each","image",":","return",":","Loss","gradients","of","the","same","shape","as","`","x","`","."]
code: def _get_losses(
      self,
      x: Union[np.ndarray, "torch.Tensor"],
      y: List[Dict[str, Union[np.ndarray, "torch.Tensor"]]],
  ) -> Tuple[Dict[str, "torch.Tensor"], "torch.Tensor", "torch.Tensor"]:
      """
      Get the loss tensor output of the model including all preprocessing.
  
      :param x: Samples of shape (nb_samples, nb_channels, height, width).
      :param y: Target values of format `List[Dict[Tensor]]`, one for each input image. The fields of the Dict are as
                follows:
                - boxes (FloatTensor[N, 4]): the boxes in [x1, y1, x2, y2] format, with 0 <= x1 < x2 <= W and
                                             0 <= y1 < y2 <= H.
                - labels (Int64Tensor[N]): the labels for each image
      :return: Loss gradients of the same shape as `x`.
      """
      import torch
  
      self._model.train()
  
      self.set_dropout(False)
      self.set_multihead_attention(False)
  
      if self.all_framework_preprocessing:
          if (
              y is not None
              and isinstance(y, list)
              and isinstance(y[0]["boxes"], np.ndarray)
          ):
              y_tensor = []
              for y_i in y:
                  y_t = {
                      "boxes": torch.from_numpy(y_i["boxes"])
                      .type(torch.float)
                      .to(self.device),
                      "labels": torch.from_numpy(y_i["labels"])
                      .type(torch.int64)
                      .to(self.device),
                  }
                  y_tensor.append(y_t)
          elif y is not None and isinstance(y, dict):
              y_tensor = []
              for i in range(y["boxes"].shape[0]):
                  y_t = {
                      "boxes": y["boxes"][i],
                      "labels": y["labels"][i],
                  }
                  y_tensor.append(y_t)
          else:
              y_tensor = y  # type: ignore
  
          if isinstance(x, np.ndarray):
              if self.clip_values is not None:
                  norm_factor = self.clip_values[1]
              else:
                  norm_factor = 1.0
  
              x_grad = torch.from_numpy(x / norm_factor).to(self.device)
              x_grad.requires_grad = True
  
          else:
              x_grad = x.to(self.device)
              if (
                  x_grad.shape[2] < x_grad.shape[0]
                  and x_grad.shape[2] < x_grad.shape[1]
              ):
                  x_grad = torch.permute(x_grad, (2, 0, 1)).to(
                      self.device
                  )
  
          image_tensor_list_grad = x_grad
          x_preprocessed, y_preprocessed = self._apply_preprocessing(
              x_grad, y=y_tensor, fit=False, no_grad=False
          )
          inputs_t = x_preprocessed
  
      elif isinstance(x, np.ndarray):
          if (
              y is not None
              and isinstance(y, list)
              and isinstance(y[0]["boxes"], np.ndarray)
          ):
              y_tensor = []
              for y_i in y:
                  y_t = {
                      "boxes": torch.from_numpy(y_i["boxes"])
                      .type(torch.float)
                      .to(self.device),
                      "labels": torch.from_numpy(y_i["labels"])
                      .type(torch.int64)
                      .to(self.device),
                  }
                  y_tensor.append(y_t)
          elif y is not None and isinstance(y[0]["boxes"], np.ndarray):
              y_tensor = []
              for y_i in y:
                  y_t = {
                      "boxes": torch.from_numpy(y_i["boxes"])
                      .type(torch.float)
                      .to(self.device),
                      "labels": torch.from_numpy(y_i["labels"])
                      .type(torch.int64)
                      .to(self.device),
                  }
                  y_tensor.append(y_t)
          else:
              y_tensor = y  # type: ignore
  
          x_preprocessed, y_preprocessed = self._apply_preprocessing(
              x, y=y_tensor, fit=False, no_grad=True
          )
  
          if self.clip_values is not None:
              norm_factor = self.clip_values[1]
          else:
              norm_factor = 1.0
  
          x_grad = torch.from_numpy(x_preprocessed / norm_factor).to(
              self.device
          )
          x_grad.requires_grad = True
          image_tensor_list_grad = x_grad
          inputs_t = image_tensor_list_grad
  
      else:
          raise NotImplementedError(
              "Combination of inputs and preprocessing not supported."
          )
  
      outputs = self._model(inputs_t)
      loss_components = self.criterion(outputs, y_preprocessed)
  
      return loss_components, inputs_t, image_tensor_list_grad
  
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 6 for calling the PyTorchDetectionTransformer obj's underlying member method code with example usage: obj._get_losses(x, y) and returns: loss_components,  inputs_t,  image_tensor_list_grad