class_docstr: This class defines the `Vision Transformer architecture <https://arxiv.org/abs/2010.11929>`_. Our model implementation
  is inspired from `Early Convolutions Help Transformers See Better <https://arxiv.org/abs/2106.14881>`_
  
  .. note::
      Our implementation is different from the original implementation in two ways:
      1. Kernel size is odd.
      2. Our positional encoding implementation allows us to use ViT with any multiple input scales
      3. We do not use StochasticDepth
      4. We do not add positional encoding to class token (if enabled), as suggested in `DeiT-3 paper <https://arxiv.org/abs/2204.07118>`_
class_docstr_tok: ["This","class","defines","the","`","Vision","Transformer","architecture","<","https",":","\/\/arxiv.org\/abs\/2010.11929",">","`","_",".","Our","model","implementation","is","inspired","from","`","Early","Convolutions","Help","Transformers","See","Better","<","https",":","\/\/arxiv.org\/abs\/2106.14881",">","`","_","..","note",":",":","Our","implementation","is","different","from","the","original","implementation","in","two","ways",":","1",".","Kernel","size","is","odd",".","2",".","Our","positional","encoding","implementation","allows","us","to","use","ViT","with","any","multiple","input","scales","3",".","We","do","not","use","StochasticDepth","4",".","We","do","not","add","positional","encoding","to","class","token","(","if","enabled",")",",","as","suggested","in","`","DeiT-3","paper","<","https",":","\/\/arxiv.org\/abs\/2204.07118",">","`","_"]
code: def __init__(self, opts: argparse.Namespace, *args, **kwargs) -> None:
      image_channels = 3
      num_classes = getattr(opts, "model.classification.n_classes")
      if num_classes is None:
          logger.error(
              "Please specify number of classes using --model.classification.n-classes argument"
          )
      pytorch_mha = getattr(
          opts, "model.classification.vit.use_pytorch_mha"
      )
  
      super().__init__(opts, *args, **kwargs)
      if pytorch_mha and self.gradient_checkpointing:
          logger.error(
              "Current version of ViT supports PyTorch MHA without gradient checkpointing. "
              "Please use either of them, but not both"
          )
  
      # If output stride is not None, then it is likely a segmentation task.
      # in that case, ensure that output stride is either 8 or 16
      kernel_sizes_conv_stem = [4, 2, 2]
      strides_conv_stem = [4, 2, 2]
      if self.output_stride is not None and self.output_stride not in [
          8,
          16,
      ]:
          logger.error("Output stride should be 8 or 16")
      elif self.output_stride is not None and self.output_stride == 8:
          strides_conv_stem[0] = 2
  
      vit_config = get_configuration(opts)
  
      # Typically, in the ImageNet dataset, we use 224x224 as a resolution.
      # For out ViT implementation, patch size is 16 (16 = 4 * 2 * 2)
      # Therefore, total number of embeddings along width and height are (224 / 16)^2
      num_embeddings = (224 // 16) ** 2
  
      embed_dim = vit_config["embed_dim"]
      ffn_dim = vit_config["ffn_dim"]
      pos_emb_drop_p = vit_config["pos_emb_drop_p"]
      n_transformer_layers = vit_config["n_transformer_layers"]
      num_heads = vit_config["n_attn_heads"]
      attn_dropout = vit_config["attn_dropout"]
      dropout = vit_config["dropout"]
      ffn_dropout = vit_config["ffn_dropout"]
      norm_layer = vit_config["norm_layer"]
  
      conv_stem_proj_dim = max(32, embed_dim // 4)
      patch_emb = [
          ConvLayer2d(
              opts=opts,
              in_channels=image_channels,
              out_channels=conv_stem_proj_dim,
              kernel_size=kernel_sizes_conv_stem[0],
              stride=strides_conv_stem[0],
              bias=False,
              use_norm=True,
              use_act=True,
          ),
          ConvLayer2d(
              opts=opts,
              in_channels=conv_stem_proj_dim,
              out_channels=conv_stem_proj_dim,
              kernel_size=kernel_sizes_conv_stem[1],
              stride=strides_conv_stem[1],
              bias=False,
              use_norm=True,
              use_act=True,
          ),
          ConvLayer2d(
              opts=opts,
              in_channels=conv_stem_proj_dim,
              out_channels=embed_dim,
              kernel_size=kernel_sizes_conv_stem[2],
              stride=strides_conv_stem[2],
              bias=True,
              use_norm=False,
              use_act=False,
          ),
      ]
  
      self.patch_emb = nn.Sequential(*patch_emb)
  
      use_cls_token = not getattr(
          opts, "model.classification.vit.no_cls_token"
      )
      stochastic_dropout = getattr(
          opts, "model.classification.vit.stochastic_dropout"
      )
      per_layer_stochastic_drop_rate = [
          round(x, 3)
          for x in np.linspace(
              0, stochastic_dropout, n_transformer_layers
          )
      ]
      transformer_blocks = [
          TransformerEncoder(
              opts=opts,
              embed_dim=embed_dim,
              ffn_latent_dim=ffn_dim,
              num_heads=num_heads,
              attn_dropout=attn_dropout,
              dropout=dropout,
              ffn_dropout=ffn_dropout,
              transformer_norm_layer=norm_layer,
              stochastic_dropout=per_layer_stochastic_drop_rate[
                  layer_idx
              ],
          )
          for layer_idx in range(n_transformer_layers)
      ]
  
      self.post_transformer_norm = get_normalization_layer(
          opts=opts, num_features=embed_dim, norm_type=norm_layer
      )
  
      self.transformer = nn.Sequential(*transformer_blocks)
      self.classifier = LinearLayer(embed_dim, num_classes)
  
      self.reset_parameters(opts=opts)
  
      if use_cls_token:
          self.cls_token = nn.Parameter(
              torch.zeros(size=(1, 1, embed_dim))
          )
          torch.nn.init.trunc_normal_(self.cls_token, std=0.02)
      else:
          self.cls_token = None
  
      self.pos_embed = PositionalEmbedding(
          opts=opts,
          num_embeddings=num_embeddings,
          embedding_dim=embed_dim,
          sequence_first=False,
          padding_idx=None,
          is_learnable=not getattr(
              opts, "model.classification.vit.sinusoidal_pos_emb"
          ),
          interpolation_mode="bilinear",
      )
      self.emb_dropout = Dropout(p=pos_emb_drop_p)
      self.use_pytorch_mha = pytorch_mha
      self.embed_dim = embed_dim
      # We need to enable gradient checkpointing (--model.classification.gradient-checkpointing)
      # to use checkpoint segments in ViT
      self.checkpoint_segments = getattr(
          opts, "model.classification.vit.checkpoint_segments"
      )
  
      self.model_conf_dict = {
          "conv1": {"in": image_channels, "out": embed_dim},
          "layer1": {"in": embed_dim, "out": embed_dim},
          "layer2": {"in": embed_dim, "out": embed_dim},
          "layer3": {"in": embed_dim, "out": embed_dim},
          "layer4": {"in": embed_dim, "out": embed_dim},
          "layer5": {"in": embed_dim, "out": embed_dim},
          "exp_before_cls": {"in": embed_dim, "out": embed_dim},
          "cls": {"in": embed_dim, "out": num_classes},
      }
  
      use_simple_fpn = getattr(
          opts, "model.classification.vit.use_simple_fpn"
      )
      self.simple_fpn = None
      if use_simple_fpn:
          # for object detection, we add Simple FPN on top of ViT backbone, so that it can
          # generate multi-scale representations. See https://arxiv.org/abs/2203.16527 for details
          self.simple_fpn = self._build_simple_fpn_layers(
              opts, embed_dim, norm_layer
          )
          self.reset_simple_fpn_params()
  
      self.update_layer_norm_eps()
  
imports: import argparse
  from typing import Dict, List, Optional, Tuple, Union
  import numpy
  import torch
  from torch import Tensor, nn
  from torch.utils.checkpoint import checkpoint_sequential
  from cvnets.layers import ConvLayer2d, Dropout, Identity, LinearLayer, MaxPool2d, PositionalEmbedding, TransposeConvLayer2d, get_normalization_layer
  from cvnets.misc.common import parameter_list
  from cvnets.misc.init_utils import initialize_conv_layer
  from cvnets.models import MODEL_REGISTRY
  from cvnets.models.classification.base_image_encoder import BaseImageEncoder
  from cvnets.models.classification.config.vit import get_configuration
  from cvnets.modules import TransformerEncoder
  from utils import logger
label_desc: Use image node_id 1 to create a new VisionTransformer object from inherited base classes: BaseImageEncoder with example: obj = VisionTransformer(opts)