Compare
Keras vs ONNX: training saves and inference exports
.keras saves model configuration, weights and any optimizer state in Keras. ONNX represents computation graphs for compatible runtimes.
Which format should you choose?
Consider .keras to reload a model and continue training in Keras, or ONNX to deliver inference to another compatible runtime.
Good fit for Keras
Saving a Keras v3 model configuration weights and available training state in one archive · Saving and reloading models in compatible Keras environments
- Saving models built mainly from serializable Keras layers
- Managing model configuration and weights together
Good fit for ONNX
Exchanging machine-learning computation graphs and weights with supported runtimes · Moving models between training frameworks and inference environments
- Inference with runtimes that support the model's operators and opset
- Exchanging a model graph with inputs outputs and types
Basic information
| Field | Keras | ONNX |
|---|---|---|
| Extension | .keras | .onnx |
| MIME type | application/zip | application/octet-stream |
| Common uses | Saving a Keras v3 model configuration weights and available training state in one archive · Saving and reloading models in compatible Keras environments | Exchanging machine-learning computation graphs and weights with supported runtimes · Moving models between training frameworks and inference environments |
| How to open | Load it with a compatible Keras version using `load_model`. If custom layers or objects are used, register trusted implementations in the environment and normally keep safe mode enabled. ZIP tools can inspect the archive, but editing extracted contents may prevent successful loading. | Load it with an ONNX checker or compatible inference runtime. Before deployment, inspect IR and opset versions, operators, input/output types and shapes, and runtime support; compare outputs with the source model on representative inputs. |
| Export or convert | Save the whole model with Keras `save` or `saving.save_model`. For inference-only delivery, whole-model `.keras` saving and `export` serve different purposes. Reload the saved artifact in the target Keras environment and verify representative outputs, weights, and any needed compile or optimizer state. | Export using the source framework's ONNX exporter and validate the structure with an ONNX checker. Successful conversion does not guarantee numerical equivalence or execution on the target device. If external data is used, keep the model and every referenced file together in the distribution unit. |
Choose by use case
Saving models built mainly from serializable Keras layers
Saving a Keras v3 model configuration weights and available training state in one archive · Saving and reloading models in compatible Keras environments
KerasManaging model configuration and weights together
Saving a Keras v3 model configuration weights and available training state in one archive · Saving and reloading models in compatible Keras environments
KerasInference with runtimes that support the model's operators and opset
Exchanging machine-learning computation graphs and weights with supported runtimes · Moving models between training frameworks and inference environments
ONNXExchanging a model graph with inputs outputs and types
Exchanging machine-learning computation graphs and weights with supported runtimes · Moving models between training frameworks and inference environments
ONNXKey point 1
Keras save preserves model configuration and state; custom-object implementations may be needed separately.
More on the differencesKey point 2
ONNX includes typed inputs and outputs, a computation graph and opset information; execution requires a compatible runtime.
More on the differencesMore on the differences
Saving .keras and exporting ONNX are separate operations. Check Keras version, backend and operation support, retaining the original .keras and dependency code when creating an inference artifact.
- The official Keras export API supports ONNX as an output format. Record input types, shapes and the chosen opset; check that the destination runtime implements all operations.
- Compare outputs with the source using identical preprocessing and representative inputs, documenting tolerances and device conditions. ONNX structural checks alone do not guarantee numerical agreement, speed or retention of Keras training state.
Frequently asked questions
Can I delete .keras and resume training from an ONNX export?
Do not assume so. An inference export is not guaranteed to reconstruct the Keras save, including optimizer state and custom objects. Keep the original .keras, required code and environment details.