class_docstr: Head of `End-to-End Multi-Person Pose Estimation with Transformers`.
  
  Args:
      num_classes (int): Number of categories excluding the background.
      in_channels (int): Number of channels in the input feature map.
      num_query (int): Number of query in Transformer.
      num_kpt_fcs (int, optional): Number of fully-connected layers used in
          `FFN`, which is then used for the keypoint regression head.
          Default 2.
      transformer (obj:`mmcv.ConfigDict`|dict): ConfigDict is used for
          building the Encoder and Decoder. Default: None.
      sync_cls_avg_factor (bool): Whether to sync the avg_factor of
          all ranks. Default to False.
      positional_encoding (obj:`mmcv.ConfigDict`|dict):
          Config for position encoding.
      loss_cls (obj:`mmcv.ConfigDict`|dict): Config of the
          classification loss. Default `CrossEntropyLoss`.
      loss_kpt (obj:`mmcv.ConfigDict`|dict): Config of the
          regression loss. Default `L1Loss`.
      loss_oks (obj:`mmcv.ConfigDict`|dict): Config of the
          regression oks loss. Default `OKSLoss`.
      loss_hm (obj:`mmcv.ConfigDict`|dict): Config of the
          regression heatmap loss. Default `NegLoss`.
      as_two_stage (bool) : Whether to generate the proposal from
          the outputs of encoder.
      with_kpt_refine (bool): Whether to refine the reference points
          in the decoder. Defaults to True.
      test_cfg (obj:`mmcv.ConfigDict`|dict): Testing config of
          transformer head.
      init_cfg (dict or list[dict], optional): Initialization config dict.
          Default: None.
class_docstr_tok: ["Head","of","`","End-to-End","Multi-Person","Pose","Estimation","with","Transformers","`",".","Args",":","num_classes","(","int",")",":","Number","of","categories","excluding","the","background",".","in_channels","(","int",")",":","Number","of","channels","in","the","input","feature","map",".","num_query","(","int",")",":","Number","of","query","in","Transformer",".","num_kpt_fcs","(","int",",","optional",")",":","Number","of","fully-connected","layers","used","in","`","FFN","`",",","which","is","then","used","for","the","keypoint","regression","head",".","Default","2.","transformer","(","obj",":","`","mmcv.ConfigDict","`","|dict",")",":","ConfigDict","is","used","for","building","the","Encoder","and","Decoder",".","Default",":","None",".","sync_cls_avg_factor","(","bool",")",":","Whether","to","sync","the","avg_factor","of","all","ranks",".","Default","to","False",".","positional_encoding","(","obj",":","`","mmcv.ConfigDict","`","|dict",")",":","Config","for","position","encoding",".","loss_cls","(","obj",":","`","mmcv.ConfigDict","`","|dict",")",":","Config","of","the","classification","loss",".","Default","`","CrossEntropyLoss","`",".","loss_kpt","(","obj",":","`","mmcv.ConfigDict","`","|dict",")",":","Config","of","the","regression","loss",".","Default","`","L1Loss","`",".","loss_oks","(","obj",":","`","mmcv.ConfigDict","`","|dict",")",":","Config","of","the","regression","oks","loss",".","Default","`","OKSLoss","`",".","loss_hm","(","obj",":","`","mmcv.ConfigDict","`","|dict",")",":","Config","of","the","regression","heatmap","loss",".","Default","`","NegLoss","`",".","as_two_stage","(","bool",")",":","Whether","to","generate","the","proposal","from","the","outputs","of","encoder",".","with_kpt_refine","(","bool",")",":","Whether","to","refine","the","reference","points","in","the","decoder",".","Defaults","to","True",".","test_cfg","(","obj",":","`","mmcv.ConfigDict","`","|dict",")",":","Testing","config","of","transformer","head",".","init_cfg","(","dict","or","list","[","dict","]",",","optional",")",":","Initialization","config","dict",".","Default",":","None","."]
docstr: Loss function.
  
  Args:
      all_cls_scores (Tensor): Classification score of all
          decoder layers, has shape
          [nb_dec, bs, num_query, cls_out_channels].
      all_kpt_preds (Tensor): Sigmoid regression
          outputs of all decode layers. Each is a 4D-tensor with
          normalized coordinate format (x_{i}, y_{i}) and shape
          [nb_dec, bs, num_query, K*2].
      enc_cls_scores (Tensor): Classification scores of
          points on encode feature map, has shape
          (N, h*w, num_classes). Only be passed when as_two_stage is
          True, otherwise is None.
      enc_kpt_preds (Tensor): Regression results of each points
          on the encode feature map, has shape (N, h*w, K*2). Only be
          passed when as_two_stage is True, otherwise is None.
      gt_bboxes_list (list[Tensor]): Ground truth bboxes for each image
          with shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format.
      gt_labels_list (list[Tensor]): Ground truth class indices for each
          image with shape (num_gts, ).
      gt_keypoints_list (list[Tensor]): Ground truth keypoints for each
          image with shape (num_gts, K*3) in [p^{1}_x, p^{1}_y, p^{1}_v,
              ..., p^{K}_x, p^{K}_y, p^{K}_v] format.
      gt_areas_list (list[Tensor]): Ground truth mask areas for each
          image with shape (num_gts, ).
      img_metas (list[dict]): List of image meta information.
      gt_bboxes_ignore (list[Tensor], optional): Bounding boxes
          which can be ignored for each image. Default None.
  
  Returns:
      dict[str, Tensor]: A dictionary of loss components.
