Exporting, Saving, Loading & Deploying Models

Keras Basics

2 min read

Published Nov 17 2025, updated Aug 17 2026


11
0
0
0

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")


Save Only Weights

model.save_weights("my_weights.h5")

later:

model.load_weights("my_weights.h5")


Save Architecture as JSON

json_config = model.to_json()with open("model.json", "w") as f:    f.write(json_config)

This does NOT include weights.






Loading Models

Load full model:

import tensorflow as tfmodel = tf.keras.models.load_model("my_model.keras")

Load weights into a recreated architecture:

# recreate architecturemodel = make_model() model.load_weights("my_weights.h5")





Saving and Loading During Training

ModelCheckpoint callback:

checkpoint = tf.keras.callbacks.ModelCheckpoint(    "best_model.keras",    save_best_only=True)

Later:

best_model = tf.keras.models.load_model("best_model.keras")





Exporting for TensorFlow Serving

TensorFlow Serving expects the SavedModel format:

model.save("exported_model")

Directory structure:

exported_model/    assets/    variables/    saved_model.pb

Deploy using:

tensorflow_model_server --model_base_path=/path/to/exported_model





Inference (Prediction) After Deployment

Once a model is loaded, you can make predictions with:

predictions = model.predict(new_data)

Example:

img = tf.image.resize(img, (160, 160))/255pred = model.predict(img[None, ...])[0][0]print("Dog" if pred > 0.5 else "Cat")





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)

Enable Optimisations (Smaller + Faster)

converter.optimizations = [tf.lite.Optimize.DEFAULT]

This reduces file size drastically.






Running a TFLite Model

import numpy as npimport tensorflow as tfinterpreter = 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)





Exporting to TensorFlow.js (Browser Deployment)

Install the converter:

pip install tensorflowjs

Convert:

tensorflowjs_converter --input_format=keras_saved_model \    exported_model \    web_model

This allows loading in a web app:

const model = await tf.loadLayersModel('web_model/model.json');
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact
Keras Basics | Exporting, Saving, Loading & Deploying Models | SimpleSteps.guide