class_docstr: Pyramid Vision Transformer (PVT)
  
  Implementation of `Pyramid Vision Transformer: A Versatile Backbone for
  Dense Prediction without Convolutions
  <https://arxiv.org/pdf/2102.12122.pdf>`_.
  
  Args:
      pretrain_img_size (int | tuple[int]): The size of input image when
          pretrain. Defaults: 224.
      in_channels (int): Number of input channels. Default: 3.
      embed_dims (int): Embedding dimension. Default: 64.
      num_stags (int): The num of stages. Default: 4.
      num_layers (Sequence[int]): The layer number of each transformer encode
          layer. Default: [3, 4, 6, 3].
      num_heads (Sequence[int]): The attention heads of each transformer
          encode layer. Default: [1, 2, 5, 8].
      patch_sizes (Sequence[int]): The patch_size of each patch embedding.
          Default: [4, 2, 2, 2].
      strides (Sequence[int]): The stride of each patch embedding.
          Default: [4, 2, 2, 2].
      paddings (Sequence[int]): The padding of each patch embedding.
          Default: [0, 0, 0, 0].
      sr_ratios (Sequence[int]): The spatial reduction rate of each
          transformer encode layer. Default: [8, 4, 2, 1].
      out_indices (Sequence[int] | int): Output from which stages.
          Default: (0, 1, 2, 3).
      mlp_ratios (Sequence[int]): The ratio of the mlp hidden dim to the
          embedding dim of each transformer encode layer.
          Default: [8, 8, 4, 4].
      qkv_bias (bool): Enable bias for qkv if True. Default: True.
      drop_rate (float): Probability of an element to be zeroed.
          Default 0.0.
      attn_drop_rate (float): The drop out rate for attention layer.
          Default 0.0.
      drop_path_rate (float): stochastic depth rate. Default 0.1.
      use_abs_pos_embed (bool): If True, add absolute position embedding to
          the patch embedding. Defaults: True.
      use_conv_ffn (bool): If True, use Convolutional FFN to replace FFN.
          Default: False.
      act_cfg (dict): The activation config for FFNs.
          Default: dict(type='GELU').
      norm_cfg (dict): Config dict for normalization layer.
          Default: dict(type='LN').
      pretrained (str, optional): model pretrained path. Default: None.
      convert_weights (bool): The flag indicates whether the
          pre-trained model is from the original repo. We may need
          to convert some keys to make it compatible.
          Default: True.
      init_cfg (dict or list[dict], optional): Initialization config dict.
          Default: None.