docstr_tok: ["Loss","function",".","Args",":","all_cls_scores","(","Tensor",")",":","Classification","score","of","all","decoder","layers",",","has","shape","[","nb_dec",",","bs",",","num_query",",","cls_out_channels","]",".","all_kpt_preds","(","Tensor",")",":","Sigmoid","regression","outputs","of","all","decode","layers",".","Each","is","a","4D-tensor","with","normalized","coordinate","format","(","x_","{","i","}",",","y_","{","i","}",")","and","shape","[","nb_dec",",","bs",",","num_query",",","K","*","2","]",".","enc_cls_scores","(","Tensor",")",":","Classification","scores","of","points","on","encode","feature","map",",","has","shape","(","N",",","h","*","w",",","num_classes",")",".","Only","be","passed","when","as_two_stage","is","True",",","otherwise","is","None",".","enc_kpt_preds","(","Tensor",")",":","Regression","results","of","each","points","on","the","encode","feature","map",",","has","shape","(","N",",","h","*","w",",","K","*","2",")",".","Only","be","passed","when","as_two_stage","is","True",",","otherwise","is","None",".","gt_bboxes_list","(","list","[","Tensor","]",")",":","Ground","truth","bboxes","for","each","image","with","shape","(","num_gts",",","4",")","in","[","tl_x",",","tl_y",",","br_x",",","br_y","]","format",".","gt_labels_list","(","list","[","Tensor","]",")",":","Ground","truth","class","indices","for","each","image","with","shape","(","num_gts",",",")",".","gt_keypoints_list","(","list","[","Tensor","]",")",":","Ground","truth","keypoints","for","each","image","with","shape","(","num_gts",",","K","*","3",")","in","[","p^","{","1","}","_x",",","p^","{","1","}","_y",",","p^","{","1","}","_v",",","...",",","p^","{","K","}","_x",",","p^","{","K","}","_y",",","p^","{","K","}","_v","]","format",".","gt_areas_list","(","list","[","Tensor","]",")",":","Ground","truth","mask","areas","for","each","image","with","shape","(","num_gts",",",")",".","img_metas","(","list","[","dict","]",")",":","List","of","image","meta","information",".","gt_bboxes_ignore","(","list","[","Tensor","]",",","optional",")",":","Bounding","boxes","which","can","be","ignored","for","each","image",".","Default","None",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def loss(
      self,
      all_cls_scores,
      all_kpt_preds,
      enc_cls_scores,
      enc_kpt_preds,
      enc_hm_proto,
      gt_bboxes_list,
      gt_labels_list,
      gt_keypoints_list,
      gt_areas_list,
      img_metas,
      gt_bboxes_ignore=None,
  ):
      """Loss function.
  
      Args:
          all_cls_scores (Tensor): Classification score of all
              decoder layers, has shape
              [nb_dec, bs, num_query, cls_out_channels].
          all_kpt_preds (Tensor): Sigmoid regression
              outputs of all decode layers. Each is a 4D-tensor with
              normalized coordinate format (x_{i}, y_{i}) and shape
              [nb_dec, bs, num_query, K*2].
          enc_cls_scores (Tensor): Classification scores of
              points on encode feature map, has shape
              (N, h*w, num_classes). Only be passed when as_two_stage is
              True, otherwise is None.
          enc_kpt_preds (Tensor): Regression results of each points
              on the encode feature map, has shape (N, h*w, K*2). Only be
              passed when as_two_stage is True, otherwise is None.
          gt_bboxes_list (list[Tensor]): Ground truth bboxes for each image
              with shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format.
          gt_labels_list (list[Tensor]): Ground truth class indices for each
              image with shape (num_gts, ).
          gt_keypoints_list (list[Tensor]): Ground truth keypoints for each
              image with shape (num_gts, K*3) in [p^{1}_x, p^{1}_y, p^{1}_v,
                  ..., p^{K}_x, p^{K}_y, p^{K}_v] format.
          gt_areas_list (list[Tensor]): Ground truth mask areas for each
              image with shape (num_gts, ).
          img_metas (list[dict]): List of image meta information.
          gt_bboxes_ignore (list[Tensor], optional): Bounding boxes
              which can be ignored for each image. Default None.
  
      Returns:
          dict[str, Tensor]: A dictionary of loss components.
      """
      assert gt_bboxes_ignore is None, (
          f"{self.__class__.__name__} only supports "
          f"for gt_bboxes_ignore setting to None."
      )
  
      num_dec_layers = len(all_cls_scores)
      all_gt_labels_list = [
          gt_labels_list for _ in range(num_dec_layers)
      ]
      all_gt_keypoints_list = [
          gt_keypoints_list for _ in range(num_dec_layers)
      ]
      all_gt_areas_list = [gt_areas_list for _ in range(num_dec_layers)]
      img_metas_list = [img_metas for _ in range(num_dec_layers)]
  
      (
          losses_cls,
          losses_kpt,
          losses_oks,
          kpt_preds_list,
          kpt_targets_list,
          area_targets_list,
          kpt_weights_list,
      ) = multi_apply(
          self.loss_single,
          all_cls_scores,
          all_kpt_preds,
          all_gt_labels_list,
          all_gt_keypoints_list,
          all_gt_areas_list,
          img_metas_list,
      )
  
      loss_dict = dict()
      # loss of proposal generated from encode feature map.
      if enc_cls_scores is not None:
          binary_labels_list = [
              paddle.zeros_like(gt_labels_list[i])
              for i in range(len(img_metas))
          ]
          enc_loss_cls, enc_losses_kpt = self.loss_single_rpn(
              enc_cls_scores,
              enc_kpt_preds,
              binary_labels_list,
              gt_keypoints_list,
              gt_areas_list,
              img_metas,
          )
          loss_dict["enc_loss_cls"] = enc_loss_cls
          loss_dict["enc_loss_kpt"] = enc_losses_kpt
  
      # loss from the last decoder layer
      loss_dict["loss_cls"] = losses_cls[-1]
      loss_dict["loss_kpt"] = losses_kpt[-1]
      loss_dict["loss_oks"] = losses_oks[-1]
      # loss from other decoder layers
      num_dec_layer = 0
      for loss_cls_i, loss_kpt_i, loss_oks_i in zip(
          losses_cls[:-1], losses_kpt[:-1], losses_oks[:-1]
      ):
          loss_dict[f"d{num_dec_layer}.loss_cls"] = loss_cls_i
          loss_dict[f"d{num_dec_layer}.loss_kpt"] = loss_kpt_i
          loss_dict[f"d{num_dec_layer}.loss_oks"] = loss_oks_i
          num_dec_layer += 1
  
      # losses of heatmap generated from P3 feature map
      hm_pred, hm_mask = enc_hm_proto
      loss_hm = self.loss_heatmap(
          hm_pred,
          hm_mask,
          gt_keypoints_list,
          gt_labels_list,
          gt_bboxes_list,
      )
      loss_dict["loss_hm"] = loss_hm
  
      return loss_dict, (
          kpt_preds_list[-1],
          kpt_targets_list[-1],
          area_targets_list[-1],
          kpt_weights_list[-1],
      )
  
imports: import copy
  import numpy
  import paddle
  import paddle.nn
  import paddle.nn.functional
  from ppdet.core.workspace import register
  import paddle.distributed
  from ..transformers.petr_transformer import inverse_sigmoid, masked_fill
  from ..initializer import constant_, normal_
  from functools import partial
label_desc: Use image node_id 7 for calling the PETRHead obj's underlying member method code with example usage: obj.loss(all_cls_scores, all_kpt_preds, enc_cls_scores, enc_kpt_preds, enc_hm_proto, gt_bboxes_list, gt_labels_list, gt_keypoints_list, gt_areas_list, img_metas, gt_bboxes_ignore) and returns: loss_dict,  kpt_preds_list,  kpt_targets_list,  area_targets_list,  kpt_weights_list