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: Forward function.
  
  Args:
      mlvl_masks (tuple[Tensor]): The key_padding_mask from
          different level used for encoder and decoder,
          each is a 3D-tensor with shape (bs, H, W).
      losses (dict[str, Tensor]): A dictionary of loss components.
      img_metas (list[dict]): List of image information.
  
  Returns:
      dict[str, Tensor]: A dictionary of loss components.
docstr_tok: ["Forward","function",".","Args",":","mlvl_masks","(","tuple","[","Tensor","]",")",":","The","key_padding_mask","from","different","level","used","for","encoder","and","decoder",",","each","is","a","3D-tensor","with","shape","(","bs",",","H",",","W",")",".","losses","(","dict","[","str",",","Tensor","]",")",":","A","dictionary","of","loss","components",".","img_metas","(","list","[","dict","]",")",":","List","of","image","information",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def forward_refine(
      self, memory, mlvl_masks, refine_targets, losses, img_metas
  ):
      """Forward function.
  
      Args:
          mlvl_masks (tuple[Tensor]): The key_padding_mask from
              different level used for encoder and decoder,
              each is a 3D-tensor with shape (bs, H, W).
          losses (dict[str, Tensor]): A dictionary of loss components.
          img_metas (list[dict]): List of image information.
  
      Returns:
          dict[str, Tensor]: A dictionary of loss components.
      """
      kpt_preds, kpt_targets, area_targets, kpt_weights = refine_targets
      pos_inds = kpt_weights.sum(-1) > 0
      if not pos_inds.any():
          pos_kpt_preds = paddle.zeros_like(kpt_preds[:1])
          pos_img_inds = paddle.zeros([1], dtype="int64")
      else:
          pos_kpt_preds = kpt_preds[pos_inds]
          pos_img_inds = (
              (pos_inds.nonzero() / self.num_query)
              .squeeze(1)
              .astype("int64")
          )
      (
          hs,
          init_reference,
          inter_references,
      ) = self.transformer.forward_refine(
          mlvl_masks,
          memory,
          pos_kpt_preds.detach(),
          pos_img_inds,
          kpt_branches=self.refine_kpt_branches
          if self.with_kpt_refine
          else None,  # noqa:E501
      )
  
      outputs_kpts = []
  
      for lvl in range(hs.shape[0]):
          if lvl == 0:
              reference = init_reference
          else:
              reference = inter_references[lvl - 1]
          reference = inverse_sigmoid(reference)
          tmp_kpt = self.refine_kpt_branches[lvl](hs[lvl])
          assert reference.shape[-1] == 2
          tmp_kpt += reference
          outputs_kpt = F.sigmoid(tmp_kpt)
          outputs_kpts.append(outputs_kpt)
      outputs_kpts = paddle.stack(outputs_kpts)
  
      if not self.training:
          return outputs_kpts
  
      num_valid_kpt = paddle.clip(
          reduce_mean(kpt_weights.sum()), min=1
      ).item()
      num_total_pos = paddle.to_tensor(
          [outputs_kpts.shape[1]], dtype=kpt_weights.dtype
      )
      num_total_pos = paddle.clip(
          reduce_mean(num_total_pos), min=1
      ).item()
  
      if not pos_inds.any():
          for i, kpt_refine_preds in enumerate(outputs_kpts):
              loss_kpt = loss_oks = kpt_refine_preds.sum() * 0
              losses[f"d{i}.loss_kpt_refine"] = loss_kpt
              losses[f"d{i}.loss_oks_refine"] = loss_oks
              continue
          return losses
  
      batch_size = mlvl_masks[0].shape[0]
      factors = []
      for img_id in range(batch_size):
          img_h, img_w, _ = img_metas[img_id]["img_shape"]
          factor = (
              paddle.to_tensor(
                  [img_w, img_h, img_w, img_h], dtype="float32"
              )
              .squeeze(-1)
              .unsqueeze(0)
              .tile((self.num_query, 1))
          )
          factors.append(factor)
      factors = paddle.concat(factors, 0)
      factors = factors[pos_inds][:, :2].tile(
          (1, kpt_preds.shape[-1] // 2)
      )
  
      pos_kpt_weights = kpt_weights[pos_inds]
      pos_kpt_targets = kpt_targets[pos_inds]
      pos_kpt_targets_scaled = pos_kpt_targets * factors
      pos_areas = area_targets[pos_inds]
      pos_valid = kpt_weights[pos_inds][:, 0::2]
      for i, kpt_refine_preds in enumerate(outputs_kpts):
          if not pos_inds.any():
              print("refine kpt and oks skip")
              loss_kpt = loss_oks = kpt_refine_preds.sum() * 0
              losses[f"d{i}.loss_kpt_refine"] = loss_kpt
              losses[f"d{i}.loss_oks_refine"] = loss_oks
              continue
  
          # kpt L1 Loss
          pos_refine_preds = kpt_refine_preds.reshape(
              (kpt_refine_preds.shape[0], -1)
          )
          loss_kpt = self.loss_kpt_refine(
              pos_refine_preds,
              pos_kpt_targets,
              pos_kpt_weights,
              avg_factor=num_valid_kpt,
          )
          losses[f"d{i}.loss_kpt_refine"] = loss_kpt
          # kpt oks loss
          pos_refine_preds_scaled = pos_refine_preds * factors
          assert (pos_areas > 0).all()
          loss_oks = self.loss_oks_refine(
              pos_refine_preds_scaled,
              pos_kpt_targets_scaled,
              pos_valid,
              pos_areas,
              avg_factor=num_total_pos,
          )
          losses[f"d{i}.loss_oks_refine"] = loss_oks
      return losses
  
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 5 for calling the PETRHead obj's underlying member method code with example usage: obj.forward_refine(memory, mlvl_masks, refine_targets, losses, img_metas) and returns: losses, outputs_kpts, losses