Chapter 20 · Hands-on: more projects
Teach a model to recognise your photos
With transfer learning, a small set of your own labelled photos is enough to train a useful classifier.
Chapter 20 · Hands-on: more projects
With transfer learning, a small set of your own labelled photos is enough to train a useful classifier.
Your photos sit in one folder per label…
You will not train a whole model from nothing. You will borrow one that already understands pictures in general, lock it, and train only a new last layer that knows your labels. That is transfer learning.
As in the transfer learning lesson, a model trained on millions of pictures has already learned edges, shapes and textures. Teaching it your own categories then needs far fewer photos and far less computer time than starting over.
You need Python and a virtual environment (see Set up Python and a virtual environment). This works on Windows, macOS and Linux desktops. A normal laptop processor is enough for a small photo set; a graphics card makes big sets faster but is not required.
PyTorch trains and runs models. torchvision adds image tools and pretrained models. Pillow opens photo files. The install is large, so it can take several minutes.
mkdir photo-classifier
cd photo-classifier
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install torch torchvision pillow Check: The prompt starts with (.venv) and the install finishes without red errors.
Pick two or three things you can tell apart and photograph easily: for example your two plants, three kinds of fruit, or tidy and messy desks. Aim for a few dozen photos of each, taken in different light, angles and backgrounds. Put them in one folder per label, inside a folder called photos. The folder name is the label. Use the checker below to plan your counts.
Plain .jpg or .png files work best. Keep names and folder labels free of spaces.
photo-classifier/
photos/
plant_a/ (many .jpg files)
plant_b/ (many .jpg files)Check: Each label folder holds its photos and nothing else.
Create train.py in photo-classifier (next to the photos folder) and paste this in. In short, it:
import random
import time
import torch
from torch import nn
from torch.utils.data import DataLoader, Subset
from torchvision import datasets, models
DATA = "photos" # one sub-folder per label, e.g. photos/cats and photos/dogs
EPOCHS = 5 # how many times the model sees every training photo
BATCH = 16
weights = models.ResNet18_Weights.DEFAULT
prep = weights.transforms() # the resizing and colour scaling this model expects
everything = datasets.ImageFolder(DATA, transform=prep)
labels = everything.classes
print("Labels found:", labels, "| photos:", len(everything))
order = list(range(len(everything)))
random.Random(0).shuffle(order)
cut = int(len(order) * 0.8)
train_loader = DataLoader(Subset(everything, order[:cut]), batch_size=BATCH, shuffle=True)
check_loader = DataLoader(Subset(everything, order[cut:]), batch_size=BATCH)
model = models.resnet18(weights=weights)
for p in model.parameters():
p.requires_grad = False # keep what it already knows
model.fc = nn.Linear(model.fc.in_features, len(labels)) # new last layer: ours to train
optimiser = torch.optim.Adam(model.fc.parameters(), lr=0.001)
loss_fn = nn.CrossEntropyLoss()
def accuracy(loader):
model.eval()
right = total = 0
with torch.no_grad():
for photos, answers in loader:
right += (model(photos).argmax(dim=1) == answers).sum().item()
total += len(answers)
return right / total
for epoch in range(1, EPOCHS + 1):
model.train()
start = time.time()
for photos, answers in train_loader:
optimiser.zero_grad()
loss = loss_fn(model(photos), answers)
loss.backward()
optimiser.step()
print(f"Round {epoch}/{EPOCHS}: last-batch loss {loss.item():.3f}, "
f"right on unseen photos {accuracy(check_loader):.0%} ({time.time() - start:.0f}s)")
torch.save({"weights": model.state_dict(), "labels": labels}, "photo-model.pt")
print("Saved photo-model.pt") Check: The file is saved as train.py.
Run the script. Each “round” shows the model every training photo once.
python train.py Check: You see Labels found: [...], then five lines like Round 3/5: … right on unseen photos 83%, then Saved photo-model.pt. The percentage usually climbs over the rounds.
The “unseen photos” score is the one that matters: it is like the test set in the train and test lesson. With a small set it is only a rough guide, because one photo can move it by several points. A high score on training photos but a low one on unseen photos means the model memorised, which is overfitting. Photos that look alike (same room, same hour) also make the score look better than real life.
Check: You can explain why unseen photos are used for the score.
Take a fresh photo that is not in your folders and put it next to predict.py. This second script rebuilds the model, loads your saved last layer and prints a chance for each label.
Try something that is none of your labels, like a shoe. The model has to pick one of your labels anyway, and may sound very sure. It only knows what you taught it.
import sys
import torch
from PIL import Image
from torch import nn
from torchvision import models
saved = torch.load("photo-model.pt")
labels = saved["labels"]
weights = models.ResNet18_Weights.DEFAULT
model = models.resnet18(weights=None)
model.fc = nn.Linear(model.fc.in_features, len(labels))
model.load_state_dict(saved["weights"])
model.eval()
photo = weights.transforms()(Image.open(sys.argv[1]).convert("RGB")).unsqueeze(0)
with torch.no_grad():
chances = torch.softmax(model(photo), dim=1)[0]
for chance, label in sorted(zip(chances.tolist(), labels), reverse=True):
print(f"{label}: {chance:.0%}")python predict.py new-photo.jpgCheck: You see each label with a percentage, highest first, adding up to 100%.
Better results usually come from better data, not cleverer code. Add more photos where it makes mistakes, balance the labels, vary light and background, and remove wrong or blurry photos. You can also raise EPOCHS, but watch the unseen-photo score: if it stops improving, more rounds will not help.
Check: You retrained after changing the data and compared the unseen-photo score.
Type your labels and photo counts. The checker draws the folders, shows the train and test split and gives general advice. It does not look at any photo.
photos/
cats/ (40 photos)
dogs/ (35 photos)The script trains on about 60 photos and keeps about 15 aside to test on.
General guidance only. Good photos, varied and honest labels matter more than any rule of thumb.
What do you see?
Why does the script lock the pretrained layers?
Which score tells you most about how it will do on new photos?
You show it a shoe, which is none of your labels. What will it do?