Sub topic 13
TensorFlow and Keras Formats and Their Risks
Assume you know basic Python, have used a deep-learning framework such as PyTorch or TensorFlow, and understand that model weights are learned numbers. The important distinction here is between storing tensor data and storing instructions for rebuilding a model.
TensorFlow SavedModel
A TensorFlow SavedModel is a directory rather than one file. It commonly contains:
- A computational graph, which describes operations and their connections.
- Variable data, which contains weights.
- Metadata and serving signatures.
- Optional assets, such as vocabulary files.
The graph is usually stored in protocol-buffer files, and the variables are stored in checkpoint files. SavedModel does not normally use Python pickle to store the graph. Loading it therefore does not have the same automatic arbitrary-Python-code behavior as ordinary unpickling.
However, “not pickle” does not mean “always safe.” A model can contain custom operations, external assets, or signatures that perform unexpected work when called. A native custom operation is compiled code, so it must be treated as trusted software. A serving system should also restrict network access, file access, and permissions around model assets.
HDF5 and legacy Keras files
Older Keras workflows commonly saved models as .h5 or .hdf5 files. These files can contain:
- A JSON model configuration.
- Layer settings and connections.
- Tensor weights.
- Optimizer state.
HDF5 itself is a general-purpose container. The risk comes from what the framework does with the configuration. To rebuild a model, Keras may look up layer classes, functions, losses, metrics, or other custom objects. A malicious or incompatible configuration can exploit deserialization behavior, or simply cause code to run when a custom object is imported or constructed.
Lambda layers are a particular portability concern. Older Keras formats could serialize the Python bytecode for a Lambda layer. Loading that bytecode is unsafe when the file is untrusted and can fail across Python versions. Prefer named, registered layer classes over serialized anonymous Python functions.
The newer `.keras` format
Keras 3 uses .keras as its native archive format. It is a ZIP-based package containing configuration, metadata, and weight data. This separates much of the model description from executable Python code, but the configuration still needs to be deserialized into Keras objects.
Keras provides safe_mode=True when loading a .keras model. This blocks unsafe lambda deserialization. It does not make an unknown model trustworthy, and it does not remove risks from custom code, custom operations, or dependencies.
import keras
# Use only for a model from a trusted source or after inspection.
model = keras.models.load_model(
"model.keras",
safe_mode=True,
)
print(model.input_shape)A model that needs a custom layer may require an explicit custom_objects mapping. Review that code before supplying it.
Practical comparison
| Format | Main contents | Main risk |
|---|---|---|
| SavedModel | Graph, variables, assets | Custom operations and untrusted serving behavior |
| HDF5 Keras file | Configuration and weights | Unsafe custom-object or Lambda deserialization |
| `.keras` archive | Keras configuration and weights | Custom code and unsafe deserialization choices |
Exercise
Why is a .keras file safer with safe_mode=True, but not automatically safe?
Show answer
safe_mode=True blocks unsafe Lambda deserialization, but custom objects, custom operations, dependencies, and model assets can still introduce risk.