class_docstr: Utilities to freeze/unfreeze layers.
  
  Args:
      model:
          a model with weights.
  Return:
      LayerFreezer.
  
  Example:
      >>> from towhee.trainer.utils.layer_freezer import LayerFreezer
      >>> from towhee.models import vit
      >>> my_model = vit.create_model()
      >>> my_freezer = LayerFreezer(my_model)
      >>> # Check if modules in the last layer are frozen
      >>> my_freezer.status(-1)
      >>> # Check if modules in the layer "head" are frozen
      >>> my_freezer.status("head")
      ['unfrozen', 'unfrozen']
      >>> # Show all frozen layers
      >>> my_freezer.show_frozen_layers()
      ['patch_embed', 'head']
      >>> # Freeze layers by a list of layer indexes
      >>> my_freezer.by_idx([0, -1])
      >>> # Freeze layers by a list of layer names
      >>> my_freezer.by_names(['head'])
      >>> # Freeze all layers
      >>> my_freezer.set_all()
      >>> # Unfreeze all layers
      >>> my_freezer.set_all(freeze=False)
      >>> # Freeze all except the last layer
      >>> my_freezer.set_slice(-1)
docstr: Freeze/unfreeze layers by names
  
  Args:
      names (`list`):
          a list of layer names
      freeze (`bool`):
          if or not freeze layers (default: True)
code: def by_names(self, names: list, freeze: bool = True):
      """
      Freeze/unfreeze layers by names
  
      Args:
          names (`list`):
              a list of layer names
          freeze (`bool`):
              if or not freeze layers (default: True)
      """
      if not set(names).issubset(set(self.layer_names)):
          invalid_names = set(names) - set(self.layer_names)
          raise ValueError(f"Layer does not exist: {invalid_names}")
      for name, layer in self.model.named_children():
          if name not in names:
              pass
          else:
              for param in layer.parameters():
                  param.requires_grad = not freeze
  
file_path: towhee/towhee/trainer/utils/layer_freezer.py
imports: from typing import Union
label_desc: Use image node_id 4 for calling the LayerFreezer obj's underlying member method code with example usage: obj.by_names(names, freeze) without return types