📚 Course contents
AI & Computer Vision, lesson 21 of 52
Transfer Learning: Standing on Giants' Shoulders
Introduction
Big companies train networks on millions of photos for weeks. You don't have that, and you don't need it. With transfer learning you borrow one of those networks, keep everything it already knows about edges, shapes and textures, and teach it only the last step: your ball on your pitch. It needs fewer pictures and less time, and usually wins.
By the end of this lesson you'll be able to:
- Explain transfer learning: reusing what a network learned on a big dataset for a new, smaller job
- Say what freezing layers means, and why we do it
- Build a model with a frozen MobileNetV2 base and a small new head
- Train the head, then fine-tune the top of the base with a very small learning rate
- Compare it with the lesson 18 model on accuracy, training time and the amount of data needed
- Name the trade-offs: size, speed and fit to your task
Working principle
Why it works
From lesson 17: the first layers of a CNN learn simple things (edges, corners, colour blobs) that are useful for any picture. Only the last layers are specific to the original task (dogs, cars, cups).
So we keep the early and middle layers as they are, and replace the last layers with our own.
The recipe
| Step | What happens | Learning rate |
|---|---|---|
| 1. Load the base | MobileNetV2, ImageNet weights, no top layer | |
| 2. Freeze it | Its weights do not change | |
| 3. Add a head | Pooling, dropout and one sigmoid neuron | |
| 4. Train the head | Only the new layer learns, which is quick | Normal (1e-3) |
| 5. Unfreeze the top | Let the last ~30 layers of the base adjust a little | Tiny (1e-5) |
| 6. Train again | Gentle fine-tuning |
The tiny learning rate matters: large steps would wreck what the base already knows.
What transfer learning buys you
| Small CNN from scratch (lesson 18) | Transfer learning (lesson 21) | |
|---|---|---|
| Data needed | Hundreds to thousands | Often a few hundred |
| Training time | Short for small images | Short too, for the head |
| Accuracy on small data | Limited | Usually much better |
| Model size | Tiny | A few megabytes (MobileNetV2 is built to be small) |
| Risk | Overfitting | Less, but possible |
Choosing a base model
| Base | Why you'd choose it |
|---|---|
| MobileNetV2 | Small and fast: good for laptops and phones, and for the browser |
| EfficientNet (B0 and up) | Better accuracy for its size, a little more to run |
| ResNet, others | Larger and slower: more power than we need for a ball |
All are in keras.applications. Check the Keras documentation for the exact names and input sizes.
Limits
- The base was trained on everyday photos. A very different domain (X-rays, thermal images) transfers less.
- Input size matters: with ImageNet weights MobileNetV2 expects 96, 128, 160, 192 or 224 pixels. Our 64 by 64 crops are enlarged to 96.
- Bias comes along too. The base inherits whatever was in its data (lesson 43).

