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","."]
docstr: Generate proposals from encoded memory.
  
  Args:
      memory (Tensor): The output of encoder, has shape
          (bs, num_key, embed_dim). num_key is equal the number of points
          on feature map from all level.
      memory_padding_mask (Tensor): Padding mask for memory.
          has shape (bs, num_key).
      spatial_shapes (Tensor): The shape of all feature maps.
          has shape (num_level, 2).
  
  Returns:
      tuple: A tuple of feature map and bbox prediction.
  
          - output_memory (Tensor): The input of decoder, has shape
              (bs, num_key, embed_dim). num_key is equal the number of
              points on feature map from all levels.
          - output_proposals (Tensor): The normalized proposal
              after a inverse sigmoid, has shape (bs, num_keys, 4).
docstr_tok: ["Generate","proposals","from","encoded","memory",".","Args",":","memory","(","Tensor",")",":","The","output","of","encoder",",","has","shape","(","bs",",","num_key",",","embed_dim",")",".","num_key","is","equal","the","number","of","points","on","feature","map","from","all","level",".","memory_padding_mask","(","Tensor",")",":","Padding","mask","for","memory",".","has","shape","(","bs",",","num_key",")",".","spatial_shapes","(","Tensor",")",":","The","shape","of","all","feature","maps",".","has","shape","(","num_level",",","2",")",".","Returns",":","tuple",":","A","tuple","of","feature","map","and","bbox","prediction",".","-","output_memory","(","Tensor",")",":","The","input","of","decoder",",","has","shape","(","bs",",","num_key",",","embed_dim",")",".","num_key","is","equal","the","number","of","points","on","feature","map","from","all","levels",".","-","output_proposals","(","Tensor",")",":","The","normalized","proposal","after","a","inverse","sigmoid",",","has","shape","(","bs",",","num_keys",",","4",")","."]
code: def gen_encoder_output_proposals(
      self, memory, memory_padding_mask, spatial_shapes
  ):
      """Generate proposals from encoded memory.
  
      Args:
          memory (Tensor): The output of encoder, has shape
              (bs, num_key, embed_dim). num_key is equal the number of points
              on feature map from all level.
          memory_padding_mask (Tensor): Padding mask for memory.
              has shape (bs, num_key).
          spatial_shapes (Tensor): The shape of all feature maps.
              has shape (num_level, 2).
  
      Returns:
          tuple: A tuple of feature map and bbox prediction.
  
              - output_memory (Tensor): The input of decoder, has shape
                  (bs, num_key, embed_dim). num_key is equal the number of
                  points on feature map from all levels.
              - output_proposals (Tensor): The normalized proposal
                  after a inverse sigmoid, has shape (bs, num_keys, 4).
      """
  
      N, S, C = memory.shape
      proposals = []
      _cur = 0
      for lvl, (H, W) in enumerate(spatial_shapes):
          mask_flatten_ = memory_padding_mask[
              :, _cur : (_cur + H * W)
          ].reshape([N, H, W, 1])
          valid_H = paddle.sum(mask_flatten_[:, :, 0, 0], 1)
          valid_W = paddle.sum(mask_flatten_[:, 0, :, 0], 1)
  
          grid_y, grid_x = paddle.meshgrid(
              paddle.linspace(0, H - 1, H, dtype="float32"),
              paddle.linspace(0, W - 1, W, dtype="float32"),
          )
          grid = paddle.concat(
              [grid_x.unsqueeze(-1), grid_y.unsqueeze(-1)], -1
          )
  
          scale = paddle.concat(
              [valid_W.unsqueeze(-1), valid_H.unsqueeze(-1)], 1
          ).reshape([N, 1, 1, 2])
          grid = (
              grid.unsqueeze(0).expand((N, -1, -1, -1)) + 0.5
          ) / scale
          proposal = grid.reshape([N, -1, 2])
          proposals.append(proposal)
          _cur += H * W
      output_proposals = paddle.concat(proposals, 1)
      output_proposals_valid = (
          ((output_proposals > 0.01) & (output_proposals < 0.99))
          .all(-1, keepdim=True)
          .astype("bool")
      )
      output_proposals = paddle.log(
          output_proposals / (1 - output_proposals)
      )
      output_proposals = masked_fill(
          output_proposals,
          ~memory_padding_mask.astype("bool").unsqueeze(-1),
          float("inf"),
      )
      output_proposals = masked_fill(
          output_proposals, ~output_proposals_valid, float("inf")
      )
  
      output_memory = memory
      output_memory = masked_fill(
          output_memory,
          ~memory_padding_mask.astype("bool").unsqueeze(-1),
          float(0),
      )
      output_memory = masked_fill(
          output_memory, ~output_proposals_valid, float(0)
      )
      output_memory = self.enc_output_norm(
          self.enc_output(output_memory)
      )
      return output_memory, output_proposals
  
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 4 for calling the PETRTransformer obj's underlying member method code with example usage: obj.gen_encoder_output_proposals(memory, memory_padding_mask, spatial_shapes) and returns: output_memory,  output_proposals