CX Learn by CadetX
Exams My Dashboard
Keras · TensorFlow · Deep Learning
KR-101 · Module 10 Intermediate about 45 minutes 8 Lessons Prereq: Modules 1 to 9

Pretrained Models & Transfer Learning

← →

You don't have to start from scratch. Keras gives you powerful models that have already learned from millions of images and huge amounts of text, ready to adapt to your own problem. In this final module you'll use pretrained models with transfer learning, then learn to save your models and get them out of the notebook and into the real world.

  • Level: Intermediate
  • Time: about 45 minutes
  • Needs: Modules 1 to 9

By the end of this module you will be able to

  • Explain transfer learning and why it saves time and data
  • Load pretrained image models from Keras Applications
  • Freeze and unfreeze layers for feature extraction and fine-tuning
  • Use a pretrained text model from KerasHub
  • Save and load whole models and weights
  • Export models for serving, phones and browsers
  • Choose a deployment option for a real project

10.1Why use a pretrained model?

Training a large image model from scratch can take millions of labelled photos and days of computing. But a model already trained on a huge dataset has learned useful, general skills: its early layers recognise edges, textures and shapes that appear in almost any picture. Transfer learning reuses those skills for your own task.

Like hiring someone experienced

An experienced chef who joins a new restaurant doesn't need to learn how to hold a knife. They just learn the new menu. A pretrained model already knows how to "see"; you only teach it your specific classes.

From scratchWith a pretrained model
Labelled data neededOften tens of thousands of examplesOften a few hundred per class
Training timeHours to daysMinutes
Accuracy with little dataUsually poorUsually much better

10.2Keras Applications

Keras Applications is a collection of famous image models, pretrained on ImageNet (over a million photos of 1,000 kinds of object), each available in one line:

ModelSizeGood for
MobileNetV2 / MobileNetV3SmallFast; laptops and phones. A great first choice
EfficientNetB0 to B7Small to largeHigh accuracy for their size
ResNet50MediumA reliable, well-known workhorse
ConvNeXtMedium to largeModern, very accurate designs

Three settings matter when you load one:

SettingMeaning
weights="imagenet"Download the pretrained weights (or None for random weights)
include_top=FalseRemove the original 1,000-class output layer, so you can add your own
input_shape=(160, 160, 3)The size of your images

Don't forget preprocess_input

Each model expects images scaled exactly the way its training images were. Use that model's own function, such as keras.applications.mobilenet_v2.preprocess_input, instead of your usual Rescaling layer. Getting this wrong quietly ruins accuracy.

10.3Feature extraction and fine-tuning

There are two stages, and most projects use both:

