class_docstr: A class providing methods for saving and loading prediction head modules from the file system.
  
  Model classes supporting configurable head modules via config files should provide a prediction head dict at
  `model.heads` and a method `add_prediction_head(head_name, config)`.
class_docstr_tok: ["A","class","providing","methods","for","saving","and","loading","prediction","head","modules","from","the","file","system",".","Model","classes","supporting","configurable","head","modules","via","config","files","should","provide","a","prediction","head","dict","at","`","model.heads","`","and","a","method","`","add_prediction_head","(","head_name",",","config",")","`","."]
docstr: Loads a prediction head module from the given directory.
  
  Args:
      save_directory (str): The directory from where to load the weights.
      load_as (str, optional): Load the weights with this name. Defaults to None.
  
  Returns:
      Tuple[str, str]: A tuple consisting of the local file system directory from which the weights where loaded
      and the name of the loaded weights.
code: def load(
      self, save_directory, load_as=None, loading_info=None, **kwargs
  ):
      """
      Loads a prediction head module from the given directory.
  
      Args:
          save_directory (str): The directory from where to load the weights.
          load_as (str, optional): Load the weights with this name. Defaults to None.
  
      Returns:
          Tuple[str, str]: A tuple consisting of the local file system directory from which the weights where loaded
          and the name of the loaded weights.
      """
      if not exists(join(save_directory, HEAD_WEIGHTS_NAME)):
          if self.error_on_missing:
              raise ValueError(
                  "Loading path should be a directory where the head is saved."
              )
          else:
              logger.info(
                  "No matching prediction head found in '{}'".format(
                      save_directory
                  )
              )
              return None, None
      # label2id map used to override default
      custom_id2label = kwargs.pop("id2label", None)
      if custom_id2label:
          custom_label2id = {
              label: id_ for id_, label in custom_id2label.items()
          }
      else:
          custom_label2id = None
  
      head_name = None
      conversion_rename_func = None
  
      # Load head config if available - otherwise just blindly try to load the weights
      if isfile(join(save_directory, HEAD_CONFIG_NAME)):
          config = self.weights_helper.load_weights_config(
              save_directory
          )
          # make sure that the model class of the loaded head matches the current class
          if (
              not self.convert_to_flex_head
              and self.model.__class__.__name__ != config["model_class"]
          ):
              error_msg = (
                  f"Model class '{config['model_class']}' of found prediction head does not match current model"
                  " class."
              )
              if self.error_on_missing:
                  raise ValueError(error_msg)
              else:
                  logger.warning(error_msg)
                  return None, None
  
          # model with flex heads
          if hasattr(self.model, "heads"):
              # load head of same model class, no conversion needed
              if self.model.__class__.__name__ == config["model_class"]:
                  head_name = load_as or config["name"]
                  head_config = config["config"]
              elif config["model_class"].endswith("ModelWithHeads"):
                  this_class = self.model.__class__.__name__.replace(
                      "AdapterModel", ""
                  )
                  other_class = config["model_class"].replace(
                      "ModelWithHeads", ""
                  )
                  if this_class == other_class:
                      head_name = load_as or config["name"]
                      head_config = config["config"]
                  else:
                      raise ValueError(
                          f"Cannot automatically convert prediction head of model class {config['model_class']} to"
                          " flex head."
                      )
              # try to convert a static head to a flex head
              elif (
                  self.convert_to_flex_head
                  and config["model_class"] in STATIC_TO_FLEX_HEAD_MAP
              ):
                  head_name = kwargs.pop("main_load_name", load_as)
                  if head_name is None:
                      raise ValueError(
                          "Could not identify a name for the prediction head to be loaded. Please specify 'load_as'."
                      )
                  (
                      head_config,
                      conversion_rename_func,
                  ) = get_head_config_and_rename_list(
                      config["model_class"],
                      head_name,
                      custom_label2id or config.get("label2id"),
                      num_labels=config.get("num_labels"),
                  )
              else:
                  raise ValueError(
                      f"Cannot automatically convert prediction head of model class {config['model_class']} to flex"
                      " head."
                  )
              if head_name in self.model.heads:
                  logger.warning(
                      "Overwriting existing head '{}'".format(head_name)
                  )
  
              # make sure the label2id map is correct
              custom_label2id = custom_label2id or head_config.get(
                  "label2id", None
              )
              if custom_label2id:
                  head_config["id2label"] = {
                      int(id_): label
                      for label, id_ in custom_label2id.items()
                  }
                  head_config["label2id"] = {
                      label: int(id_)
                      for label, id_ in custom_label2id.items()
                  }
  
              self.model.add_prediction_head_from_config(
                  head_name,
                  head_config,
                  overwrite_ok=True,
                  set_active=kwargs.pop("set_active", True),
              )
          # model with static head
          else:
              if self.convert_to_flex_head:
                  raise ValueError(
                      "Cannot set convert_flex_head on model class with static head."
                  )
              custom_label2id = custom_label2id or config.get(
                  "label2id", None
              )
              if custom_label2id:
                  self.model.config.id2label = {
                      int(id_): label
                      for label, id_ in custom_label2id.items()
                  }
                  self.model.config.label2id = {
                      label: int(id_)
                      for label, id_ in custom_label2id.items()
                  }
  
      # Load head weights
      filter_func = self.filter_func(head_name)
      rename_funcs = []
      if load_as:
          rename_funcs.append(self.rename_func(config["name"], load_as))
      if conversion_rename_func:
          rename_funcs.append(conversion_rename_func)
      self.weights_helper.load_weights(
          save_directory,
          filter_func,
          rename_func=rename_funcs,
          loading_info=loading_info,
      )
  
      return save_directory, head_name
  
imports: import json
  import logging
  from abc import ABC, abstractmethod
  from os import mkdir
  from os.path import exists, isdir, isfile, join
  from typing import Callable, Mapping, Sequence, Tuple
  import torch
  from .configuration import AdapterConfig, build_full_config
  from .head_utils import STATIC_TO_FLEX_HEAD_MAP, get_head_config_and_rename_list
  from .utils import ACTIVATION_RENAME, ADAPTERFUSION_CONFIG_NAME, ADAPTERFUSION_WEIGHTS_NAME, CONFIG_NAME, HEAD_CONFIG_NAME, HEAD_WEIGHTS_NAME, WEIGHTS_NAME, AdapterType, resolve_adapter_path
label_desc: Use image node_id 5 for calling the PredictionHeadLoader obj's underlying member method code with example usage: obj.load(save_directory, load_as, loading_info) and returns: save_directory,  head_name, None,  None, None,  None