📚 Course contents
AI & Computer Vision, lesson 18 of 52
Train Your First Image Classifier: Ball or No Ball
Introduction
Time to train a real model. You'll make a pile of small picture crops, some with a ball and some without, build a small CNN in Keras, and teach it to tell them apart. Then you'll read its learning curves to see whether it truly learned or just memorised.
By the end of this lesson you'll be able to:
- Explain the difference between classification (one answer per picture) and detection (boxes, from lesson 23)
- Prepare ball and no-ball crops from labelled images, split by session
- Build a small CNN in Keras and say what each layer does
- Train it and read the training and validation curves
- Save the trained model to a file
- Say why the test folder stays untouched until lesson 19
Working principle
Classification versus detection
| Classification | Detection | |
|---|---|---|
| Question | "Is there a ball in this picture?" | "Where are the balls and players?" |
| Answer | One label (and a confidence) | A list of boxes, each with a label |
| Model | Today's CNN | YOLO (lesson 23 onwards) |
We start with classification because it's the simplest complete training loop. Detection builds on it.
Why crops?
A full football frame is mostly grass. A ball is a few pixels. A classifier shown the whole frame would mostly learn "grass". So we make crops: a small square window around each labelled ball (about three times the ball's size, so some context is included), and random crops with no ball in them. Both are resized to 64 by 64.
This uses the boxes you drew in lesson 12, and the splits from lesson 13. Crops from the training images go to crops/train, those from the validation images to crops/valid, those from the test images to crops/test. Never mix them, or the leakage problem from lesson 10 and lesson 13 comes back.
The model
| Layer | What it does |
|---|---|
Rescaling(1/255) | Pixels 0 to 255 become 0 to 1, which trains better |
Conv2D(16, 3) + MaxPooling2D | 16 learned 3 by 3 filters find edges and blobs, pooling shrinks the map |
Conv2D(32, 3) + MaxPooling2D | 32 filters find combinations (curves, patches) |
Conv2D(64, 3) + MaxPooling2D | 64 filters find bigger parts |
Flatten | Lays the feature maps out as one list |
Dense(64) | Combines everything |
Dense(1, sigmoid) | One number between 0 and 1: the probability of "ball" |
This is lesson 17's recipe, with lesson 16's loss and gradient descent running underneath (adam is a smarter version of gradient descent that adjusts the step size for each weight).
Reading the curves
| You see | It means |
|---|---|
| Training and validation both improve | Learning is working |
| Training keeps improving, validation stops or gets worse | Overfitting: memorising instead of learning (lesson 20) |
| Both stay poor | Underfitting: the model is too simple, the data is too hard, or the labels are bad |
| Validation is far better than training at first | Often just dropout or averaging effects. Watch the trend. |

