Exporting, Saving, Loading & Deploying Models

Keras Basics

2 min read

Published Nov 17 2025


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 tf
model = tf.keras.models.load_model("my_model.keras")

Load weights into a recreated architecture:

# recreate architecture
model = 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))/255
pred = 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 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)





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');

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact