Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Deep Learning & Neural Networks — Alpha School

Lesson 12: Transfer Learning and Pretrained Models

Training a large CNN from scratch needs a lot of labelled images and a lot of computing time — two things a school project rarely has. Transfer learning solves this by reusing a network that someone else has already trained on a large dataset. The early layers of that network have already learned to detect edges, textures and shapes, and those features are useful for almost any image task. You keep them, and only train the final layers for your own problem.

The usual recipe is simple. Take a pretrained network, remove its final classification layer, and add a new one with the right number of outputs for your classes. First, freeze the early layers so their weights do not change, and train only the new layer on your data. This is fast, because few weights are moving, and it works well even with a few hundred images per class. If you have more data and time, you can then unfreeze the last few layers and train them gently, with a small learning rate, so the network adapts its deeper features to your task without forgetting what it learned.

Why It Works So Well

Transfer learning works because visual features are largely shared. The edges and textures that mark a cat in one dataset also mark a leaf, a car, or a cell in another. Only the final combinations — which features mean which class — change from task to task. That is why freezing the early layers is safe: they already know the general visual world, and your small dataset only needs to teach the last step.

There is a catch. If your images are very different from the ones the pretrained network saw — for example, X-ray scans instead of ordinary photographs — the transferred features help less, and you may need more of your own data and more unfrozen layers. Transfer learning is a head start, not a guarantee: always compare against a simple baseline trained on your data alone, and keep a validation set to judge honestly which approach wins.

A Worked Example

Imagine a worked example: a class wants to sort photos of leaves into five tree species, with only 150 photos per species. Training a CNN from scratch on 750 images would overfit badly. Instead, the class takes a pretrained network, replaces its final layer with one that has five outputs, freezes everything else, and trains the new layer for a few epochs. Validation accuracy reaches a respectable level quickly. With time left, they unfreeze the last block of layers, retrain with a small learning rate, and gain a little more. The project succeeds not because the class had more data, but because it reused learning that already existed.

That reuse is one of the most practical ideas in modern deep learning. The big models are trained once, at great expense, and then adapted cheaply to thousands of smaller tasks. Knowing how to adapt one well — freeze, train the head, then fine-tune gently — is a skill you will use again and again.

When Transfer Learning Fails

Transfer learning can fail in instructive ways. The most common is a mismatch in the input: the pretrained network expects images of a certain size and colour format, and feeding it something different silently degrades its features. Always check that your images are resized and normalised the way the original network expected — the documentation of the model will say how. A second failure is a classification head trained too aggressively: a large learning rate on the new layer can produce wild updates that swamp the useful features beneath it, so the head should be trained gently at first, even though it is new.

A third failure is subtler: the pretrained features are good, but your classes are distinguished by details the network was never trained to notice. A network trained on everyday photographs may not have features for the fine differences between two similar bird species, or between healthy and diseased tissue in a scan. In that case the transferred features help only a little, and the honest response is to unfreeze more layers, gather more data, or accept that this task needs a model trained for it. Recognising when a head start is not enough is part of using the technique well.

Watch & Learn

One video to watch for this unit, from a trusted educator.

  • But what is a convolution? — 3Blue1Brown
    Convolutions from probability to image processing, with the sliding-window idea this unit is built on.

Key Terms

Transfer learning
Reusing a network trained on one task as the starting point for a new task.
Pretrained model
A network already trained on a large dataset, available for others to adapt.
Fine-tuning
Training a pretrained network's later layers gently on new data, with a small learning rate.
Freezing
Keeping some layers' weights fixed during training so they do not change.
Feature extractor
The early layers of a network, reused as-is to turn images into useful features.
Classification head
The final layers of a network, replaced and retrained for a new set of classes.

Discussion Questions

  1. Why are the early layers of a pretrained image network safe to reuse for a completely different set of photos?
  2. When would transfer learning help less — and what would you do instead?
  3. Is it fair to say the class in the worked example 'cheated' by reusing a pretrained model? Why or why not?

Practice Questions

  1. What is transfer learning in one sentence?
  2. What does freezing a layer mean?
  3. In the leaf example, what is trained first, and what is frozen?
  4. Why should fine-tuning use a small learning rate?
  5. Why might transfer learning help less for X-ray scans than for ordinary photos?
Show answers
  1. Reusing a network already trained on a large dataset as the starting point for a new task. You adapt it instead of training from scratch.
  2. Keeping its weights fixed during training so they do not change. Frozen layers keep what they already learned.
  3. The new five-output classification layer is trained first, while all the earlier layers are frozen. Only later are the last layers unfrozen for gentle fine-tuning.
  4. So the pretrained weights change only slightly and do not forget what they learned on the large dataset. A large rate could destroy the useful features.
  5. Because the pretrained network learned from ordinary photographs, so its features match that world better than the world of medical scans. Very different images need more of your own data and more unfrozen layers.
Keep reading to complete this lesson