code: def load_model_state(
      opts: argparse.Namespace,
      model: torch.nn.Module,
      model_ema: Optional[torch.nn.Module] = None,
  ) -> Tuple[torch.nn.Module, Optional[torch.nn.Module]]:
      """Load the model (and optionally the EMA model) for finetuning.
  
      Args:
          opts: Input arguments.
          model: The model to be loaded with checkpoint at `common.finetune`.
          model_ema: The EMA model to be loaded with checkpoint at `common.finetune_ema`.
  
      Returns:
          Tuple of loaded model and EMA model. The second returned value is None when model_ema is not passed.
      """
      dev_id = getattr(opts, "dev.device_id", None)
      device = getattr(opts, "dev.device", torch.device("cpu"))
      finetune_loc = getattr(opts, "common.finetune", None)
      finetune_ema_loc = getattr(opts, "common.finetune_ema", None)
  
      def load_state(path):
          path = get_local_path(opts, path=path)
          if dev_id is None:
              model_state = torch.load(path, map_location=device)
          else:
              model_state = torch.load(
                  path, map_location="cuda:{}".format(dev_id)
              )
          return model_state
  
      if finetune_loc is not None and os.path.isfile(finetune_loc):
          # load model dict
          model = load_state_dict(model, load_state(finetune_loc))
  
          # load ema dict
          if model_ema is not None and os.path.isfile(finetune_ema_loc):
              model_ema = load_state_dict(
                  model, load_state(finetune_ema_loc)
              )
  
      return model, model_ema
  
imports: import argparse
  import glob
  import math
  import os
  from typing import Dict, List, Optional, Tuple, Union
  import torch
  from cvnets import EMA
  from optim import BaseOptim
  from utils import logger
  from utils.common_utils import unwrap_model_fn
  from utils.ddp_utils import is_master
  from utils.download_utils import get_local_path
label_desc: Use image node_id 9 for calling a global function with example usage: load_model_state(opts, model, model_ema) and returns: model,  model_ema, model_state