CNN image classification with a clean data split
In this article (5 sections)
An image classifier can memorize shared sources, patients, products or video frames across train and test. Randomly splitting image files is unsafe when several files derive from one underlying subject. Establish the grouping unit before augmentation and model fitting.
A fully inspectable convolutional run
The local lab generates 300 original 8×8 images containing a noisy vertical or horizontal stripe. Every image has a unique source group. It assigns 180 groups to training, 60 to validation and 60 to test, with zero train-validation and train-test group overlap.
Its 45-parameter NumPy model has four learned 3×3 convolution filters, ReLU, global average pooling and a sigmoid output. All weights, activations and gradients are visible in one file.
from deep_learning_cases import cnn_split_case
result = cnn_split_case()
assert result["group_overlaps"]["train_validation"] == 0
assert result["group_overlaps"]["train_test"] == 0
assert result["parameters"] == 45
print(result["training_loss_start"], result["training_loss_end"])Training loss falls from 0.680 to 0.138. Validation and test accuracy are both 100% on this simple authored task. That result establishes that the tiny workflow can learn two strong geometric patterns; it is not evidence for medical, industrial or natural-image classification.
Split before deriving images
Assign a patient, scene, specimen or source recording to one partition, then create crops, frames and augmentations inside that partition. Otherwise two nearly identical views can cross the boundary. Record a group manifest and assert disjointness in QA.
Fit normalization statistics on training. Determine class mappings and filter corrupt files without consulting test outcomes. Keep the test directory read-only while comparing architecture and augmentation choices.
Make the task harder honestly
Perfect performance on a toy task should trigger scope expansion, not promotional claims. Add varied positions, illumination, backgrounds and nuisance artifacts. Create deployment-relevant slices. Compare a linear pixel baseline and a non-neural image-feature baseline. Verify that the CNN learns the intended object rather than a border or watermark.
For real images, document licenses, consent and sensitive attributes. Duplicate checks and group separation address evaluation integrity; they do not resolve whether the dataset should be used.
The Data Science course connects CNN syntax with split design, failure analysis and reproducible artifacts.
Exercise
Generate five augmented views per source after the group split. Assert that every view inherits its source partition. Add an unseen stripe thickness and report errors by thickness and noise level.
Continue learning
This article is part of the Deep learning and computer vision sequence. Use the neighbouring tasks when you need the prerequisite or the next application.
- Review the prerequisite or neighbouring task in Early stopping with a reproducible validation policy.
- Continue with Image augmentation that preserves the label meaning.
References: scikit-learn guidance on grouped cross-validation and PyTorch Conv2d API.
Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.
This article is part of our Data Science programme — 6 months. From data foundations to machine learning, deep learning and deployment.
Explore Data Science