Image classification is the "hello world" of computer vision, but it does not have to be a toy. With transfer learning, a few hundred photos per class and a free GPU, you can build a classifier for plant diseases, waste sorting or road damage that performs well enough to demo with real photos.
This guide builds one in PyTorch, step by step. You will organise your images in folders, load a pretrained ResNet18 from torchvision, train a new output layer, fine-tune deeper layers, and evaluate with accuracy and a confusion matrix. The code is around 120 lines in total and works on any image dataset arranged as one folder per class.
You do not need to design a CNN from scratch. Knowing why you did not, and being able to explain what the pretrained layers already know, is exactly what a viva panel will test.
What is an image classification project?
An image classification project builds a model that looks at an image and assigns it one label from a fixed set of classes. Examples: "tomato early blight" versus "tomato healthy", or "plastic" versus "paper" versus "metal". It is one of the core tasks of computer vision.
Most modern image classifiers are convolutional neural networks (CNNs) or vision transformers. Training one from scratch needs a very large labelled dataset. Transfer learning avoids that: you start from a network already trained on ImageNet (over a million images in 1,000 classes), keep the layers that detect edges, textures and shapes, and retrain only the final layer for your classes.
If neural networks are new to you, read what a neural network is first. The rest of this guide assumes you know what layers and weights are.
What do you need for an image classification project in Python?
You need Python basics, an image dataset arranged as one folder per class, and ideally a GPU. Google Colab's free tier offers GPU access, though Google states that free resources are not guaranteed and usage limits fluctuate.
| Part | What we use | Why |
|---|---|---|
| Framework | PyTorch and torchvision | Pretrained models and image tools in one place |
| Model | ResNet18 with ResNet18_Weights.DEFAULT | Small, fast, strong baseline |
| Data loading | torchvision.datasets.ImageFolder | Reads one folder per class, no CSV needed |
| Metrics | scikit-learn classification_report and confusion matrix | Per-class precision, recall and F1 |
| Compute | Google Colab GPU or your own GPU | A CPU works for small datasets, just slower |
On Colab: open a new notebook, go to the Runtime menu, choose Change runtime type, and select a GPU. Upload your dataset as a zip or mount Google Drive.
How does transfer learning for image classification work?
Transfer learning reuses a pretrained network and trains it in two phases:
- Prepare data: resize images to 224 by 224 pixels, normalise colours the way ImageNet models expect, and add random flips and crops to the training set (data augmentation).
- Phase 1, feature extraction: freeze every pretrained layer and train only a new final layer with one output per class. Fast, and hard to overfit.
- Phase 2, fine-tuning: unfreeze the last block of layers and train it with a small learning rate, so the network adapts its high-level features to your images.
- Evaluate: accuracy on a validation split the model never trained on, plus a confusion matrix that shows which classes get mixed up.
The validation split is the honest judge. Training accuracy near 100 percent with much lower validation accuracy is overfitting, and augmentation plus freezing layers are your main defences against it.