code: def match_state_dict(
      model_state_dict, weight_state_dict, mode="default"
  ):
      """
      Match between the model state dict and pretrained weight state dict.
      Return the matched state dict.
  
      The method supposes that all the names in pretrained weight state dict are
      subclass of the names in models`, if the prefix 'backbone.' in pretrained weight
      keys is stripped. And we could get the candidates for each model key. Then we
      select the name with the longest matched size as the final match result. For
      example, the model state dict has the name of
      'backbone.res2.res2a.branch2a.conv.weight' and the pretrained weight as
      name of 'res2.res2a.branch2a.conv.weight' and 'branch2a.conv.weight'. We
      match the 'res2.res2a.branch2a.conv.weight' to the model key.
      """
  
      model_keys = sorted(model_state_dict.keys())
      weight_keys = sorted(weight_state_dict.keys())
  
      def teacher_match(a, b):
          # skip student params
          if b.startswith("modelStudent"):
              return False
          return a == b or a.endswith("." + b) or b.endswith("." + a)
  
      def student_match(a, b):
          # skip teacher params
          if b.startswith("modelTeacher"):
              return False
          return a == b or a.endswith("." + b) or b.endswith("." + a)
  
      def match(a, b):
          if b.startswith("backbone.res5"):
              b = b[9:]
          return a == b or a.endswith("." + b)
  
      if mode == "student":
          match_op = student_match
      elif mode == "teacher":
          match_op = teacher_match
      else:
          match_op = match
  
      match_matrix = np.zeros([len(model_keys), len(weight_keys)])
      for i, m_k in enumerate(model_keys):
          for j, w_k in enumerate(weight_keys):
              if match_op(m_k, w_k):
                  match_matrix[i, j] = len(w_k)
      max_id = match_matrix.argmax(1)
      max_len = match_matrix.max(1)
      max_id[max_len == 0] = -1
      load_id = set(max_id)
      load_id.discard(-1)
      not_load_weight_name = []
      if weight_keys[0].startswith("modelStudent") or weight_keys[
          0
      ].startswith("modelTeacher"):
          for match_idx in range(len(max_id)):
              if max_id[match_idx] == -1:
                  not_load_weight_name.append(model_keys[match_idx])
          if len(not_load_weight_name) > 0:
              logger.info(
                  "{} in model is not matched with pretrained weights, "
                  "and its will be trained from scratch".format(
                      not_load_weight_name
                  )
              )
  
      else:
          for idx in range(len(weight_keys)):
              if idx not in load_id:
                  not_load_weight_name.append(weight_keys[idx])
  
          if len(not_load_weight_name) > 0:
              logger.info(
                  "{} in pretrained weight is not used in the model, "
                  "and its will not be loaded".format(
                      not_load_weight_name
                  )
              )
      matched_keys = {}
      result_state_dict = {}
      for model_id, weight_id in enumerate(max_id):
          if weight_id == -1:
              continue
          model_key = model_keys[model_id]
          weight_key = weight_keys[weight_id]
          weight_value = weight_state_dict[weight_key]
          model_value_shape = list(model_state_dict[model_key].shape)
  
          if list(weight_value.shape) != model_value_shape:
              logger.info(
                  "The shape {} in pretrained weight {} is unmatched with "
                  "the shape {} in model {}. And the weight {} will not be "
                  "loaded".format(
                      weight_value.shape,
                      weight_key,
                      model_value_shape,
                      model_key,
                      weight_key,
                  )
              )
              continue
  
          assert model_key not in result_state_dict
          result_state_dict[model_key] = weight_value
          if weight_key in matched_keys:
              raise ValueError(
                  "Ambiguity weight {} loaded, it matches at least "
                  "{} and {} in the model".format(
                      weight_key, model_key, matched_keys[weight_key]
                  )
              )
          matched_keys[weight_key] = model_key
      return result_state_dict
  
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 4 for calling a global function with example usage: match_state_dict(model_state_dict, weight_state_dict, mode) and returns: result_state_dict, unknown, unknown, unknown, False, False