Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Function

keras.models.load_model

Loads a Keras model from a saved model file or Orbax checkpoint. It can restore compilation and resolve custom serialized objects while protecting against unsafe lambda deserialization by default.

How it works

The function deserializes a model from a Keras file or Orbax checkpoint, reconstructing its configuration and state. It can also restore compilation, while custom classes and functions can be supplied for deserialization.

Syntax

keras.saving.load_model(filepath, custom_objects=None, compile=True, safe_mode=True)

Parameters

NameTypeDescription
filepathstr | pathlib.PathString or pathlib.Path identifying the saved model file or Orbax checkpoint directory.
custom_objectsdict | NoneOptional mapping from serialized names to custom classes or functions.
compileboolWhether to compile the model after loading.
safe_modeboolWhether to disallow unsafe lambda deserialization.

Returns

A Keras model instance. If the original model was compiled and compile=True, the returned model is compiled; otherwise it is uncompiled.

Key points

  • The function accepts models saved with model.save() as well as Orbax checkpoints.
  • Use custom_objects to resolve custom classes or functions referenced by the serialized model.
  • The compile parameter determines whether the deserialized model is compiled.
  • The safe_mode parameter controls whether unsafe lambda deserialization is blocked.
  • The safe_mode option applies to the Keras v3 model format.

Common mistakes

  • Do not disable safe mode for untrusted model files because deserialization can execute arbitrary code.
  • A loaded model is not necessarily compiled: compilation is restored only when the saved model was compiled and compile is true.

Example: Load a saved model

import keras

model = keras.models.load_model("classifier.keras")
predictions = model.predict(x_test)

Loads a saved Keras model and uses the returned model for inference.

When to use it

Inference and evaluation

Restore a previously saved model for prediction or evaluation.

Custom-object restoration

Reconstruct a model that uses registered custom layers or functions.

Checkpoint recovery

Resume work with model state stored in an Orbax checkpoint directory.

Interview questions

QWhat does keras.models.load_model do?

It loads a model saved with model.save(), or an Orbax checkpoint, and returns a Keras model instance. The model's configuration and saved state are reconstructed during deserialization.

Follow-up: How would you load a model containing custom layers? Pass a custom_objects dictionary mapping the serialized names to the corresponding classes or functions.

QWhat is the purpose of the compile argument?

compile controls whether Keras compiles the model after loading. With its default value of true, a previously compiled model is returned compiled; otherwise the returned model remains uncompiled.

Follow-up: When might you deliberately set compile to false? Set it when only inference is needed or when the saved training configuration should not be restored.

QWhy does safe_mode matter?

safe_mode defaults to true and prevents unsafe lambda deserialization. Setting it to false permits that deserialization but can allow arbitrary code execution, so it should only be used with trusted files.

Follow-up: What input types can filepath identify? It can be a string or pathlib.Path pointing to a saved model file or an Orbax checkpoint directory.

  • keras.saving.save_model
  • keras.Model.save
  • keras.saving.load_assets
  • keras.saving.load_own_variables

Source: Whole model saving & loading. Checked 2026-10-15.