Transfer Learning with Pretrained Models

Deep Learning with Keras and TensorFlow 12 min min read Updated: Nov 16, 2026 Intermediate
Transfer Learning with Pretrained Models
Intermediate Topic 6 of 9

What is Transfer Learning?

Big models such as MobileNet, ResNet and EfficientNet were trained on ImageNet, a dataset with over a million images in 1000 classes. They already know how to see edges, textures, shapes and objects. In transfer learning we borrow that knowledge and only teach the model our own small task.

Simple example: A person who already knows how to ride a bicycle learns to ride a motorbike much faster than a person who has never balanced on two wheels. The old skill transfers.

Popular Pretrained Models

ModelSizeGood for
MobileNetV2Small, fastPhones, quick experiments
ResNet50MediumStrong general baseline
EfficientNetB0Small, accurateGood accuracy for the size
VGG16LargeSimple structure, learning purposes

Step 1: Load the Base Model

python import keras from keras import layers base_model = keras.applications.MobileNetV2( input_shape=(160, 160, 3), include_top=False, # remove the original 1000-class classifier weights="imagenet" # download pretrained weights ) base_model.trainable = False # freeze: do not change the learned knowledge

Step 2: Add Your Own Classifier

python inputs = keras.Input(shape=(160, 160, 3)) x = layers.RandomFlip("horizontal")(inputs) x = layers.RandomRotation(0.1)(x) x = keras.applications.mobilenet_v2.preprocess_input(x) # scales pixels to -1..1 x = base_model(x, training=False) x = layers.GlobalAveragePooling2D()(x) x = layers.Dropout(0.2)(x) outputs = layers.Dense(1, activation="sigmoid")(x) model = keras.Model(inputs, outputs) model.compile(optimizer=keras.optimizers.Adam(1e-3), loss="binary_crossentropy", metrics=["accuracy"]) model.summary()
Important: Each pretrained model expects a particular pixel scaling. Always use the matching preprocess_input function from the same model family.

Step 3: Train Only the New Layers

python history = model.fit(train_ds, validation_data=val_ds, epochs=5)
Result

With the cats vs dogs data, validation accuracy typically jumps to around 95 percent or more after only a few epochs.

Why Is It So Good?

  • The base model already recognizes fur, eyes, ears and shapes
  • Only a few thousand new weights are trained, so there is little overfitting
  • Training is quick, even on a CPU

When Does It Work Best?

Your dataStrategy
Small and similar to ImageNetFreeze the base, train a new head
Medium and somewhat differentTrain the head, then fine-tune a few top layers
Large and very differentFine-tune most layers or train from scratch
Next step: In the next lesson you will learn fine-tuning, which can add a few more points of accuracy.

Conclusion

Transfer learning is the most practical technique in computer vision. Use it as your first choice for any image project with limited data.

Quick Summary: Load a pretrained base with include_top=False, freeze it, add GlobalAveragePooling and a new Dense head, and train.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators