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 forward_features(self, x):
      b, _, t, h, _ = x.shape
      w = self.num_patches
      x = self.patch_embed(x)
      cls_tokens = self.cls_token.expand(x.size(0), -1, -1)
      x = torch.cat((cls_tokens, x), dim=1)
  
      # resizing the positional embeddings in case they don't match the input at inference
      if x.size(1) != self.pos_embed.size(1):
          pos_embed = self.pos_embed
          cls_pos_embed = pos_embed[0, 0, :].unsqueeze(0).unsqueeze(1)
          other_pos_embed = (
              pos_embed[0, 1:, :].unsqueeze(0).transpose(1, 2)
          )
          p = int(other_pos_embed.size(2) ** 0.5)
          h = x.size(1) // w
          other_pos_embed = other_pos_embed.reshape(1, x.size(2), p, p)
          new_pos_embed = nn.functional.interpolate(
              other_pos_embed, size=(h, w), mode="nearest"
          )
          new_pos_embed = new_pos_embed.flatten(2)
          new_pos_embed = new_pos_embed.transpose(1, 2)
          new_pos_embed = torch.cat((cls_pos_embed, new_pos_embed), 1)
          x = x + new_pos_embed
      else:
          x = x + self.pos_embed
      x = self.pos_drop(x)
  
      # Time Embeddings
      if self.attention_type != "space_only":
          cls_tokens = x[:b, 0, :].unsqueeze(1)
          x = x[:, 1:]
          x = rearrange(x, "(b t) n m -> (b n) t m", b=b, t=t)
          # Resize time embeddings in case they don't match
          if t != self.time_embed.size(1):
              time_embed = self.time_embed.transpose(1, 2)
              new_time_embed = nn.functional.interpolate(
                  time_embed, size=t, mode="nearest"
              )
              new_time_embed = new_time_embed.transpose(1, 2)
              x = x + new_time_embed
          else:
              x = x + self.time_embed
          x = self.time_drop(x)
          x = rearrange(x, "(b n) t m -> b (n t) m", b=b, t=t)
          x = torch.cat((cls_tokens, x), dim=1)
  
      # Attention blocks
      for blk in self.blocks:
          x = blk(x, b, t, w)
  
      # Predictions for space-only baseline
      if self.attention_type == "space_only":
          x = rearrange(x, "(b t) n m -> b t n m", b=b, t=t)
          x = torch.mean(x, 1)  # averaging predictions for every frame
  
      x = self.norm(x)
      return x[:, 0]
  
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 2 for calling the TimeSformer obj's underlying member method code with example usage: obj.forward_features(x) and returns: x