class_docstr: Implementation of `DETR: End-to-End Object Detection with Transformers.
  
  <https://arxiv.org/pdf/2005.12872>`_.
  
  Code is modified from the `official github repo
  <https://github.com/facebookresearch/detr>`_.
docstr: Prepare the inputs of the Transformer.
  
  The forward procedure of the transformer is defined as:
  'pre_transformer' -> 'encoder' -> 'pre_decoder' -> 'decoder'
  More details can be found at `TransformerDetector.forward_transformer`
  in `mmdet/detector/base_detr.py`.
  
  Args:
      img_feats (Tuple[Tensor]): Tuple of features output from the neck,
          has shape (bs, c, h, w).
      batch_data_samples (List[:obj:`DetDataSample`]): The batch
          data samples. It usually includes information such as
          `gt_instance` or `gt_panoptic_seg` or `gt_sem_seg`.
          Defaults to None.
  
  Returns:
      tuple[dict, dict]: The first dict contains the inputs of encoder
      and the second dict contains the inputs of decoder.
  
      - encoder_inputs_dict (dict): The keyword args dictionary of
        `self.forward_encoder()`, which includes 'feat', 'feat_mask',
        and 'feat_pos'.
      - decoder_inputs_dict (dict): The keyword args dictionary of
        `self.forward_decoder()`, which includes 'memory_mask',
        and 'memory_pos'.
docstr_tok: ["Prepare","the","inputs","of","the","Transformer",".","The","forward","procedure","of","the","transformer","is","defined","as",":","'pre_transformer","'","-",">","'encoder","'","-",">","'pre_decoder","'","-",">","'decoder'","More","details","can","be","found","at","`","TransformerDetector.forward_transformer","`","in","`","mmdet\/detector\/base_detr.py","`",".","Args",":","img_feats","(","Tuple","[","Tensor","]",")",":","Tuple","of","features","output","from","the","neck",",","has","shape","(","bs",",","c",",","h",",","w",")",".","batch_data_samples","(","List","[",":","obj",":","`","DetDataSample","`","]",")",":","The","batch","data","samples",".","It","usually","includes","information","such","as","`","gt_instance","`","or","`","gt_panoptic_seg","`","or","`","gt_sem_seg","`",".","Defaults","to","None",".","Returns",":","tuple","[","dict",",","dict","]",":","The","first","dict","contains","the","inputs","of","encoder","and","the","second","dict","contains","the","inputs","of","decoder",".","-","encoder_inputs_dict","(","dict",")",":","The","keyword","args","dictionary","of","`","self.forward_encoder","(",")","`",",","which","includes","'feat","'",",","'feat_mask","'",",","and","'feat_pos","'",".","-","decoder_inputs_dict","(","dict",")",":","The","keyword","args","dictionary","of","`","self.forward_decoder","(",")","`",",","which","includes","'memory_mask","'",",","and","'memory_pos","'","."]
code: def pre_transformer(
      self,
      img_feats: Tuple[Tensor],
      batch_data_samples: OptSampleList = None,
  ) -> Tuple[Dict, Dict]:
      """Prepare the inputs of the Transformer.
  
      The forward procedure of the transformer is defined as:
      'pre_transformer' -> 'encoder' -> 'pre_decoder' -> 'decoder'
      More details can be found at `TransformerDetector.forward_transformer`
      in `mmdet/detector/base_detr.py`.
  
      Args:
          img_feats (Tuple[Tensor]): Tuple of features output from the neck,
              has shape (bs, c, h, w).
          batch_data_samples (List[:obj:`DetDataSample`]): The batch
              data samples. It usually includes information such as
              `gt_instance` or `gt_panoptic_seg` or `gt_sem_seg`.
              Defaults to None.
  
      Returns:
          tuple[dict, dict]: The first dict contains the inputs of encoder
          and the second dict contains the inputs of decoder.
  
          - encoder_inputs_dict (dict): The keyword args dictionary of
            `self.forward_encoder()`, which includes 'feat', 'feat_mask',
            and 'feat_pos'.
          - decoder_inputs_dict (dict): The keyword args dictionary of
            `self.forward_decoder()`, which includes 'memory_mask',
            and 'memory_pos'.
      """
  
      feat = img_feats[-1]  # NOTE img_feats contains only one feature.
      batch_size, feat_dim, _, _ = feat.shape
      # construct binary masks which for the transformer.
      assert batch_data_samples is not None
      batch_input_shape = batch_data_samples[0].batch_input_shape
      input_img_h, input_img_w = batch_input_shape
      img_shape_list = [
          sample.img_shape for sample in batch_data_samples
      ]
      same_shape_flag = all(
          [
              s[0] == input_img_h and s[1] == input_img_w
              for s in img_shape_list
          ]
      )
      if torch.onnx.is_in_onnx_export() or same_shape_flag:
          masks = None
          # [batch_size, embed_dim, h, w]
          pos_embed = self.positional_encoding(masks, input=feat)
      else:
          masks = feat.new_ones((batch_size, input_img_h, input_img_w))
          for img_id in range(batch_size):
              img_h, img_w = img_shape_list[img_id]
              masks[img_id, :img_h, :img_w] = 0
          # NOTE following the official DETR repo, non-zero values represent
          # ignored positions, while zero values mean valid positions.
  
          masks = (
              F.interpolate(masks.unsqueeze(1), size=feat.shape[-2:])
              .to(torch.bool)
              .squeeze(1)
          )
          # [batch_size, embed_dim, h, w]
          pos_embed = self.positional_encoding(masks)
  
      # use `view` instead of `flatten` for dynamically exporting to ONNX
      # [bs, c, h, w] -> [bs, h*w, c]
      feat = feat.view(batch_size, feat_dim, -1).permute(0, 2, 1)
      pos_embed = pos_embed.view(batch_size, feat_dim, -1).permute(
          0, 2, 1
      )
      # [bs, h, w] -> [bs, h*w]
      if masks is not None:
          masks = masks.view(batch_size, -1)
  
      # prepare transformer_inputs_dict
      encoder_inputs_dict = dict(
          feat=feat, feat_mask=masks, feat_pos=pos_embed
      )
      decoder_inputs_dict = dict(
          memory_mask=masks, memory_pos=pos_embed
      )
      return encoder_inputs_dict, decoder_inputs_dict
  
file_path: mmdetection/mmdet/models/detectors/detr.py
imports: from typing import Dict, Tuple
  import torch
  import torch.nn.functional
  from torch import Tensor, nn
  from mmdet.registry import MODELS
  from mmdet.structures import OptSampleList
  from ..layers import DetrTransformerDecoder, DetrTransformerEncoder, SinePositionalEncoding
  from .base_detr import DetectionTransformer
label_desc: Use image node_id 3 for calling the DETR obj's underlying member method code with example usage: obj.pre_transformer(img_feats, batch_data_samples) and returns: encoder_inputs_dict,  decoder_inputs_dict