StageWhat you doLearning rate
1. Feature extractionFreeze the whole pretrained model with base.trainable = False, and train only your new output headNormal (Adam's default, 0.001)
2. Fine-tuningUnfreeze the top layers of the pretrained model and train a little more, so they adapt to your imagesVery small, e.g. 0.00001

Always train the new head first: its random weights produce large, messy updates at the start, which would damage the pretrained weights if they were unfrozen. And call the base model with training=False so its batch normalisation layers keep the statistics they learned. After changing trainable, always compile again for the change to take effect.

Here's a complete, ready-to-use image classifier template. Put your photos in one folder per class and run it in Google Colab:

transfer_learning.py
# Run in Google Colab with a GPU. Put your photos in photos/<class_name>/ folders.
import keras
from keras import layers

train_ds = keras.utils.image_dataset_from_directory(
    "photos", validation_split=0.2, subset="training", seed=1,
    image_size=(160, 160), batch_size=32)
val_ds = keras.utils.image_dataset_from_directory(
    "photos", validation_split=0.2, subset="validation", seed=1,
    image_size=(160, 160), batch_size=32)
n_classes = len(train_ds.class_names)

base = keras.applications.MobileNetV2(input_shape=(160, 160, 3),
                                      include_top=False, weights="imagenet")
base.trainable = False                                       # stage 1: freeze

inputs = keras.Input(shape=(160, 160, 3))
x = layers.RandomFlip("horizontal")(inputs)                  # augmentation (Module 8)
x = keras.applications.mobilenet_v2.preprocess_input(x)      # scale like ImageNet
x = base(x, training=False)
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dropout(0.2)(x)
outputs = layers.Dense(n_classes, activation="softmax")(x)
model = keras.Model(inputs, outputs)

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(train_ds, validation_data=val_ds, epochs=10)       # train the new head

base.trainable = True                                        # stage 2: fine-tune
for layer in base.layers[:-20]:
    layer.trainable = False
model.compile(optimizer=keras.optimizers.Adam(1e-5),         # a tiny learning rate
              loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(train_ds, validation_data=val_ds, epochs=10)
model.save("my_classifier.keras")
▶ Open Google ColabPaste the code into a new Colab notebook and press Run. Keras is already installed.

We haven't shown output for this template because it downloads the pretrained weights and uses your own photos. Run it in Colab with a GPU turned on.

10.4Pretrained text models with KerasHub

For text, the best results today come from pretrained Transformer models, which have read enormous amounts of text. KerasHub, the official Keras library of pretrained models, lets you load and fine-tune them in a few lines. Each model comes as a ready-made preset:

keras_hub_text.py
# pip install keras-hub      (run in Colab)
import keras_hub

classifier = keras_hub.models.TextClassifier.from_preset(
    "bert_tiny_en_uncased",     # a small pretrained BERT model
    num_classes=2)              # e.g. negative / positive
classifier.fit(x=train_texts, y=train_labels, batch_size=16, epochs=2)
classifier.predict(["the delivery was not great"])
▶ Open Google ColabPaste the code into a new Colab notebook and press Run. Keras is already installed.

Unlike the Module 9 models, a pretrained model already knows that "bad" and "awful" mean similar things, even if your training data never used them. KerasHub also includes image models and larger language models. Browse its presets on the Keras website and on Hugging Face.

10.5Saving and loading models

What to saveCodeIncludes
The whole model (recommended)model.save("model.keras")
keras.models.load_model("model.keras")
Architecture, weights, compile settings and optimiser state, in one file
Weights onlymodel.save_weights("model.weights.h5")
model.load_weights("model.weights.h5")
Just the numbers; you must rebuild the same architecture first
Best model during trainingModelCheckpoint callback (Module 5)Saves automatically whenever the model improves

Because layers like Normalization, Rescaling, TextVectorization and augmentation are part of the model, they're saved too. That's a big advantage: the saved model accepts raw data and prepares it exactly as in training.

10.6Deploying your model

A model is only useful once people can use it. The right format depends on where it will run:

Try it: deployment chooser

RecommendedFormat
deploy.py

Before you deploy

Check that the model works on fresh, real-world data, not just your test set. Record which data and settings you used. Monitor predictions once it's live, because the real world changes over time and models can slowly get worse (called data drift). And think about fairness and privacy: who could be affected if the model is wrong?

10.7Freezing, saving and exporting in Python

This example runs anywhere, with no downloads. First we build MobileNetV2 (with random weights, to avoid a download) and see how freezing changes what trains. Then we train a small digits model and take it all the way from a .keras file to a phone-ready TensorFlow Lite model, checking the predictions at every step.

save_and_deploy.py
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import numpy as np
import keras
from keras import layers
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split

keras.utils.set_random_seed(0)

# ---------- 1. A pretrained architecture: MobileNetV2 without its top ----------
# weights=None builds the structure only; in Colab use weights="imagenet"
base = keras.applications.MobileNetV2(input_shape=(160, 160, 3),
                                      include_top=False, weights=None)
print(f"MobileNetV2 base: {len(base.layers)} layers, {base.count_params():,} weights")

inputs = keras.Input(shape=(160, 160, 3))
x = base(inputs, training=False)                    # keep BatchNorm in inference mode
x = layers.GlobalAveragePooling2D()(x)
outputs = layers.Dense(3, activation="softmax")(x)  # a new head for 3 classes
model = keras.Model(inputs, outputs)

def trainable():
    return sum(np.prod(w.shape) for w in model.trainable_weights)

base.trainable = False                              # stage 1: feature extraction
print(f"Frozen base:        {trainable():>9,} trainable weights (just the new head)")
base.trainable = True
for layer in base.layers[:-20]:                     # stage 2: fine-tune the top 20 layers
    layer.trainable = False
print(f"Top 20 unfrozen:    {trainable():>9,} trainable weights")

# ---------- 2. Train a small model to save and deploy ----------
X, y = load_digits(return_X_y=True)
X = (X / 16.0).astype("float32")
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
digits = keras.Sequential([keras.Input(shape=(64,)),
                           layers.Dense(64, activation="relu"),
                           layers.Dense(10, activation="softmax")], name="digits")
digits.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
digits.fit(X_train, y_train, epochs=30, verbose=0)
original = digits.predict(X_test[:3], verbose=0)

# ---------- 3. Save and reload the whole model ----------
digits.save("digits.keras")
reloaded = keras.models.load_model("digits.keras")
print("\nSaved .keras file:", os.path.getsize("digits.keras") // 1024, "KB")
print("Same predictions after reloading:",
      np.allclose(original, reloaded.predict(X_test[:3], verbose=0)))

# ---------- 4. Save only the weights ----------
digits.save_weights("digits.weights.h5")
copy = keras.models.clone_model(digits)             # same structure, new random weights
copy.load_weights("digits.weights.h5")
print("Weights loaded into a copy:", np.allclose(original, copy.predict(X_test[:3], verbose=0)))

# ---------- 5. Export for serving, then convert for phones (TensorFlow Lite) ----------
digits.export("digits_savedmodel", verbose=False)
print("Exported SavedModel folder:", sorted(os.listdir("digits_savedmodel")))

import tensorflow as tf
tflite = tf.lite.TFLiteConverter.from_saved_model("digits_savedmodel").convert()
print("TensorFlow Lite model:", len(tflite) // 1024, "KB")
interpreter = tf.lite.Interpreter(model_content=tflite)
interpreter.allocate_tensors()
inp, out = interpreter.get_input_details()[0], interpreter.get_output_details()[0]
interpreter.set_tensor(inp["index"], X_test[:1])
interpreter.invoke()
print("Phone-ready model predicts:", interpreter.get_tensor(out["index"]).argmax(),
      "| Keras model predicts:", original[0].argmax(), "| true digit:", y_test[0])
Output
MobileNetV2 base: 154 layers, 2,257,984 weights
Frozen base:            3,843 trainable weights (just the new head)
Top 20 unfrozen:    1,209,923 trainable weights
Saved .keras file: 76 KB
Same predictions after reloading: True
Weights loaded into a copy: True
Exported SavedModel folder: ['assets', 'fingerprint.pb', 'saved_model.pb', 'variables']
TensorFlow Lite model: 20 KB
Phone-ready model predicts: 6 | Keras model predicts: 6 | true digit: 6
▶ Open Google ColabPaste the code into a new Colab notebook and press Run. Keras is already installed.

What the output tells us

  • MobileNetV2 has 154 layers and about 2.26 million weights without its top. When frozen, only the new head's 3,843 weights train (1,280 features × 3 classes + 3). Unfreezing the top 20 layers raises that to about 1.2 million.
  • The .keras file is just 76 KB, and the reloaded model makes exactly the same predictions.
  • Weights only work too, once loaded into a model with the same structure.
  • export() created a SavedModel folder ready for TensorFlow Serving, which was then converted to a 20 KB TensorFlow Lite model small enough for any phone. It predicts the same digit (6) as the original Keras model.

If you run this yourself, TensorFlow may warn that tf.lite.Interpreter is moving to Google's LiteRT package (ai_edge_litert). The conversion still works; for new phone apps, follow the LiteRT documentation.

SummaryKey takeaways

  • Transfer learning reuses a model trained on huge datasets, needing far less data and time.
  • Load image models from Keras Applications with include_top=False and weights="imagenet", and use the model's own preprocess_input.
  • Freeze the base and train a new head first, then fine-tune the top layers with a tiny learning rate. Re-compile after changing trainable.
  • KerasHub gives you pretrained Transformer text models as ready-made presets.
  • Save everything with model.save("model.keras"); preprocessing layers are saved too.
  • Deploy with a .keras file, a SavedModel for serving, TensorFlow Lite for phones or TensorFlow.js for browsers.

Check your understanding

Your Keras roadmap

  1. 01
    Introduction to KerasWhat Keras is, setup and your first model
  2. 02
    Tensors and Data for KerasShapes, scaling and splitting data
  3. 03
    The Sequential API and Dense LayersStacking layers into a model
  4. 04
    Compiling ModelsLosses, optimisers and metrics
  5. 05
    Training and Evaluatingfit(), validation and callbacks
  6. 06
    The Functional APIMultiple inputs, outputs and branches
  7. 07
    RegularisationDropout, batch normalisation and early stopping
  8. 08
    Image Classification with KerasCNNs and data augmentation
  9. 09
    Text and Sequences with KerasEmbeddings, LSTMs and text classification
  10. 10
    Pretrained Models, Saving and DeploymentYou are here

Congratulations, you've completed the Keras track!

You can now prepare data, build, train, regularise and evaluate Keras models for tables, images and text, use pretrained models, and deploy your work. That's a genuinely practical deep learning skill set.