class_docstr: This module implements the model- and task specific estimator for YOLO v3, v5 object detector models in PyTorch.
  
  | Paper link: https://arxiv.org/abs/1804.02767
class_docstr_tok: ["This","module","implements","the","model-","and","task","specific","estimator","for","YOLO","v3",",","v5","object","detector","models","in","PyTorch",".","|","Paper","link",":","https",":","\/\/arxiv.org\/abs\/1804.02767"]
docstr: Compute the gradient of the loss function w.r.t. `x`.
  
  :param x: Samples of shape NCHW or NHWC.
  :param y: Target values of format `List[Dict[str, Union[np.ndarray, torch.Tensor]]]`, one for each input image.
            The fields of the Dict are as follows:
  
            - 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: 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","NCHW","or","NHWC",".",":","param","y",":","Target","values","of","format","`","List","[","Dict","[","str",",","Union","[","np.ndarray",",","torch.Tensor","]","]","]","`",",","one","for","each","input","image",".","The","fields","of","the","Dict","are","as","follows",":","-","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",":","Loss","gradients","of","the","same","shape","as","`","x","`","."]
code: def loss_gradient(  # pylint: disable=W0613
      self,
      x: Union[np.ndarray, "torch.Tensor"],
      y: List[Dict[str, Union[np.ndarray, "torch.Tensor"]]],
      **kwargs,
  ) -> Union[np.ndarray, "torch.Tensor"]:
      """
      Compute the gradient of the loss function w.r.t. `x`.
  
      :param x: Samples of shape NCHW or NHWC.
      :param y: Target values of format `List[Dict[str, Union[np.ndarray, torch.Tensor]]]`, one for each input image.
                The fields of the Dict are as follows:
  
                - 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: Loss gradients of the same shape as `x`.
      """
      import torch
  
      loss_components, x_grad = self._get_losses(x=x, y=y)
  
      # Compute the gradient and return
      loss = None
      for loss_name in self.attack_losses:
          if loss is None:
              loss = loss_components[loss_name]
          else:
              loss = loss + loss_components[loss_name]
  
      # Clean gradients
      self._model.zero_grad()
  
      # Compute gradients
      loss.backward(retain_graph=True)  # type: ignore
  
      if x_grad.grad is not None:
          if isinstance(x, np.ndarray):
              grads = x_grad.grad.cpu().numpy()
          else:
              grads = x_grad.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, grads)
  
      if not self.channels_first:
          if isinstance(x, np.ndarray):
              grads = np.transpose(grads, (0, 2, 3, 1))
          else:
              grads = torch.permute(grads, (0, 2, 3, 1))
  
      assert grads.shape == x.shape
  
      return grads
  
imports: import logging
  from typing import List, Dict, Optional, Tuple, Union, TYPE_CHECKING
  import numpy
  from art.estimators.object_detection.object_detector import ObjectDetectorMixin
  from art.estimators.object_detection.utils import cast_inputs_to_pt
  from art.estimators.pytorch import PyTorchEstimator
label_desc: Use image node_id 10 for calling the PyTorchYolo obj's underlying member method code with example usage: obj.loss_gradient(x, y) and returns: grads