code: def freeze_layers(
      model: nn.Module, freeze_vars=None, exclude_vars=None
  ):
      """
      A utilty function to help freeze specific layers.
  
      Args:
          model: a source PyTorch model to freeze layer.
          freeze_vars: a regular expression to match the `model` variable names,
              so that their `requires_grad` will set to `False`.
          exclude_vars: a regular expression to match the `model` variable names,
              except for matched variable names, other `requires_grad` will set to `False`.
  
      Raises:
          ValueError: when freeze_vars and exclude_vars are both specified.
  
      """
      if freeze_vars is not None and exclude_vars is not None:
          raise ValueError(
              "Incompatible values: freeze_vars and exclude_vars are both specified."
          )
      src_dict = get_state_dict(model)
  
      frozen_keys = list()
      if freeze_vars is not None:
          to_freeze = {
              s_key
              for s_key in src_dict
              if freeze_vars and re.compile(freeze_vars).search(s_key)
          }
          for name, param in model.named_parameters():
              if name in to_freeze:
                  param.requires_grad = False
                  frozen_keys.append(name)
              elif not param.requires_grad:
                  param.requires_grad = True
                  warnings.warn(
                      f"The freeze_vars does not include {param}, but requires_grad is False, change it to True."
                  )
      if exclude_vars is not None:
          to_exclude = {
              s_key
              for s_key in src_dict
              if exclude_vars and re.compile(exclude_vars).search(s_key)
          }
          for name, param in model.named_parameters():
              if name not in to_exclude:
                  param.requires_grad = False
                  frozen_keys.append(name)
              elif not param.requires_grad:
                  param.requires_grad = True
                  warnings.warn(
                      f"The exclude_vars includes {param}, but requires_grad is False, change it to True."
                  )
  
      logger.info(
          f"{len(frozen_keys)} of {len(src_dict)} variables frozen."
      )
  
imports: from __future__ import annotations
  import io
  import re
  import warnings
  from collections import OrderedDict
  from collections.abc import Callable, Mapping, Sequence
  from contextlib import contextmanager
  from copy import deepcopy
  from typing import Any
  import numpy
  import torch
  import torch.nn
  from monai.apps.utils import get_logger
  from monai.config import PathLike
  from monai.utils.misc import ensure_tuple, save_obj, set_determinism
  from monai.utils.module import look_up_option, optional_import, pytorch_after
  from monai.utils.type_conversion import convert_to_dst_type, convert_to_tensor
label_desc: Use image node_id 25 for calling a global function with example usage: freeze_layers(model, freeze_vars, exclude_vars) without return types