class_docstr_tok: ["Pyramid","Vision","Transformer","(","PVT",")","Implementation","of","`","Pyramid","Vision","Transformer",":","A","Versatile","Backbone","for","Dense","Prediction","without","Convolutions","<","https",":","\/\/arxiv.org\/pdf\/2102.12122.pdf",">","`","_",".","Args",":","pretrain_img_size","(","int","|","tuple","[","int","]",")",":","The","size","of","input","image","when","pretrain",".","Defaults",":","224.","in_channels","(","int",")",":","Number","of","input","channels",".","Default",":","3.","embed_dims","(","int",")",":","Embedding","dimension",".","Default",":","64.","num_stags","(","int",")",":","The","num","of","stages",".","Default",":","4.","num_layers","(","Sequence","[","int","]",")",":","The","layer","number","of","each","transformer","encode","layer",".","Default",":","[","3",",","4",",","6",",","3","]",".","num_heads","(","Sequence","[","int","]",")",":","The","attention","heads","of","each","transformer","encode","layer",".","Default",":","[","1",",","2",",","5",",","8","]",".","patch_sizes","(","Sequence","[","int","]",")",":","The","patch_size","of","each","patch","embedding",".","Default",":","[","4",",","2",",","2",",","2","]",".","strides","(","Sequence","[","int","]",")",":","The","stride","of","each","patch","embedding",".","Default",":","[","4",",","2",",","2",",","2","]",".","paddings","(","Sequence","[","int","]",")",":","The","padding","of","each","patch","embedding",".","Default",":","[","0",",","0",",","0",",","0","]",".","sr_ratios","(","Sequence","[","int","]",")",":","The","spatial","reduction","rate","of","each","transformer","encode","layer",".","Default",":","[","8",",","4",",","2",",","1","]",".","out_indices","(","Sequence","[","int","]","|","int",")",":","Output","from","which","stages",".","Default",":","(","0",",","1",",","2",",","3",")",".","mlp_ratios","(","Sequence","[","int","]",")",":","The","ratio","of","the","mlp","hidden","dim","to","the","embedding","dim","of","each","transformer","encode","layer",".","Default",":","[","8",",","8",",","4",",","4","]",".","qkv_bias","(","bool",")",":","Enable","bias","for","qkv","if","True",".","Default",":","True",".","drop_rate","(","float",")",":","Probability","of","an","element","to","be","zeroed",".","Default","0.0.","attn_drop_rate","(","float",")",":","The","drop","out","rate","for","attention","layer",".","Default","0.0.","drop_path_rate","(","float",")",":","stochastic","depth","rate",".","Default","0.1.","use_abs_pos_embed","(","bool",")",":","If","True",",","add","absolute","position","embedding","to","the","patch","embedding",".","Defaults",":","True",".","use_conv_ffn","(","bool",")",":","If","True",",","use","Convolutional","FFN","to","replace","FFN",".","Default",":","False",".","act_cfg","(","dict",")",":","The","activation","config","for","FFNs",".","Default",":","dict","(","type='GELU","'",")",".","norm_cfg","(","dict",")",":","Config","dict","for","normalization","layer",".","Default",":","dict","(","type='LN","'",")",".","pretrained","(","str",",","optional",")",":","model","pretrained","path",".","Default",":","None",".","convert_weights","(","bool",")",":","The","flag","indicates","whether","the","pre-trained","model","is","from","the","original","repo",".","We","may","need","to","convert","some","keys","to","make","it","compatible",".","Default",":","True",".","init_cfg","(","dict","or","list","[","dict","]",",","optional",")",":","Initialization","config","dict",".","Default",":","None","."]
code: def __init__(
      self,
      pretrain_img_size=224,
      in_channels=3,
      embed_dims=64,
      num_stages=4,
      num_layers=[3, 4, 6, 3],
      num_heads=[1, 2, 5, 8],
      patch_sizes=[4, 2, 2, 2],
      strides=[4, 2, 2, 2],
      paddings=[0, 0, 0, 0],
      sr_ratios=[8, 4, 2, 1],
      out_indices=(0, 1, 2, 3),
      mlp_ratios=[8, 8, 4, 4],
      qkv_bias=True,
      drop_rate=0.0,
      attn_drop_rate=0.0,
      drop_path_rate=0.1,
      use_abs_pos_embed=True,
      norm_after_stage=False,
      use_conv_ffn=False,
      act_cfg=dict(type="GELU"),
      norm_cfg=dict(type="LN", eps=1e-6),
      pretrained=None,
      convert_weights=True,
      init_cfg=None,
  ):
      super().__init__(init_cfg=init_cfg)
  
      self.convert_weights = convert_weights
      if isinstance(pretrain_img_size, int):
          pretrain_img_size = to_2tuple(pretrain_img_size)
      elif isinstance(pretrain_img_size, tuple):
          if len(pretrain_img_size) == 1:
              pretrain_img_size = to_2tuple(pretrain_img_size[0])
          assert len(pretrain_img_size) == 2, (
              f"The size of image should have length 1 or 2, "
              f"but got {len(pretrain_img_size)}"
          )
  
      assert not (
          init_cfg and pretrained
      ), "init_cfg and pretrained cannot be setting at the same time"
      if isinstance(pretrained, str):
          warnings.warn(
              "DeprecationWarning: pretrained is deprecated, "
              'please use "init_cfg" instead'
          )
          self.init_cfg = dict(type="Pretrained", checkpoint=pretrained)
      elif pretrained is None:
          self.init_cfg = init_cfg
      else:
          raise TypeError("pretrained must be a str or None")
  
      self.embed_dims = embed_dims
  
      self.num_stages = num_stages
      self.num_layers = num_layers
      self.num_heads = num_heads
      self.patch_sizes = patch_sizes
      self.strides = strides
      self.sr_ratios = sr_ratios
      assert (
          num_stages
          == len(num_layers)
          == len(num_heads)
          == len(patch_sizes)
          == len(strides)
          == len(sr_ratios)
      )
  
      self.out_indices = out_indices
      assert max(out_indices) < self.num_stages
      self.pretrained = pretrained
  
      # transformer encoder
      dpr = [
          x.item()
          for x in torch.linspace(0, drop_path_rate, sum(num_layers))
      ]  # stochastic num_layer decay rule
  
      cur = 0
      self.layers = ModuleList()
      for i, num_layer in enumerate(num_layers):
          embed_dims_i = embed_dims * num_heads[i]
          patch_embed = PatchEmbed(
              in_channels=in_channels,
              embed_dims=embed_dims_i,
              kernel_size=patch_sizes[i],
              stride=strides[i],
              padding=paddings[i],
              bias=True,
              norm_cfg=norm_cfg,
          )
  
          layers = ModuleList()
          if use_abs_pos_embed:
              pos_shape = pretrain_img_size // np.prod(
                  patch_sizes[: i + 1]
              )
              pos_embed = AbsolutePositionEmbedding(
                  pos_shape=pos_shape,
                  pos_dim=embed_dims_i,
                  drop_rate=drop_rate,
              )
              layers.append(pos_embed)
          layers.extend(
              [
                  PVTEncoderLayer(
                      embed_dims=embed_dims_i,
                      num_heads=num_heads[i],
                      feedforward_channels=mlp_ratios[i] * embed_dims_i,
                      drop_rate=drop_rate,
                      attn_drop_rate=attn_drop_rate,
                      drop_path_rate=dpr[cur + idx],
                      qkv_bias=qkv_bias,
                      act_cfg=act_cfg,
                      norm_cfg=norm_cfg,
                      sr_ratio=sr_ratios[i],
                      use_conv_ffn=use_conv_ffn,
                  )
                  for idx in range(num_layer)
              ]
          )
          in_channels = embed_dims_i
          # The ret[0] of build_norm_layer is norm name.
          if norm_after_stage:
              norm = build_norm_layer(norm_cfg, embed_dims_i)[1]
          else:
              norm = nn.Identity()
          self.layers.append(ModuleList([patch_embed, layers, norm]))
          cur += num_layer
  
imports: import math
  import warnings
  from collections import OrderedDict
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import Conv2d, build_activation_layer, build_norm_layer
  from mmcv.cnn.bricks.drop import build_dropout
  from mmcv.cnn.bricks.transformer import MultiheadAttention
  from mmengine.logging import MMLogger
  from mmengine.model import BaseModule, ModuleList, Sequential, constant_init, normal_init, trunc_normal_init
  from mmengine.model.weight_init import trunc_normal_
  from mmengine.runner.checkpoint import CheckpointLoader, load_state_dict
  from torch.nn.modules.utils import _pair
  from mmdet.registry import MODELS
  from ..layers import PatchEmbed, nchw_to_nlc, nlc_to_nchw
label_desc: Use image node_id 1 to create a new PyramidVisionTransformer object from inherited base classes: BaseModule with example: obj = PyramidVisionTransformer(pretrain_img_size, in_channels, embed_dims, num_stages, num_layers, num_heads, patch_sizes, strides, paddings, sr_ratios, out_indices, mlp_ratios, qkv_bias, drop_rate, attn_drop_rate, drop_path_rate, use_abs_pos_embed, norm_after_stage, use_conv_ffn, act_cfg, norm_cfg, pretrained, convert_weights, init_cfg)