code: def load_pretrained_model(
      model: torch.nn.Module,
      wt_loc: str,
      opts: argparse.Namespace,
      *args,
      **kwargs,
  ) -> torch.nn.Module:
      """Helper function to load pre-trained weights.
      Args:
          model: Model whose weights will be loaded.
          wt_loc: Path to file to load state_dict from.
          opts: Input arguments.
      Returns:
          The model loaded with the given weights.
  
      """
      if not os.path.isfile(wt_loc):
          logger.error(
              "Pretrained file is not found here: {}".format(wt_loc)
          )
  
      wts = torch.load(wt_loc, map_location="cpu")
  
      is_master_node = is_start_rank_node(opts)
  
      exclude_scopes = getattr(opts, "model.resume_exclude_scopes", "")
      exclude_scopes: List[str] = clean_strip(exclude_scopes)
  
      missing_scopes = getattr(opts, "model.ignore_missing_scopes", "")
      missing_scopes: List[str] = clean_strip(missing_scopes)
  
      rename_scopes_map: List[List[str]] = getattr(
          opts, "model.rename_scopes_map", []
      )
      if rename_scopes_map:
          for entry in rename_scopes_map:
              if len(entry) != 2:
                  raise ValueError(
                      "Every entry in model.rename_scopes_map must contain exactly two string elements"
                      " for before and after. Got {}.".format(
                          str(entry)
                      )
                  )
  
      # By default, adding scopes that we exclude to missing scopes
      # If you excluded something, you can't expect it to be there.
      missing_scopes += exclude_scopes
  
      # remove unwanted scopes
      if exclude_scopes:
          for key in wts.copy():
              if any([re.match(x, key) for x in exclude_scopes]):
                  del wts[key]
  
      if rename_scopes_map:
          for before, after in rename_scopes_map:
              wts = {
                  re.sub(before, after, key): value
                  for key, value in wts.items()
              }
  
      strict = not bool(missing_scopes)
  
      try:
          module = unwrap_model_fn(model)
          missing_keys, unexpected_keys = module.load_state_dict(
              wts, strict=strict
          )
  
          if unexpected_keys:
              raise Exception(
                  "Found unexpected keys: {}."
                  "You can ignore these keys using `model.resume_exclude_scopes`.".format(
                      ",".join(unexpected_keys)
                  )
              )
  
          missing_keys = [
              key
              for key in missing_keys
              if not any([re.match(x, key) for x in missing_scopes])
          ]
  
          if missing_keys:
              raise Exception(
                  "Missing keys detected. Did not find the following keys in pre-trained model: {}."
                  " You can ignore the keys using `model.ignore_missing_scopes`.".format(
                      ",".join(missing_keys)
                  )
              )
  
          if is_master_node:
              logger.log(
                  "Pretrained weights are loaded from {}".format(wt_loc)
              )
      except Exception as e:
          if is_master_node:
              logger.error(
                  "Unable to load pretrained weights from {}. Error: {}".format(
                      wt_loc, e
                  )
              )
  
      return model
  
file_path: ml-cvnets/cvnets/misc/common.py
imports: import argparse
  import os
  import re
  from types import MethodType
  from typing import Dict, List, Optional, Tuple, Union
  import torch
  from torch import Tensor
  from utils import logger
  from utils.common_utils import unwrap_model_fn
  from utils.ddp_utils import is_master, is_start_rank_node
label_desc: Use image node_id 2 for calling a global function with example usage: load_pretrained_model(model, wt_loc, opts) and returns: model