class_docstr: SlowFast model builder for SlowFast network.
  
  Christoph Feichtenhofer, Haoqi Fan, Jitendra Malik, and Kaiming He.
  "SlowFast networks for video recognition."
  https://arxiv.org/pdf/1812.03982.pdf
class_docstr_tok: ["SlowFast","model","builder","for","SlowFast","network",".","Christoph","Feichtenhofer",",","Haoqi","Fan",",","Jitendra","Malik",",","and","Kaiming","He",".","``","SlowFast","networks","for","video","recognition",".","''","https",":","\/\/arxiv.org\/pdf\/1812.03982.pdf"]
docstr: Builds a SlowFast model. The first pathway is the Slow pathway and the
      second pathway is the Fast pathway.
  Args:
      cfg (CfgNode): model building configs, details are in the
          comments of the config file.
code: def _construct_network(self, cfg):
      """
      Builds a SlowFast model. The first pathway is the Slow pathway and the
          second pathway is the Fast pathway.
      Args:
          cfg (CfgNode): model building configs, details are in the
              comments of the config file.
      """
      assert cfg.MODEL.ARCH in _POOL1.keys()
      pool_size = _POOL1[cfg.MODEL.ARCH]
      assert len({len(pool_size), self.num_pathways}) == 1
      assert cfg.RESNET.DEPTH in _MODEL_STAGE_DEPTH.keys()
  
      (d2, d3, d4, d5) = _MODEL_STAGE_DEPTH[cfg.RESNET.DEPTH]
  
      num_groups = cfg.RESNET.NUM_GROUPS
      width_per_group = cfg.RESNET.WIDTH_PER_GROUP
      dim_inner = num_groups * width_per_group
      out_dim_ratio = (
          cfg.SLOWFAST.BETA_INV
          // cfg.SLOWFAST.FUSION_CONV_CHANNEL_RATIO
      )
  
      temp_kernel = _TEMPORAL_KERNEL_BASIS[cfg.MODEL.ARCH]
  
      self.s1 = stem_helper.VideoModelStem(
          dim_in=cfg.DATA.INPUT_CHANNEL_NUM,
          dim_out=[
              width_per_group,
              width_per_group // cfg.SLOWFAST.BETA_INV,
          ],
          kernel=[
              temp_kernel[0][0] + [7, 7],
              temp_kernel[0][1] + [7, 7],
          ],
          stride=[[1, 2, 2]] * 2,
          padding=[
              [temp_kernel[0][0][0] // 2, 3, 3],
              [temp_kernel[0][1][0] // 2, 3, 3],
          ],
          norm_module=self.norm_module,
      )
      self.s1_fuse = FuseFastToSlow(
          width_per_group // cfg.SLOWFAST.BETA_INV,
          cfg.SLOWFAST.FUSION_CONV_CHANNEL_RATIO,
          cfg.SLOWFAST.FUSION_KERNEL_SZ,
          cfg.SLOWFAST.ALPHA,
          norm_module=self.norm_module,
      )
  
      self.s2 = resnet_helper.ResStage(
          dim_in=[
              width_per_group + width_per_group // out_dim_ratio,
              width_per_group // cfg.SLOWFAST.BETA_INV,
          ],
          dim_out=[
              width_per_group * 4,
              width_per_group * 4 // cfg.SLOWFAST.BETA_INV,
          ],
          dim_inner=[dim_inner, dim_inner // cfg.SLOWFAST.BETA_INV],
          temp_kernel_sizes=temp_kernel[1],
          stride=cfg.RESNET.SPATIAL_STRIDES[0],
          num_blocks=[d2] * 2,
          num_groups=[num_groups] * 2,
          num_block_temp_kernel=cfg.RESNET.NUM_BLOCK_TEMP_KERNEL[0],
          nonlocal_inds=cfg.NONLOCAL.LOCATION[0],
          nonlocal_group=cfg.NONLOCAL.GROUP[0],
          nonlocal_pool=cfg.NONLOCAL.POOL[0],
          instantiation=cfg.NONLOCAL.INSTANTIATION,
          trans_func_name=cfg.RESNET.TRANS_FUNC,
          dilation=cfg.RESNET.SPATIAL_DILATIONS[0],
          norm_module=self.norm_module,
      )
      self.s2_fuse = FuseFastToSlow(
          width_per_group * 4 // cfg.SLOWFAST.BETA_INV,
          cfg.SLOWFAST.FUSION_CONV_CHANNEL_RATIO,
          cfg.SLOWFAST.FUSION_KERNEL_SZ,
          cfg.SLOWFAST.ALPHA,
          norm_module=self.norm_module,
      )
  
      for pathway in range(self.num_pathways):
          pool = nn.MaxPool3d(
              kernel_size=pool_size[pathway],
              stride=pool_size[pathway],
              padding=[0, 0, 0],
          )
          self.add_module("pathway{}_pool".format(pathway), pool)
  
      self.s3 = resnet_helper.ResStage(
          dim_in=[
              width_per_group * 4
              + width_per_group * 4 // out_dim_ratio,
              width_per_group * 4 // cfg.SLOWFAST.BETA_INV,
          ],
          dim_out=[
              width_per_group * 8,
              width_per_group * 8 // cfg.SLOWFAST.BETA_INV,
          ],
          dim_inner=[
              dim_inner * 2,
              dim_inner * 2 // cfg.SLOWFAST.BETA_INV,
          ],
          temp_kernel_sizes=temp_kernel[2],
          stride=cfg.RESNET.SPATIAL_STRIDES[1],
          num_blocks=[d3] * 2,
          num_groups=[num_groups] * 2,
          num_block_temp_kernel=cfg.RESNET.NUM_BLOCK_TEMP_KERNEL[1],
          nonlocal_inds=cfg.NONLOCAL.LOCATION[1],
          nonlocal_group=cfg.NONLOCAL.GROUP[1],
          nonlocal_pool=cfg.NONLOCAL.POOL[1],
          instantiation=cfg.NONLOCAL.INSTANTIATION,
          trans_func_name=cfg.RESNET.TRANS_FUNC,
          dilation=cfg.RESNET.SPATIAL_DILATIONS[1],
          norm_module=self.norm_module,
      )
      self.s3_fuse = FuseFastToSlow(
          width_per_group * 8 // cfg.SLOWFAST.BETA_INV,
          cfg.SLOWFAST.FUSION_CONV_CHANNEL_RATIO,
          cfg.SLOWFAST.FUSION_KERNEL_SZ,
          cfg.SLOWFAST.ALPHA,
          norm_module=self.norm_module,
      )
  
      self.s4 = resnet_helper.ResStage(
          dim_in=[
              width_per_group * 8
              + width_per_group * 8 // out_dim_ratio,
              width_per_group * 8 // cfg.SLOWFAST.BETA_INV,
          ],
          dim_out=[
              width_per_group * 16,
              width_per_group * 16 // cfg.SLOWFAST.BETA_INV,
          ],
          dim_inner=[
              dim_inner * 4,
              dim_inner * 4 // cfg.SLOWFAST.BETA_INV,
          ],
          temp_kernel_sizes=temp_kernel[3],
          stride=cfg.RESNET.SPATIAL_STRIDES[2],
          num_blocks=[d4] * 2,
          num_groups=[num_groups] * 2,
          num_block_temp_kernel=cfg.RESNET.NUM_BLOCK_TEMP_KERNEL[2],
          nonlocal_inds=cfg.NONLOCAL.LOCATION[2],
          nonlocal_group=cfg.NONLOCAL.GROUP[2],
          nonlocal_pool=cfg.NONLOCAL.POOL[2],
          instantiation=cfg.NONLOCAL.INSTANTIATION,
          trans_func_name=cfg.RESNET.TRANS_FUNC,
          dilation=cfg.RESNET.SPATIAL_DILATIONS[2],
          norm_module=self.norm_module,
      )
      self.s4_fuse = FuseFastToSlow(
          width_per_group * 16 // cfg.SLOWFAST.BETA_INV,
          cfg.SLOWFAST.FUSION_CONV_CHANNEL_RATIO,
          cfg.SLOWFAST.FUSION_KERNEL_SZ,
          cfg.SLOWFAST.ALPHA,
          norm_module=self.norm_module,
      )
  
      self.s5 = resnet_helper.ResStage(
          dim_in=[
              width_per_group * 16
              + width_per_group * 16 // out_dim_ratio,
              width_per_group * 16 // cfg.SLOWFAST.BETA_INV,
          ],
          dim_out=[
              width_per_group * 32,
              width_per_group * 32 // cfg.SLOWFAST.BETA_INV,
          ],
          dim_inner=[
              dim_inner * 8,
              dim_inner * 8 // cfg.SLOWFAST.BETA_INV,
          ],
          temp_kernel_sizes=temp_kernel[4],
          stride=cfg.RESNET.SPATIAL_STRIDES[3],
          num_blocks=[d5] * 2,
          num_groups=[num_groups] * 2,
          num_block_temp_kernel=cfg.RESNET.NUM_BLOCK_TEMP_KERNEL[3],
          nonlocal_inds=cfg.NONLOCAL.LOCATION[3],
          nonlocal_group=cfg.NONLOCAL.GROUP[3],
          nonlocal_pool=cfg.NONLOCAL.POOL[3],
          instantiation=cfg.NONLOCAL.INSTANTIATION,
          trans_func_name=cfg.RESNET.TRANS_FUNC,
          dilation=cfg.RESNET.SPATIAL_DILATIONS[3],
          norm_module=self.norm_module,
      )
  
      if cfg.DETECTION.ENABLE:
          self.head = head_helper.ResNetRoIHead(
              dim_in=[
                  width_per_group * 32,
                  width_per_group * 32 // cfg.SLOWFAST.BETA_INV,
              ],
              num_classes=cfg.MODEL.NUM_CLASSES,
              pool_size=[
                  [
                      cfg.DATA.NUM_FRAMES
                      // cfg.SLOWFAST.ALPHA
                      // pool_size[0][0],
                      1,
                      1,
                  ],
                  [cfg.DATA.NUM_FRAMES // pool_size[1][0], 1, 1],
              ],
              resolution=[[cfg.DETECTION.ROI_XFORM_RESOLUTION] * 2] * 2,
              scale_factor=[cfg.DETECTION.SPATIAL_SCALE_FACTOR] * 2,
              dropout_rate=cfg.MODEL.DROPOUT_RATE,
              act_func=cfg.MODEL.HEAD_ACT,
              aligned=cfg.DETECTION.ALIGNED,
              detach_final_fc=cfg.MODEL.DETACH_FINAL_FC,
          )
      else:
          self.head = head_helper.ResNetBasicHead(
              dim_in=[
                  width_per_group * 32,
                  width_per_group * 32 // cfg.SLOWFAST.BETA_INV,
              ],
              num_classes=cfg.MODEL.NUM_CLASSES,
              pool_size=[None, None]
              if cfg.MULTIGRID.SHORT_CYCLE
              or cfg.MODEL.MODEL_NAME == "ContrastiveModel"
              else [
                  [
                      cfg.DATA.NUM_FRAMES
                      // cfg.SLOWFAST.ALPHA
                      // pool_size[0][0],
                      cfg.DATA.TRAIN_CROP_SIZE // 32 // pool_size[0][1],
                      cfg.DATA.TRAIN_CROP_SIZE // 32 // pool_size[0][2],
                  ],
                  [
                      cfg.DATA.NUM_FRAMES // pool_size[1][0],
                      cfg.DATA.TRAIN_CROP_SIZE // 32 // pool_size[1][1],
                      cfg.DATA.TRAIN_CROP_SIZE // 32 // pool_size[1][2],
                  ],
              ],  # None for AdaptiveAvgPool3d((1, 1, 1))
              dropout_rate=cfg.MODEL.DROPOUT_RATE,
              act_func=cfg.MODEL.HEAD_ACT,
              detach_final_fc=cfg.MODEL.DETACH_FINAL_FC,
              cfg=cfg,
          )
  
imports: import math
  from functools import partial
  import torch
  import torch.nn
  import torch.nn.functional
  from torch.nn.init import trunc_normal_
  import slowfast.utils.logging
  import slowfast.utils.weight_init_helper
  from slowfast.models.attention import MultiScaleBlock
  from slowfast.models.batchnorm_helper import get_norm
  from slowfast.models.common import TwoStreamFusion
  from slowfast.models.reversible_mvit import ReversibleMViT
  from slowfast.models.utils import calc_mvit_feature_geometry, get_3d_sincos_pos_embed, round_width, validate_checkpoint_wrapper_import
  from .None import head_helper, operators, resnet_helper, stem_helper
  from .build import MODEL_REGISTRY
label_desc: Use image node_id 2 for calling the SlowFast obj's underlying member method code with example usage: obj._construct_network(cfg) without return types