code: def load_checkpoint(
      path_to_checkpoint,
      model,
      data_parallel=True,
      optimizer=None,
      scaler=None,
      inflation=False,
      convert_from_caffe2=False,
      epoch_reset=False,
      clear_name_pattern=(),
      image_init=False,
  ):
      """
      Load the checkpoint from the given file. If inflation is True, inflate the
      2D Conv weights from the checkpoint to 3D Conv.
      Args:
          path_to_checkpoint (string): path to the checkpoint to load.
          model (model): model to load the weights from the checkpoint.
          data_parallel (bool): if true, model is wrapped by
          torch.nn.parallel.DistributedDataParallel.
          optimizer (optim): optimizer to load the historical state.
          scaler (GradScaler): GradScaler to load the mixed precision scale.
          inflation (bool): if True, inflate the weights from the checkpoint.
          convert_from_caffe2 (bool): if True, load the model from caffe2 and
              convert it to pytorch.
          epoch_reset (bool): if True, reset #train iterations from the checkpoint.
          clear_name_pattern (string): if given, this (sub)string will be cleared
              from a layer name if it can be matched.
      Returns:
          (int): the number of training epoch of the checkpoint.
      """
      logger.info(
          "Loading network weights from {}.".format(path_to_checkpoint)
      )
  
      # Account for the DDP wrapper in the multi-gpu setting.
      ms = model.module if data_parallel else model
      if convert_from_caffe2:
          with pathmgr.open(path_to_checkpoint, "rb") as f:
              caffe2_checkpoint = pickle.load(f, encoding="latin1")
          state_dict = OrderedDict()
          name_convert_func = get_name_convert_func()
          for key in caffe2_checkpoint["blobs"].keys():
              converted_key = name_convert_func(key)
              converted_key = c2_normal_to_sub_bn(
                  converted_key, ms.state_dict()
              )
              if converted_key in ms.state_dict():
                  c2_blob_shape = caffe2_checkpoint["blobs"][key].shape
                  model_blob_shape = ms.state_dict()[
                      converted_key
                  ].shape
  
                  # expand shape dims if they differ (eg for converting linear to conv params)
                  if len(c2_blob_shape) < len(model_blob_shape):
                      c2_blob_shape += (1,) * (
                          len(model_blob_shape) - len(c2_blob_shape)
                      )
                      caffe2_checkpoint["blobs"][key] = np.reshape(
                          caffe2_checkpoint["blobs"][key], c2_blob_shape
                      )
                  # Load BN stats to Sub-BN.
                  if (
                      len(model_blob_shape) == 1
                      and len(c2_blob_shape) == 1
                      and model_blob_shape[0] > c2_blob_shape[0]
                      and model_blob_shape[0] % c2_blob_shape[0] == 0
                  ):
                      caffe2_checkpoint["blobs"][key] = np.concatenate(
                          [caffe2_checkpoint["blobs"][key]]
                          * (model_blob_shape[0] // c2_blob_shape[0])
                      )
                      c2_blob_shape = caffe2_checkpoint["blobs"][
                          key
                      ].shape
  
                  if c2_blob_shape == tuple(model_blob_shape):
                      state_dict[converted_key] = torch.tensor(
                          caffe2_checkpoint["blobs"][key]
                      ).clone()
                      logger.info(
                          "{}: {} => {}: {}".format(
                              key,
                              c2_blob_shape,
                              converted_key,
                              tuple(model_blob_shape),
                          )
                      )
                  else:
                      logger.warn(
                          "!! {}: {} does not match {}: {}".format(
                              key,
                              c2_blob_shape,
                              converted_key,
                              tuple(model_blob_shape),
                          )
                      )
              else:
                  if not any(
                      prefix in key
                      for prefix in ["momentum", "lr", "model_iter"]
                  ):
                      logger.warn(
                          "!! {}: can not be converted, got {}".format(
                              key, converted_key
                          )
                      )
          diff = set(ms.state_dict()) - set(state_dict)
          diff = {d for d in diff if "num_batches_tracked" not in d}
          if len(diff) > 0:
              logger.warn("Not loaded {}".format(diff))
          ms.load_state_dict(state_dict, strict=False)
          epoch = -1
      else:
          # Load the checkpoint on CPU to avoid GPU mem spike.
          with pathmgr.open(path_to_checkpoint, "rb") as f:
              checkpoint = torch.load(f, map_location="cpu")
          model_state_dict_3d = (
              model.module.state_dict()
              if data_parallel
              else model.state_dict()
          )
          checkpoint["model_state"] = normal_to_sub_bn(
              checkpoint["model_state"], model_state_dict_3d
          )
          if inflation:
              # Try to inflate the model.
              inflated_model_dict = inflate_weight(
                  checkpoint["model_state"], model_state_dict_3d
              )
              ms.load_state_dict(inflated_model_dict, strict=False)
          else:
              if clear_name_pattern:
                  for item in clear_name_pattern:
                      model_state_dict_new = OrderedDict()
                      for k in checkpoint["model_state"]:
                          if item in k:
                              k_re = k.replace(
                                  item, "", 1
                              )  # only repace first occurence of pattern
                              model_state_dict_new[k_re] = checkpoint[
                                  "model_state"
                              ][k]
                              logger.info(
                                  "renaming: {} -> {}".format(k, k_re)
                              )
                          else:
                              model_state_dict_new[k] = checkpoint[
                                  "model_state"
                              ][k]
                      checkpoint["model_state"] = model_state_dict_new
  
              pre_train_dict = checkpoint["model_state"]
              model_dict = ms.state_dict()
  
              if image_init:
                  if (
                      "pos_embed" in pre_train_dict.keys()
                      and "pos_embed_xy" in model_dict.keys()
                  ):
                      print(
                          pre_train_dict["pos_embed"].shape,
                          model_dict["pos_embed_xy"].shape,
                          model_dict["pos_embed_class"].shape,
                      )
                      if (
                          pre_train_dict["pos_embed"].shape[1]
                          == model_dict["pos_embed_xy"].shape[1] + 1
                      ):
                          pre_train_dict[
                              "pos_embed_xy"
                          ] = pre_train_dict["pos_embed"][:, 1:]
                          pre_train_dict[
                              "pos_embed_class"
                          ] = pre_train_dict["pos_embed"][:, :1]
  
                  if (
                      "patch_embed.proj.weight" in pre_train_dict.keys()
                      and "patch_embed.proj.weight" in model_dict.keys()
                  ):
                      print(
                          pre_train_dict[
                              "patch_embed.proj.weight"
                          ].shape,
                          model_dict["patch_embed.proj.weight"].shape,
                      )
                      if (
                          len(
                              pre_train_dict[
                                  "patch_embed.proj.weight"
                              ].shape
                          )
                          == 4
                          and len(
                              model_dict[
                                  "patch_embed.proj.weight"
                              ].shape
                          )
                          == 5
                      ):  # img->video
                          t = model_dict[
                              "patch_embed.proj.weight"
                          ].shape[2]
                          pre_train_dict[
                              "patch_embed.proj.weight"
                          ] = pre_train_dict["patch_embed.proj.weight"][
                              :, :, None, :, :
                          ].repeat(
                              1, 1, t, 1, 1
                          )
                          logger.info(
                              f"inflate patch_embed.proj.weight to {pre_train_dict['patch_embed.proj.weight'].shape}"
                          )
                      elif (
                          len(
                              pre_train_dict[
                                  "patch_embed.proj.weight"
                              ].shape
                          )
                          == 5
                          and len(
                              model_dict[
                                  "patch_embed.proj.weight"
                              ].shape
                          )
                          == 4
                      ):  # video->img
                          orig_shape = pre_train_dict[
                              "patch_embed.proj.weight"
                          ].shape
                          # pre_train_dict["patch_embed.proj.weight"] = pre_train_dict["patch_embed.proj.weight"][:, :, orig_shape[2]//2, :, :] # take center
                          pre_train_dict[
                              "patch_embed.proj.weight"
                          ] = pre_train_dict[
                              "patch_embed.proj.weight"
                          ].sum(
                              2
                          )  # take avg
                          logger.info(
                              f"deflate patch_embed.proj.weight from {orig_shape} to {pre_train_dict['patch_embed.proj.weight'].shape}"
                          )
                          if (
                              "pos_embed_spatial"
                              in pre_train_dict.keys()
                              and "pos_embed" in model_dict.keys()
                          ):
                              pos_embds = pre_train_dict[
                                  "pos_embed_spatial"
                              ]
                              if (
                                  "pos_embed_class"
                                  in pre_train_dict.keys()
                                  and pos_embds.shape
                                  != model_dict["pos_embed"].shape
                              ):
                                  pos_embds = torch.cat(
                                      [
                                          pre_train_dict[
                                              "pos_embed_class"
                                          ],
                                          pos_embds,
                                      ],
                                      1,
                                  )
                                  pre_train_dict.pop("pos_embed_class")
                              if (
                                  pos_embds.shape
                                  == model_dict["pos_embed"].shape
                              ):
                                  pre_train_dict[
                                      "pos_embed"
                                  ] = pos_embds
                                  pre_train_dict.pop(
                                      "pos_embed_spatial"
                                  )
                                  logger.info(
                                      f"successful surgery of pos embed w/ shape {pos_embds.shape} "
                                  )
                              else:
                                  logger.info(
                                      f"UNSUCCESSFUL surgery of pos embed w/ shape {pos_embds.shape} "
                                  )
  
                  qkv = [
                      "attn.pool_k.weight",
                      "attn.pool_q.weight",
                      "attn.pool_v.weight",
                  ]
                  for k in pre_train_dict.keys():
                      if (
                          any([x in k for x in qkv])
                          and pre_train_dict[k].shape
                          != model_dict[k].shape
                      ):
                          # print(pre_train_dict[k].shape, model_dict[k].shape)
                          logger.info(
                              f"inflate {k} from {pre_train_dict[k].shape} to {model_dict[k].shape}"
                          )
                          t = model_dict[k].shape[2]
                          pre_train_dict[k] = pre_train_dict[k].repeat(
                              1, 1, t, 1, 1
                          )
  
                  for k in pre_train_dict.keys():
                      if (
                          "rel_pos" in k
                          and pre_train_dict[k].shape
                          != model_dict[k].shape
                      ):
                          # print(pre_train_dict[k].shape, model_dict[k].shape)
                          logger.info(
                              f"interpolating {k} from {pre_train_dict[k].shape} to {model_dict[k].shape}"
                          )
                          new_pos_embed = (
                              torch.nn.functional.interpolate(
                                  pre_train_dict[k]
                                  .reshape(
                                      1, pre_train_dict[k].shape[0], -1
                                  )
                                  .permute(0, 2, 1),
                                  size=model_dict[k].shape[0],
                                  mode="linear",
                              )
                          )
                          new_pos_embed = (
                              new_pos_embed.reshape(
                                  -1, model_dict[k].shape[0]
                              )
                              .permute(1, 0)
                              .squeeze()
                          )
                          pre_train_dict[k] = new_pos_embed
  
              # Match pre-trained weights that have same shape as current model.
              pre_train_dict_match = {}
              not_used_layers = []
              for k, v in pre_train_dict.items():
                  if k in model_dict:
                      if v.size() == model_dict[k].size():
                          pre_train_dict_match[k] = v
                      else:
                          if "attn.rel_pos" in k:
                              v_shape = v.shape
                              v = v.t().unsqueeze(0)
                              v = torch.nn.functional.interpolate(
                                  v,
                                  size=model_dict[k].size()[0],
                                  mode="linear",
                              )
                              v = v[0].t()
                              pre_train_dict_match[k] = v
                              logger.info(
                                  "{} reshaped from {} to {}".format(
                                      k, v_shape, v.shape
                                  )
                              )
                          elif "pos_embed_temporal" in k:
                              v_shape = v.shape
                              v = torch.nn.functional.interpolate(
                                  v.permute(0, 2, 1),
                                  size=model_dict[k].shape[1],
                                  mode="linear",
                              )
                              pre_train_dict_match[k] = v.permute(
                                  0, 2, 1
                              )
                              logger.info(
                                  "{} reshaped from {} to {}".format(
                                      k,
                                      v_shape,
                                      pre_train_dict_match[k].shape,
                                  )
                              )
                          elif "pos_embed_spatial" in k:
                              v_shape = v.shape
                              pretrain_size = int(math.sqrt(v_shape[1]))
                              model_size = int(
                                  math.sqrt(model_dict[k].shape[1])
                              )
                              assert (
                                  pretrain_size * pretrain_size
                                  == v_shape[1]
                              )
                              assert (
                                  model_size * model_size
                                  == model_dict[k].shape[1]
                              )
                              v = torch.nn.functional.interpolate(
                                  v.reshape(
                                      1,
                                      pretrain_size,
                                      pretrain_size,
                                      -1,
                                  ).permute(0, 3, 1, 2),
                                  size=(model_size, model_size),
                                  mode="bicubic",
                              )
                              pre_train_dict_match[k] = v.reshape(
                                  1, -1, model_size * model_size
                              ).permute(0, 2, 1)
                              logger.info(
                                  "{} reshaped from {} to {}".format(
                                      k,
                                      v_shape,
                                      pre_train_dict_match[k].shape,
                                  )
                              )
                          else:
                              not_used_layers.append(k)
                  else:
                      not_used_layers.append(k)
              # Weights that do not have match from the pre-trained model.
              not_load_layers = [
                  k
                  for k in model_dict.keys()
                  if k not in pre_train_dict_match.keys()
              ]
              # Log weights that are not loaded with the pre-trained weights.
              if not_load_layers:
                  for k in not_load_layers:
                      logger.info(
                          "Network weights {} not loaded.".format(k)
                      )
              if not_used_layers:
                  for k in not_used_layers:
                      logger.info(
                          "Network weights {} not used.".format(k)
                      )
              # Load pre-trained weights.
              missing_keys, unexpected_keys = ms.load_state_dict(
                  pre_train_dict_match, strict=False
              )
  
              print("missing keys: {}".format(missing_keys))
              print("unexpected keys: {}".format(unexpected_keys))
              epoch = -1
  
              # Load the optimizer state (commonly not done when fine-tuning)
          if "epoch" in checkpoint.keys() and not epoch_reset:
              epoch = checkpoint["epoch"]
              if optimizer:
                  optimizer.load_state_dict(
                      checkpoint["optimizer_state"]
                  )
              if scaler:
                  scaler.load_state_dict(checkpoint["scaler_state"])
          else:
              epoch = -1
      return epoch
  
imports: import copy
  import math
  import numpy
  import os
  import pickle
  from collections import OrderedDict
  import torch
  import slowfast.utils.distributed
  import slowfast.utils.logging
  from slowfast.utils.c2_model_loading import get_name_convert_func
  from slowfast.utils.env import checkpoint_pathmgr
label_desc: Use image node_id 9 for calling a global function with example usage: load_checkpoint(path_to_checkpoint, model, data_parallel, optimizer, scaler, inflation, convert_from_caffe2, epoch_reset, clear_name_pattern, image_init) and returns: epoch