Sub topic 12
Splitting Data Without Leakage
Assume you know basic Python, JSON, image dimensions, and the purpose of supervised model training. This chapter focuses on splitting an annotated computer-vision dataset into training, validation, and test sets without allowing related images to cross split boundaries.
What data leakage means
Data leakage occurs when information from the validation or test set influences training. The model then appears more accurate than it will be on new images.
In image datasets, leakage often happens because images are related even when their filenames differ:
- Video frames from the same recording
- Bursts from the same camera event
- Near-duplicate images
- Multiple crops of one original image
- Augmented copies
- Images from the same patient, vehicle, site, or product
If one frame from a video is in training and the next frame is in testing, the model may recognise the background and object pose. The test score measures memorisation of the recording rather than generalisation.
A group is a set of images that must stay in one split. A group might be a video, an inspection session, a patient, or an original image and all its derivatives.
Choose the split unit
First identify the unit that represents an independent real-world example. Split by that unit, not automatically by individual image.
| Dataset situation | Keep together |
|---|---|
| Frames extracted from videos | One complete video |
| Medical images | One patient |
| Factory inspection photos | One production batch or session |
| Images with augmentations | The original image and its copies |
| Independent photographs | Each image, after duplicate checking |
A common starting ratio is 70% training, 15% validation, and 15% test. The exact ratio matters less than having enough examples of every important class and condition in each split.
If deployment will use future images, a time-based split may be more realistic. Train on earlier dates and test on later dates. This measures performance under distribution change, but it can produce an imbalanced test set. Record the reason for the choice.
A reproducible group split
Store group information as metadata before splitting. The following example assigns groups randomly using a fixed seed. Because the unit is a group, related images cannot be separated.
import random
from collections import defaultdict
records = [
{"file": "video_a_001.jpg", "group": "video_a"},
{"file": "video_a_002.jpg", "group": "video_a"},
{"file": "video_b_001.jpg", "group": "video_b"},
{"file": "video_c_001.jpg", "group": "video_c"},
{"file": "video_d_001.jpg", "group": "video_d"},
]
group_to_files = defaultdict(list)
for record in records:
group_to_files[record["group"]].append(record["file"])
groups = list(group_to_files)
random.Random(7).shuffle(groups)
cut_train = round(len(groups) * 0.70)
cut_valid = round(len(groups) * 0.85)
group_splits = {
"train": groups[:cut_train],
"valid": groups[cut_train:cut_valid],
"test": groups[cut_valid:],
}
file_splits = {
split: [file for group in selected for file in group_to_files[group]]
for split, selected in group_splits.items()
}
for split, files in file_splits.items():
print(split, files)The fixed seed makes the result repeatable. Save the resulting file lists or group assignments in version control. Never regenerate them silently between experiments.
Check for leakage and balance
After splitting, verify that every group appears in only one split. Also compare class counts, image sources, object sizes, lighting conditions, and difficult cases across splits.
Exact duplicate detection can use a cryptographic hash. Near-duplicates may require perceptual hashes or image embeddings. Review suspicious matches manually because two visually similar images may still represent valid independent examples.
A split can be leakage-free but still misleading. For example, a test set containing only easy, well-lit images will overstate performance. Use the test set once for final reporting. Use the validation set for model choices, thresholds, and error analysis.
Exercise
Ten images come from one video, and five images come from another. Should you randomly distribute all 15 images across train, validation, and test?
Show answer
No. Treat each video as a group and place each complete video in one split. Otherwise, frames from the same recording can leak visual information across the boundaries.