code: def _load_model(
      fpath, preproc=None, train_data=None, custom_objects=None
  ):
      if not preproc and not train_data:
          raise ValueError("Either preproc or train_data is required.")
      if (
          preproc and isinstance(preproc, TransformersPreprocessor)
      ) or (train_data and U.is_huggingface(data=train_data)):
          if preproc:
              model = preproc.get_model(fpath=fpath)
              # if model_name is local_path, update it to reflect current predictor folder
              # in case learner was trained with local path on different machine
              # TODO: support this for Windows paths
              if preproc.model_name.startswith(os.sep):
                  preproc.model_name = fpath
          else:
              model = TransformersPreprocessor.load_model_and_configure_from_data(
                  fpath, train_data
              )
          return model
      elif (
          (
              preproc
              and (
                  isinstance(preproc, BERTPreprocessor)
                  or type(preproc).__name__ == "BERTPreprocessor"
              )
          )
          or train_data
          and U.bert_data_tuple(train_data)
      ):
          # custom BERT model
          check_keras_bert()
          if isinstance(custom_objects, dict):
              custom_objects.update(keras_bert.get_custom_objects())
          else:
              custom_objects = keras_bert.get_custom_objects()
      elif (
          (
              preproc
              and (
                  isinstance(preproc, NERPreprocessor)
                  or type(preproc).__name__ == "NERPreprocessor"
              )
          )
          or train_data
          and U.is_ner(data=train_data)
      ):
          from .text.ner.anago.layers import CRF, crf_loss
  
          custom_objects = {"CRF": CRF, "crf_loss": crf_loss}
          # save old te_model as backup
          if preproc:
              old_te_model = preproc.p.te_model
              # load TransformerEmbedding model from fpath/hf folder
              # if model_name is local_path, update it to reflect current predictor folder, since
              # all model/tokenizer/config files should have been saved there by predictor.save
  
              preproc.p.te_model = (
                  os.path.join(fpath, "hf")
                  if preproc.p.te_model
                  else preproc.p.te_model
              )
              if preproc.p.te_model:
                  # te_model should point fpath/hf folder
                  try:
                      preproc.p.activate_transformer(
                          preproc.p.te_model, layers=preproc.p.te_layers
                      )
                  except:
                      # fall back to old model id or location if error for backwards compatibility
                      warnings.warn(
                          f"could not load TransformerEmbedding model from {preproc.p.te_model} - trying {old_te_model}"
                      )
                      preproc.p.te_model = old_te_model
                      preproc.p.activate_transformer(
                          preproc.p.te_model, layers=preproc.p.te_layers
                      )
  
      elif (
          (
              preproc
              and (
                  isinstance(preproc, NodePreprocessor)
                  or type(preproc).__name__ == "NodePreprocessor"
              )
          )
          or train_data
          and U.is_nodeclass(data=train_data)
      ):
          from stellargraph.layer import MeanAggregator
  
          custom_objects = {"MeanAggregator": MeanAggregator}
      elif (
          (
              preproc
              and (
                  isinstance(preproc, LinkPreprocessor)
                  or type(preproc).__name__ == "LinkPreprocessor"
              )
          )
          or train_data
          and U.is_linkpred(data=train_data)
      ):
          from stellargraph.layer import MeanAggregator
  
          custom_objects = {"MeanAggregator": MeanAggregator}
      custom_objects = {} if custom_objects is None else custom_objects
      from .lroptimize.optimization import AdamWeightDecay
  
      custom_objects["AdamWeightDecay"] = AdamWeightDecay
      try:
          try:
              model = keras.models.load_model(
                  os.path.join(fpath, U.MODEL_NAME),
                  custom_objects=custom_objects,
              )
          except:
              try:
                  # pre-0.16: model fpath was file name of model not folder for non-Transformer models
                  # warnings.warn('could not load model as %s - attempting to load model as %s' % (os.path.join(fpath, U.MODEL_NAME), fpath))
                  model = keras.models.load_model(
                      fpath, custom_objects=custom_objects
                  )
              except:
                  # for bilstm models without CRF layer on TF2 where CRF is not supported
                  model = keras.models.load_model(
                      fpath,
                      custom_objects={
                          "AdamWeightDecay": AdamWeightDecay
                      },
                  )
      except Exception as e:
          print(
              "Call to keras.models.load_model failed. Try manually invoking this function to investigate error and report issue if necessary."
          )
          raise Exception("Error detected: %s" % (e))
  
      # see issue https://github.com/amaiya/ktrain/issues/21
      if hasattr(model, "_make_predict_function"):
          model._make_predict_function()
  
      return model
  
imports: from .None import utils
  from .graph.predictor import LinkPredictor, NodePredictor
  from .graph.preprocessor import LinkPreprocessor, NodePreprocessor
  from .imports import *
  from .lroptimize.lrfinder import *
  from .tabular.predictor import TabularPredictor
  from .tabular.preprocessor import TabularPreprocessor
  from .text.ner.predictor import NERPredictor
  from .text.ner.preprocessor import NERPreprocessor
  from .text.predictor import TextPredictor
  from .text.preprocessor import BERTPreprocessor, TextPreprocessor, TransformersPreprocessor
  from .vision.predictor import ImagePredictor
  from .vision.preprocessor import ImagePreprocessor
label_desc: Use image node_id 4 for calling a global function with example usage: _load_model(fpath, preproc, train_data, custom_objects) and returns: model, model