📚 Course contents
AI & Computer Vision, lesson 29 of 52
Clean the Field Data & Retrain
Introduction
You're back from the field with hundreds of frames. Not all of them deserve a place in your dataset. Today you'll clean the catch (blurry frames, near-copies, broken labels), split it fairly, retrain your detector on real field data, and compare it with your classroom model on the same test set, then explain the difference honestly.
By the end of this lesson you'll be able to:
- Clean a raw field dataset: blurry frames, near-duplicates, label errors
- Explain why we move rejected files aside instead of deleting them
- Check class balance after cleaning and decide what to do about it
- Split field data by session (lesson 13) so the test set is honest
- Retrain the detector on the field data and compare it fairly with the classroom model
- Explain the gap between classroom and field performance with evidence
Working principle
The cleaning steps, in order
| Step | Checks | Tool | Action |
|---|---|---|---|
| 1. Back up | Is the raw data copied elsewhere? | You | Do this first |
| 2. Unreadable and blurry | Variance of the Laplacian below a threshold (lesson 10) | clean_dataset.py | Move to rejected/ |
| 3. Near-duplicates | Almost identical images within one session | Perceptual hash | Keep the sharpest, move the rest |
| 4. Label problems | Missing files, unknown classes, boxes outside the image, tiny boxes | clean_dataset.py | Move for relabelling |
| 5. Class balance | Does one class have far fewer boxes? | Report | Collect or label more of it (lesson 20) |
| 6. Split by session | Whole sessions in train, valid or test (lesson 13) | Split script | No session in two sets |
Why move, not delete
A threshold that is a bit too strict can throw away good data. By moving rejects into a rejected/ folder with a reason in a report, you can check, undo, and explain what you did.
Near-duplicate detection with a perceptual hash
A dHash shrinks an image to 9 by 8 grey pixels and records, for each pixel, whether it is brighter than its right neighbour. That gives 64 yes/no values describing the image's overall shape. Two nearly identical frames give almost the same 64 values: the number of differing values is the Hamming distance. At 4 or less we call it a near-duplicate. We only compare frames from the same session, because frames from different positions are not duplicates even if they look alike.
A fair comparison
It is tempting to compare "mAP on the classroom validation set" with "mAP on the field validation set". But those are different exams. To compare two models, score both on the same held-out test set, ideally made of field sessions neither model has seen. Otherwise you may be measuring a harder exam, not a worse model.
Why field performance usually drops
| Cause | Looks like |
|---|---|
| Distribution shift | Classroom images do not look like the pitch |
| Motion blur and distance | Ball is a few blurry pixels |
| Lighting | Sun, shadows, glare |
| Label noise | Rushed labels on hard frames |
| Small test set | Large swings from a few frames |
| Class imbalance | Too few balls |

