How to apply data augmentation in TensorFlow 2.0 after tfds.load
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
After tfds.load, the right place for data augmentation is the tf.data pipeline or a dedicated augmentation block used only for training. The main goal is to keep preprocessing reproducible and fast while making sure random transformations affect training data only, not validation or test data.
Load the Dataset With Clear Splits
Start by separating training and evaluation data explicitly. That prevents accidental leakage when the pipeline grows later.
Using named or percentage-based splits is clearer than loading one dataset and then trying to remember later which branch should receive augmentation.
Build a Reusable Augmentation Block
In current TensorFlow, Keras preprocessing layers are a clean way to express stochastic augmentation.
Now define separate preprocessing functions for train and evaluation:
The explicit training=True call matters because it makes the stochastic behavior intentional instead of relying on an ambient training context.
Compose the tf.data Pipeline Deliberately
The pipeline order affects both correctness and performance. A strong default looks like this:
That keeps augmentation in the training path only and overlaps preprocessing with model execution through prefetching.
One subtle but important detail is cache placement. If you cache after random augmentation, you freeze one augmented version of each example. If you cache before augmentation, you keep randomness across epochs while still saving upstream decode or read costs.
Train With the Augmented Pipeline
Once the datasets produce ready-to-train tensors, the model code stays simple.
This separation is valuable because augmentation becomes a data concern rather than a hidden side effect in the training loop.
Reproducibility and Sanity Checks
Random augmentation helps generalization, but it also makes debugging harder. Set a seed when reproducibility matters:
Then run a visual sanity check before long training jobs. Pull one batch and inspect whether the transformed images still preserve the class semantics.
If the model suddenly stops converging after an augmentation change, the issue is often not the optimizer. It is usually that the new random transforms are too aggressive or were accidentally applied to validation data.
Common Pitfalls
The most common mistake is applying random augmentation to validation or test datasets. That makes evaluation noisy and hard to compare.
Another common issue is putting augmentation in the wrong place in the pipeline, especially caching after random transforms and then wondering why every epoch sees the same augmented sample. Developers also often forget to inspect transformed images visually. An augmentation policy can look reasonable in code while destroying the signal in practice.
Summary
- Apply augmentation after
tfds.loadinside a dedicated training preprocessing pipeline. - Keep validation and test preprocessing deterministic.
- Use Keras preprocessing layers or
tf.imagetransforms for stochastic train-only augmentation. - Compose
shuffle,map,batch, andprefetchintentionally. - Check cache placement and visualize a few augmented samples before committing to long training runs.
Related reading
- How to apply gradient clipping in TensorFlow?
- How to apply gradient clipping in TensorFlow?
- How to apply normalization to images in testing phase when using keras ImageDataGenerator?
- How to approximate the determinant with keras
- How to apply Drop Out in Tensorflow to improve the accuracy of neural network?
- how to apply imgaug augmentation to tf.dataDataset in Tensorflow 2.0
- How to apply kernel regularization in a custom layer in Keras/TensorFlow?
- How to apply LabelEncoder for a specific column in Pandas dataframe
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.