Exporting, Saving, Loading & Deploying Models
Keras Basics
2 min read
This section is 2 min read, full guide is 25 min read
Published Nov 17 2025
11
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
KerasNeural NetworksPythonTensorFlow
Once a model is trained, you often need to:
- Save it for later use
- Load it back into memory
- Share it with others
- Deploy it in an application
- Convert it to run on mobile or the browser
- Use it for inference in production
Keras makes this easy with a few core formats.
Saving Models
Keras has two main save formats:
- Keras SavedModel format (
.keras) → recommended - HDF5 format (
.h5) → legacy, still widely used
Save Entire Model (Recommended)
Saves:
- Architecture
- Weights
- Optimiser state
- Training history metadata
model.save("my_model.keras")
Copy to Clipboard
Save Only Weights
model.save_weights("my_weights.h5")
Copy to Clipboard
later:
model.load_weights("my_weights.h5")
Copy to Clipboard
Save Architecture as JSON
json_config = model.to_json()
with open("model.json", "w") as f:
f.write(json_config)
Copy to Clipboard
This does NOT include weights.
Loading Models
Load full model:
import tensorflow as tf
model = tf.keras.models.load_model("my_model.keras")
Copy to Clipboard
Load weights into a recreated architecture:
# recreate architecture
model = make_model()
model.load_weights("my_weights.h5")
Copy to Clipboard
Toggle show comments
Saving and Loading During Training
ModelCheckpoint callback:
checkpoint = tf.keras.callbacks.ModelCheckpoint(
"best_model.keras",
save_best_only=True
)
Copy to Clipboard
Later:
best_model = tf.keras.models.load_model("best_model.keras")
Copy to Clipboard
Exporting for TensorFlow Serving
TensorFlow Serving expects the SavedModel format:
model.save("exported_model")
Copy to Clipboard
Directory structure:
exported_model/
assets/
variables/
saved_model.pb
Deploy using:
tensorflow_model_server --model_base_path=/path/to/exported_model
Copy to Clipboard
Inference (Prediction) After Deployment
Once a model is loaded, you can make predictions with:
predictions = model.predict(new_data)
Copy to Clipboard
Example:
img = tf.image.resize(img, (160, 160))/255
pred = model.predict(img[None, ...])[0][0]
print("Dog" if pred > 0.5 else "Cat")
Copy to Clipboard
Exporting to TensorFlow Lite
TensorFlow Lite (TFLite) is used for:
- Android apps
- iOS apps
- Raspberry Pi
- Microcontrollers (with TinyML)
- Embedded devices
Convert Keras model to TFLite:
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open("model.tflite", "wb") as f:
f.write(tflite_model)
Copy to Clipboard
Enable Optimisations (Smaller + Faster)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
Copy to Clipboard
This reduces file size drastically.
Running a TFLite Model
import numpy as np
import tensorflow as tf
interpreter = tf.lite.Interpreter(model_path="model.tflite")
interpreter.allocate_tensors()
input_idx = interpreter.get_input_details()[0]["index"]
output_idx = interpreter.get_output_details()[0]["index"]
interpreter.set_tensor(input_idx, input_data)
interpreter.invoke()
pred = interpreter.get_tensor(output_idx)
Copy to Clipboard
Exporting to TensorFlow.js (Browser Deployment)
Install the converter:
pip install tensorflowjs
Copy to Clipboard
Convert:
tensorflowjs_converter --input_format=keras_saved_model \
exported_model \
web_model
Copy to Clipboard
This allows loading in a web app:
const model = await tf.loadLayersModel('web_model/model.json');
Copy to Clipboard














