class_docstr: Implements the DETR transformer.
  
  Following the official DETR implementation, this module copy-paste
  from torch.nn.Transformer with modifications:
  
      * positional encodings are passed in MultiheadAttention
      * extra LN at the end of encoder is removed
      * decoder returns a stack of activations from all decoding layers
  
  See `paper: End-to-End Object Detection with Transformers
  <https://arxiv.org/pdf/2005.12872>`_ for details.
  
  Args:
      embed_dims (int): The feature dimension.
      num_heads (int): Parallel attention heads. Same as
          `nn.MultiheadAttention`.
      num_encoder_layers (int): Number of `TransformerEncoderLayer`.
      num_decoder_layers (int): Number of `TransformerDecoderLayer`.
      feedforward_channels (int): The hidden dimension for FFNs used in both
          encoder and decoder.
      dropout (float): Probability of an element to be zeroed. Default 0.0.
      act_cfg (dict): Activation config for FFNs used in both encoder
          and decoder. Defalut ReLU.
      norm_cfg (dict): Config dict for normalization used in both encoder
          and decoder. Default layer normalization.
      num_fcs (int): The number of fully-connected layers in FFNs, which is
          used for both encoder and decoder.
      pre_norm (bool): Whether the normalization layer is ordered
          first in the encoder and decoder. Default False.
      return_intermediate_dec (bool): Whether to return the intermediate
          output from each TransformerDecoderLayer or only the last
          TransformerDecoderLayer. Default False. If False, the returned
          `hs` has shape [num_decoder_layers, bs, num_query, embed_dims].
          If True, the returned `hs` will have shape [1, bs, num_query,
          embed_dims].
class_docstr_tok: ["Implements","the","DETR","transformer",".","Following","the","official","DETR","implementation",",","this","module","copy-paste","from","torch.nn.Transformer","with","modifications",":","*","positional","encodings","are","passed","in","MultiheadAttention","*","extra","LN","at","the","end","of","encoder","is","removed","*","decoder","returns","a","stack","of","activations","from","all","decoding","layers","See","`","paper",":","End-to-End","Object","Detection","with","Transformers","<","https",":","\/\/arxiv.org\/pdf\/2005.12872",">","`","_","for","details",".","Args",":","embed_dims","(","int",")",":","The","feature","dimension",".","num_heads","(","int",")",":","Parallel","attention","heads",".","Same","as","`","nn.MultiheadAttention","`",".","num_encoder_layers","(","int",")",":","Number","of","`","TransformerEncoderLayer","`",".","num_decoder_layers","(","int",")",":","Number","of","`","TransformerDecoderLayer","`",".","feedforward_channels","(","int",")",":","The","hidden","dimension","for","FFNs","used","in","both","encoder","and","decoder",".","dropout","(","float",")",":","Probability","of","an","element","to","be","zeroed",".","Default","0.0.","act_cfg","(","dict",")",":","Activation","config","for","FFNs","used","in","both","encoder","and","decoder",".","Defalut","ReLU",".","norm_cfg","(","dict",")",":","Config","dict","for","normalization","used","in","both","encoder","and","decoder",".","Default","layer","normalization",".","num_fcs","(","int",")",":","The","number","of","fully-connected","layers","in","FFNs",",","which","is","used","for","both","encoder","and","decoder",".","pre_norm","(","bool",")",":","Whether","the","normalization","layer","is","ordered","first","in","the","encoder","and","decoder",".","Default","False",".","return_intermediate_dec","(","bool",")",":","Whether","to","return","the","intermediate","output","from","each","TransformerDecoderLayer","or","only","the","last","TransformerDecoderLayer",".","Default","False",".","If","False",",","the","returned","`","hs","`","has","shape","[","num_decoder_layers",",","bs",",","num_query",",","embed_dims","]",".","If","True",",","the","returned","`","hs","`","will","have","shape","[","1",",","bs",",","num_query",",","embed_dims","]","."]
docstr: Forward function for `Transformer`.
  
  Args:
      x (Tensor): Input query with shape [bs, c, h, w] where
          c = embed_dims.
      mask (Tensor): The key_padding_mask used for encoder and decoder,
          with shape [bs, h, w].
      query_embed (Tensor): The query embedding for decoder, with shape
          [num_query, c].
      pos_embed (Tensor): The positional encoding for encoder and
          decoder, with the same shape as `x`.
  
  Returns:
      tuple[Tensor]: results of decoder containing the following tensor.
  
          - out_dec: Output from decoder. If return_intermediate_dec                       is True output has shape [num_dec_layers, bs,
                num_query, embed_dims], else has shape [1, bs,                       num_query, embed_dims].
          - memory: Output results from encoder, with shape                       [bs, embed_dims, h, w].
