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: Compute the gradient of the loss function w.r.t. `x`.
  
  :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 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
  :return: Loss gradients of the same shape as `x`.
docstr_tok: ["Compute","the","gradient","of","the","loss","function","w.r.t",".","`","x","`",".",":","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","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",":","return",":","Loss","gradients","of","the","same","shape","as","`","x","`","."]
code: def loss_gradient(
      self,
      x: Union[np.ndarray, "torch.Tensor"],
      y: List[Dict[str, "torch.Tensor"]],
      **kwargs,
  ) -> np.ndarray:
      """
          Compute the gradient of the loss function w.r.t. `x`.
  
          :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 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
          :return: Loss gradients of the same shape as `x`.
          """
      x_resized, y_resized = self._apply_resizing(x, y)
      output, inputs_t, image_tensor_list_grad = self._get_losses(
          x=x_resized, y=y_resized
      )
      loss = sum(
          output[k] * self.weight_dict[k]
          for k in output.keys()
          if k in self.weight_dict
      )
  
      self._model.zero_grad()
  
      loss.backward(retain_graph=True)  # type: ignore
  
      if isinstance(x_resized, np.ndarray):
          if image_tensor_list_grad.grad is not None:
              grads = image_tensor_list_grad.grad.cpu().numpy().copy()
          else:
              raise ValueError(
                  "Gradient term in PyTorch model is `None`."
              )
      else:
          if inputs_t.grad is not None:
              grads = inputs_t.grad.clone()
          else:
              raise ValueError(
                  "Gradient term in PyTorch model is `None`."
              )
  
      if self.clip_values is not None:
          grads = grads / self.clip_values[1]
  
      if not self.all_framework_preprocessing:
          grads = self._apply_preprocessing_gradient(x_resized, grads)
  
      return grads
  
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 7 for calling the PyTorchDetectionTransformer obj's underlying member method code with example usage: obj.loss_gradient(x, y) and returns: grads