Unlocking Transfer Learning in Computer Vision
You don't need a million images. Reuse a pre-trained model and teach it your domain with a handful of samples.
In this guide
Training a vision model from scratch needs data you probably don't have. Transfer learning sidesteps that: take a model already trained on millions of images, keep its learned features, and only retrain the final layers on your data.
Why it works
The early layers of a convnet learn edges, textures and shapes — knowledge that transfers across domains. The last layers learn the specific classes. So we freeze the early layers and retrain only the head.
Load a pre-trained base
import tensorflow as tf
base = tf.keras.applications.MobileNetV2(
input_shape=(224, 224, 3),
include_top=False, # drop the original classifier head
weights="imagenet", # pre-trained on 1.4M images
)
base.trainable = False # freeze the learned featuresAdd a new head
from tensorflow.keras import layers, models
model = models.Sequential([
base,
layers.GlobalAveragePooling2D(),
layers.Dropout(0.2),
layers.Dense(3, activation="softmax"), # 3 of YOUR classes
])
model.compile(
optimizer=tf.keras.optimizers.Adam(1e-3),
loss="sparse_categorical_crossentropy",
metrics=["accuracy"],
)Train on a small dataset
train_ds = tf.keras.utils.image_dataset_from_directory(
"data/train", image_size=(224, 224), batch_size=32)
model.fit(train_ds, validation_data=val_ds, epochs=10)With a few hundred images per class you can hit 90%+ accuracy on many real problems — because the hard part (seeing) is already done.
Fine-tune the last layers
Once the new head converges, you can unfreeze the top of the base and retrain at a tiny learning rate to squeeze out extra accuracy.
base.trainable = True
# keep early layers frozen; only retrain the upper block
for layer in base.layers[:100]:
layer.trainable = False
model.compile(optimizer=tf.keras.optimizers.Adam(1e-5),
loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(train_ds, validation_data=val_ds, epochs=5)Transfer learning is the workhorse behind most practical computer vision today. Master it and a small dataset stops being a blocker.
More from the lab
Rust for Python Developers: A Practical Introduction
Learn Rust through the lens of Python you already know — ownership, types, and the real differences.
FrontendWhy TypeScript is a Necessary Pain (and How to Start)
TypeScript feels like friction at first — until it catches a bug that would have shipped.
PythonWeb Scraping with Python: requests + BeautifulSoup
Fetch pages, parse HTML and extract clean data with requests and BeautifulSoup — politely.
Want us to build this for you?
We turn tutorials like these into production systems every day. Tell us what you need.
Start a project →