docstr_tok: ["Forward","function","for","`","Transformer","`",".","Args",":","x","(","Tensor",")",":","Input","query","with","shape","[","bs",",","c",",","h",",","w","]","where","c","=","embed_dims",".","mask","(","Tensor",")",":","The","key_padding_mask","used","for","encoder","and","decoder",",","with","shape","[","bs",",","h",",","w","]",".","query_embed","(","Tensor",")",":","The","query","embedding","for","decoder",",","with","shape","[","num_query",",","c","]",".","pos_embed","(","Tensor",")",":","The","positional","encoding","for","encoder","and","decoder",",","with","the","same","shape","as","`","x","`",".","Returns",":","tuple","[","Tensor","]",":","results","of","decoder","containing","the","following","tensor",".","-","out_dec",":","Output","from","decoder",".","If","return_intermediate_dec","is","True","output","has","shape","[","num_dec_layers",",","bs",",","num_query",",","embed_dims","]",",","else","has","shape","[","1",",","bs",",","num_query",",","embed_dims","]",".","-","memory",":","Output","results","from","encoder",",","with","shape","[","bs",",","embed_dims",",","h",",","w","]","."]
code: def forward(self, x, mask, query_embed, pos_embed):
      """Forward function for `Transformer`.
  
          Args:
              x (Tensor): Input query with shape [bs, c, h, w] where
                  c = embed_dims.
              mask (Tensor): The key_padding_mask used for encoder and decoder,
                  with shape [bs, h, w].
              query_embed (Tensor): The query embedding for decoder, with shape
                  [num_query, c].
              pos_embed (Tensor): The positional encoding for encoder and
                  decoder, with the same shape as `x`.
  
          Returns:
              tuple[Tensor]: results of decoder containing the following tensor.
  
                  - out_dec: Output from decoder. If return_intermediate_dec \
                        is True output has shape [num_dec_layers, bs,
                        num_query, embed_dims], else has shape [1, bs, \
                        num_query, embed_dims].
                  - memory: Output results from encoder, with shape \
                        [bs, embed_dims, h, w].
          """
      bs, c, h, w = x.shape
      x = x.flatten(2).permute(2, 0, 1)  # [bs, c, h, w] -> [h*w, bs, c]
      pos_embed = pos_embed.flatten(2).permute(2, 0, 1)
      query_embed = query_embed.unsqueeze(1).repeat(
          1, bs, 1
      )  # [num_query, dim] -> [num_query, bs, dim]
      mask = mask.flatten(1)  # [bs, h, w] -> [bs, h*w]
      memory = self.encoder(
          x, pos=pos_embed, attn_mask=None, key_padding_mask=mask
      )
      target = torch.zeros_like(query_embed)
      # out_dec: [num_layers, num_query, bs, dim]
      out_dec = self.decoder(
          target,
          memory,
          memory_pos=pos_embed,
          query_pos=query_embed,
          memory_attn_mask=None,
          target_attn_mask=None,
          memory_key_padding_mask=mask,
          target_key_padding_mask=None,
      )
      out_dec = out_dec.transpose(1, 2)
      memory = memory.permute(1, 2, 0).reshape(bs, c, h, w)
      return out_dec, memory
  
imports: import torch
  import torch.nn
  from mmcv.cnn import Linear, build_activation_layer, build_norm_layer, xavier_init
  from .builder import TRANSFORMER
label_desc: Use image node_id 3 for calling the Transformer obj's underlying member method code with example usage: obj.forward(x, mask, query_embed, pos_embed) and returns: out_dec,  memory