class_docstr: Implements the DeformableDETR 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","DeformableDETR","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).
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)
          ].view(N, H, W, 1)
          valid_H = torch.sum(~mask_flatten_[:, :, 0, 0], 1)
          valid_W = torch.sum(~mask_flatten_[:, 0, :, 0], 1)
  
          grid_y, grid_x = torch.meshgrid(
              torch.linspace(
                  0, H - 1, H, dtype=torch.float32, device=memory.device
              ),
              torch.linspace(
                  0, W - 1, W, dtype=torch.float32, device=memory.device
              ),
          )
          grid = torch.cat(
              [grid_x.unsqueeze(-1), grid_y.unsqueeze(-1)], -1
          )
  
          scale = torch.cat(
              [valid_W.unsqueeze(-1), valid_H.unsqueeze(-1)], 1
          ).view(N, 1, 1, 2)
          grid = (grid.unsqueeze(0).expand(N, -1, -1, -1) + 0.5) / scale
          wh = torch.ones_like(grid) * 0.05 * (2.0**lvl)
          proposal = torch.cat((grid, wh), -1).view(N, -1, 4)
          proposals.append(proposal)
          _cur += H * W
      output_proposals = torch.cat(proposals, 1)
      output_proposals_valid = (
          (output_proposals > 0.01) & (output_proposals < 0.99)
      ).all(-1, keepdim=True)
      output_proposals = torch.log(
          output_proposals / (1 - output_proposals)
      )
      output_proposals = output_proposals.masked_fill(
          memory_padding_mask.unsqueeze(-1), float("inf")
      )
      output_proposals = output_proposals.masked_fill(
          ~output_proposals_valid, float("inf")
      )
  
      output_memory = memory
      output_memory = output_memory.masked_fill(
          memory_padding_mask.unsqueeze(-1), float(0)
      )
      output_memory = output_memory.masked_fill(
          ~output_proposals_valid, float(0)
      )
      output_memory = self.enc_output_norm(
          self.enc_output(output_memory)
      )
      return output_memory, output_proposals
  
imports: import math
  import warnings
  import torch
  import torch.nn
  from mmcv.cnn import build_norm_layer
  from mmcv.cnn.bricks.transformer import BaseTransformerLayer, TransformerLayerSequence, build_transformer_layer_sequence
  from mmcv.ops import MultiScaleDeformableAttention
  from mmengine.model import BaseModule
  from mmengine.model.weight_init import xavier_init
  from torch.nn.init import normal_
  from mmdet.models.layers.transformer import inverse_sigmoid
  from mmdet.registry import MODELS
label_desc: Use image node_id 4 for calling the DeformableDetrTransformer 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