class_docstr: TimeSformer model.
  
  Args:
      img_size (`int`):
          Image height of video frame (equal to width).
      patch_size (`int`):
          Patch height (equal to width)
      in_c (`int`):
          Number of image channel.
      num_classes (`int`):
          Number of categories of classification.
      embed_dim (`int`):
          Number of hidden features.
      depth (`int`):
          Number of blocks in model.
      num_heads (`int`):
          Number of attention heads.
      mlp_ratio (`float`):
          Mlp ratio.
      qkv_bias (`bool`):
          If use qkv_bias.
      qk_scale (`float`):
          Number to scale qk.
      drop_ratio (`float`):
          Drop rate of blocks & position embedding layer.
      attn_drop_ratio (`float`):
          Attention drop rate.
      norm_layer (`nn.Module`):
          Module used in normalization layer.
      num_frames (`int`):
          Number of samples to take frames.
      attention_type (`str`):
          Type of TimeSformer attention from ['divided_space_time', 'space_only', 'joint_space_time']
      dropout (`float`):
          Drop ratio of Dropout layer.
  
  Examples:
      >>> import torch
      >>> from towhee.models.timesformer import TimeSformer
      >>>
      >>> fake_video = torch.randn(1, 3, 8, 224, 224)  # (batch x channels x frames x height x width)
      >>> model = TimeSformer(img_size=224, num_classes=400, num_frames=8, attention_type='divided_space_time')
      >>> pred = model(fake_video)
      >>> print(pred.shape)
      torch.Size([1, 400])
class_docstr_tok: ["TimeSformer","model",".","Args",":","img_size","(","`","int","`",")",":","Image","height","of","video","frame","(","equal","to","width",")",".","patch_size","(","`","int","`",")",":","Patch","height","(","equal","to","width",")","in_c","(","`","int","`",")",":","Number","of","image","channel",".","num_classes","(","`","int","`",")",":","Number","of","categories","of","classification",".","embed_dim","(","`","int","`",")",":","Number","of","hidden","features",".","depth","(","`","int","`",")",":","Number","of","blocks","in","model",".","num_heads","(","`","int","`",")",":","Number","of","attention","heads",".","mlp_ratio","(","`","float","`",")",":","Mlp","ratio",".","qkv_bias","(","`","bool","`",")",":","If","use","qkv_bias",".","qk_scale","(","`","float","`",")",":","Number","to","scale","qk",".","drop_ratio","(","`","float","`",")",":","Drop","rate","of","blocks","&","position","embedding","layer",".","attn_drop_ratio","(","`","float","`",")",":","Attention","drop","rate",".","norm_layer","(","`","nn.Module","`",")",":","Module","used","in","normalization","layer",".","num_frames","(","`","int","`",")",":","Number","of","samples","to","take","frames",".","attention_type","(","`","str","`",")",":","Type","of","TimeSformer","attention","from","[","'divided_space_time","'",",","'space_only","'",",","'joint_space_time","'","]","dropout","(","`","float","`",")",":","Drop","ratio","of","Dropout","layer",".","Examples",":",">",">",">","import","torch",">",">",">","from","towhee.models.timesformer","import","TimeSformer",">",">",">",">",">",">","fake_video","=","torch.randn","(","1",",","3",",","8",",","224",",","224",")","#","(","batch","x","channels","x","frames","x","height","x","width",")",">",">",">","model","=","TimeSformer","(","img_size=224",",","num_classes=400",",","num_frames=8",",","attention_type='divided_space_time","'",")",">",">",">","pred","=","model","(","fake_video",")",">",">",">","print","(","pred.shape",")","torch.Size","(","[","1",",","400","]",")"]
code: def __init__(
      self,
      img_size=224,
      patch_size=16,
      in_c=3,
      num_classes=1000,
      embed_dim=768,
      depth=12,
      num_heads=12,
      mlp_ratio=4.0,
      qkv_bias=False,
      qk_scale=None,
      drop_ratio=0.0,
      attn_drop_ratio=0.0,
      drop_path_ratio=0.1,
      norm_layer=nn.LayerNorm,
      num_frames=8,
      attention_type="divided_space_time",
      dropout=0.0,
  ):
      super().__init__()
      assert attention_type in [
          "divided_space_time",
          "space_only",
          "joint_space_time",
          "frozen_in_time",
      ]
      self.img_size = img_size
      self.patch_size = patch_size
      self.attention_type = attention_type
      self.depth = depth
      self.dropout = nn.Dropout(dropout)
      self.num_classes = num_classes
      self.num_features = (
          self.embed_dim
      ) = embed_dim  # num_features for consistency with other models
      self.patch_embed = PatchEmbed2D(
          img_size=img_size,
          patch_size=patch_size,
          in_chans=in_c,
          embed_dim=embed_dim,
      )
      self.num_patches = self.patch_embed.num_patches
  
      # Positional Embeddings
      self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim))
      self.pos_embed = nn.Parameter(
          torch.zeros(1, self.num_patches + 1, embed_dim)
      )
      self.pos_drop = nn.Dropout(p=drop_ratio)
      if self.attention_type != "space_only":
          self.time_embed = nn.Parameter(
              torch.zeros(1, num_frames, embed_dim)
          )
          self.time_drop = nn.Dropout(p=drop_ratio)
  
      # Attention Blocks
      dpr = [
          x.item()
          for x in torch.linspace(0, drop_path_ratio, self.depth)
      ]  # stochastic depth decay rule
      self.blocks = nn.ModuleList(
          [
              Block(
                  dim=embed_dim,
                  num_heads=num_heads,
                  mlp_ratio=mlp_ratio,
                  qkv_bias=qkv_bias,
                  qk_scale=qk_scale,
                  drop=drop_ratio,
                  attn_drop=attn_drop_ratio,
                  drop_path=dpr[i],
                  norm_layer=norm_layer,
                  attention_type=self.attention_type,
              )
              for i in range(self.depth)
          ]
      )
      self.norm = norm_layer(embed_dim)
  
      # Classifier head
      self.head = (
          nn.Linear(embed_dim, num_classes)
          if num_classes > 0
          else nn.Identity()
      )
  
      nn.init.trunc_normal_(self.pos_embed, std=0.02)
      nn.init.trunc_normal_(self.cls_token, std=0.02)
      self.apply(init_vit_weights)
  
      # Initialize attention weights
      if self.attention_type == "divided_space_time":
          i = 0
          for m in self.blocks.modules():
              m_str = str(m)
              if "Block" in m_str:
                  if i > 0:
                      nn.init.constant_(m.temporal_fc.weight, 0)
                      nn.init.constant_(m.temporal_fc.bias, 0)
                  i += 1
  
file_path: towhee/towhee/models/timesformer/timesformer.py
imports: import os
  import torch
  from torch import nn
  from towhee.models.utils.init_vit_weights import init_vit_weights
  from towhee.models.layers.patch_embed2d import PatchEmbed2D
  from towhee.models.timesformer.timesformer_block import Block
  from towhee.models.timesformer.timesformer_utils import load_pretrained, get_configs
label_desc: Use image node_id 1 to create a new TimeSformer object from inherited base classes: nn with example: obj = TimeSformer(img_size, patch_size, in_c, num_classes, embed_dim, depth, num_heads, mlp_ratio, qkv_bias, qk_scale, drop_ratio, attn_drop_ratio, drop_path_ratio, norm_layer, num_frames, attention_type, dropout)