class_docstr: Implements the PETR transformer.
  
  Args:
      as_two_stage (bool): Generate query from encoder features.
          Default: False.
      num_feature_levels (int): Number of feature maps from FPN:
          Default: 4.
      two_stage_num_proposals (int): Number of proposals when set
          `as_two_stage` as True. Default: 300.
class_docstr_tok: ["Implements","the","PETR","transformer",".","Args",":","as_two_stage","(","bool",")",":","Generate","query","from","encoder","features",".","Default",":","False",".","num_feature_levels","(","int",")",":","Number","of","feature","maps","from","FPN",":","Default",":","4.","two_stage_num_proposals","(","int",")",":","Number","of","proposals","when","set","`","as_two_stage","`","as","True",".","Default",":","300","."]
code: def forward_refine(
      self,
      mlvl_masks,
      memory,
      reference_points_pose,
      img_inds,
      kpt_branches=None,
      **kwargs,
  ):
      mask_flatten = []
      spatial_shapes = []
      for lvl, mask in enumerate(mlvl_masks):
          bs, h, w = mask.shape
          spatial_shape = (h, w)
          spatial_shapes.append(spatial_shape)
          mask = mask.flatten(1)
          mask_flatten.append(mask)
      mask_flatten = paddle.concat(mask_flatten, 1)
      spatial_shapes_cumsum = paddle.to_tensor(
          np.array(spatial_shapes, dtype="int64").prod(1).cumsum(0)
      )
      spatial_shapes = paddle.to_tensor(spatial_shapes, dtype="int64")
      level_start_index = paddle.concat(
          (
              paddle.zeros((1,), dtype=spatial_shapes.dtype),
              spatial_shapes_cumsum[:-1],
          )
      )
      valid_ratios = paddle.stack(
          [self.get_valid_ratio(m) for m in mlvl_masks], 1
      )
  
      # pose refinement (17 queries corresponding to 17 keypoints)
      # learnable query and query_pos
      refine_query_embedding = self.refine_query_embedding.weight
      query_pos, query = paddle.split(refine_query_embedding, 2, axis=1)
      pos_num = reference_points_pose.shape[0]
      query_pos = query_pos.unsqueeze(0).expand((pos_num, -1, -1))
      query = query.unsqueeze(0).expand((pos_num, -1, -1))
      reference_points = reference_points_pose.reshape(
          (pos_num, reference_points_pose.shape[1] // 2, 2)
      )
      pos_memory = memory[img_inds]
      mask_flatten = mask_flatten[img_inds]
      valid_ratios = valid_ratios[img_inds]
      if img_inds.size == 1:
          pos_memory = pos_memory.unsqueeze(0)
          mask_flatten = mask_flatten.unsqueeze(0)
          valid_ratios = valid_ratios.unsqueeze(0)
      inter_states, inter_references = self.refine_decoder(
          query=query,
          memory=pos_memory,
          query_pos_embed=query_pos,
          memory_mask=mask_flatten,
          reference_points=reference_points,
          value_spatial_shapes=spatial_shapes,
          value_level_start_index=level_start_index,
          valid_ratios=valid_ratios,
          reg_branches=kpt_branches,
          **kwargs,
      )
      # [num_decoder, num_query, bs, embed_dim]
  
      init_reference_out = reference_points
      return inter_states, init_reference_out, inter_references
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import math
  import numpy
  import paddle
  import paddle.nn
  import paddle.nn.functional
  from paddle import ParamAttr
  from ppdet.core.workspace import register
  from ..layers import MultiHeadAttention, _convert_attention_mask
  from .utils import _get_clones
  from ..initializer import linear_init_, normal_, constant_, xavier_uniform_
label_desc: Use image node_id 9 for calling the PETRTransformer obj's underlying member method code with example usage: obj.forward_refine(mlvl_masks, memory, reference_points_pose, img_inds, kpt_branches) and returns: inter_states,  init_reference_out,  inter_references