code: def transfer_weights(
      model: nn.Module,
      model_state_dict: collections.OrderedDict,
      incompatible_shape_action="skip",
  ):
      """
      Copy weights from state dict to model, skipping layers that are incompatible.
      This method is helpful if you are doing some model surgery and want to load
      part of the model weights into different model.
      :param model: Model to load weights into
      :param model_state_dict: Model state dict to load weights from
      :param incompatible_shape_action: What to do if shape of weight tensor is incompatible.
      Possible values are:
          - "skip" - Skip loading this tensor
          - "match_mean_std" - Initialize tensor with random values with same mean and std as source tensor
      :return: None
      """
      existing_model_state_dict = model.state_dict()
  
      for name, value in model_state_dict.items():
          if name not in existing_model_state_dict:
              logger.debug(
                  f"transfer_weights skipped loading weights for key {name}, because it does not exist in model"
              )
              continue
  
          existing_value = existing_model_state_dict[name]
          if value.shape != existing_value.shape:
              if incompatible_shape_action == "skip":
                  logger.debug(
                      f"transfer_weights skipped loading weights for key {name}, because of checkpoint has shape {value.shape} and model has shape {existing_model_state_dict[name].shape}"
                  )
                  continue
              elif incompatible_shape_action == "match_mean_std":
                  logger.debug(
                      f"transfer_weights found that {name} weights tensor have incompatible shape {value.shape} and model has shape {existing_value.shape}. "
                      f"Initializing with random values with same mean {existing_value.mean()} and std {existing_value.std()} from corresponding checkpoint weights tensor."
                  )
                  torch.nn.init.normal_(
                      existing_value, mean=value.mean(), std=value.std()
                  )
                  value = existing_value
              else:
                  raise ValueError(
                      f"Unsupported incompatible_shape_action={incompatible_shape_action}"
                  )
  
          try:
              model.load_state_dict(
                  collections.OrderedDict([(name, value)]), strict=False
              )
          except Exception as e:
              logger.debug(
                  f"transfer_weights skipped loading weights for key {name}, because of error: {e}"
              )
  
imports: import collections
  import dataclasses
  import functools
  import warnings
  import logging
  from typing import Optional, Sequence, Union, Dict, List, Any, Iterable, Callable
  import numpy
  import torch
  from torch import nn, Tensor
  from torch.utils.data import Dataset, ConcatDataset
  from torch.utils.data.dataloader import default_collate
  from .support import pytorch_toolbelt_deprecated
label_desc: Use image node_id 21 for calling a global function with example usage: transfer_weights(model, model_state_dict, incompatible_shape_action) without return types