code: def load_weight(
      model, weight, optimizer=None, ema=None, exchange=True
  ):
      if is_url(weight):
          weight = get_weights_path(weight)
  
      path = _strip_postfix(weight)
      pdparam_path = path + ".pdparams"
      if not os.path.exists(pdparam_path):
          raise ValueError(
              "Model pretrain path {} does not "
              "exists.".format(pdparam_path)
          )
  
      if ema is not None and os.path.exists(path + ".pdema"):
          if exchange:
              # Exchange model and ema_model to load
              logger.info("Exchange model and ema_model to load:")
              ema_state_dict = paddle.load(pdparam_path)
              logger.info(
                  "Loading ema_model weights from {}".format(
                      path + ".pdparams"
                  )
              )
              param_state_dict = paddle.load(path + ".pdema")
              logger.info(
                  "Loading model weights from {}".format(
                      path + ".pdema"
                  )
              )
          else:
              ema_state_dict = paddle.load(path + ".pdema")
              logger.info(
                  "Loading ema_model weights from {}".format(
                      path + ".pdema"
                  )
              )
              param_state_dict = paddle.load(pdparam_path)
              logger.info(
                  "Loading model weights from {}".format(
                      path + ".pdparams"
                  )
              )
      else:
          ema_state_dict = None
          param_state_dict = paddle.load(pdparam_path)
  
      if hasattr(model, "modelTeacher") and hasattr(
          model, "modelStudent"
      ):
          print(
              "Loading pretrain weights for Teacher-Student framework."
          )
          print("Loading pretrain weights for Student model.")
          student_model_dict = model.modelStudent.state_dict()
          student_param_state_dict = match_state_dict(
              student_model_dict, param_state_dict, mode="student"
          )
          model.modelStudent.set_dict(student_param_state_dict)
          print("Loading pretrain weights for Teacher model.")
          teacher_model_dict = model.modelTeacher.state_dict()
  
          teacher_param_state_dict = match_state_dict(
              teacher_model_dict, param_state_dict, mode="teacher"
          )
          model.modelTeacher.set_dict(teacher_param_state_dict)
  
      else:
          model_dict = model.state_dict()
          model_weight = {}
          incorrect_keys = 0
          for key in model_dict.keys():
              if key in param_state_dict.keys():
                  model_weight[key] = param_state_dict[key]
              else:
                  logger.info("Unmatched key: {}".format(key))
                  incorrect_keys += 1
          assert (
              incorrect_keys == 0
          ), "Load weight {} incorrectly, \
                  {} keys unmatched, please check again.".format(
              weight, incorrect_keys
          )
          logger.info(
              "Finish resuming model weights: {}".format(pdparam_path)
          )
          model.set_dict(model_weight)
  
      last_epoch = 0
      if optimizer is not None and os.path.exists(path + ".pdopt"):
          optim_state_dict = paddle.load(path + ".pdopt")
          # to solve resume bug, will it be fixed in paddle 2.0
          for key in optimizer.state_dict().keys():
              if not key in optim_state_dict.keys():
                  optim_state_dict[key] = optimizer.state_dict()[key]
          if "last_epoch" in optim_state_dict:
              last_epoch = optim_state_dict.pop("last_epoch")
          optimizer.set_state_dict(optim_state_dict)
  
          if ema_state_dict is not None:
              ema.resume(
                  ema_state_dict,
                  optim_state_dict["LR_Scheduler"]["last_epoch"],
              )
      elif ema_state_dict is not None:
          ema.resume(ema_state_dict)
      return last_epoch
  
file_path: PaddleDetection/ppdet/utils/checkpoint.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from __future__ import unicode_literals
  import os
  import numpy
  import paddle
  import paddle.nn
  from .download import get_weights_path
  from .logger import setup_logger
label_desc: Use image node_id 3 for calling a global function with example usage: load_weight(model, weight, optimizer, ema, exchange) and returns: last_epoch