ImageDataGenerator for semantic segmentation
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Semantic segmentation needs every training image and its mask to stay perfectly aligned after augmentation. That requirement makes the task slightly different from ordinary image classification, where only the image is transformed and the label stays unchanged.
Why Segmentation Needs a Paired Generator
ImageDataGenerator was designed primarily for image classification, but it can still be used for segmentation if you drive the image and mask generators in lockstep. The main rule is simple: every random transform applied to the image must be applied to the mask with the same random seed and the same geometric parameters.
If the image rotates but the mask does not, the labels stop matching the pixels and training quickly becomes meaningless.
There is a second rule that matters just as much: masks are categorical data, not photographs. That means interpolation choices that look fine for images can corrupt the mask by inventing class values that never existed.
Basic Pattern With ImageDataGenerator
A practical legacy setup is to create two generators with identical augmentation settings and the same seed. One reads images, the other reads masks. Then you zip them together so that each batch contains aligned arrays.
This works because both flows consume random numbers in the same order. If you change one generator configuration and not the other, the synchronization breaks.
Handling Masks Correctly
The code above demonstrates the pairing pattern, but mask handling needs extra care.
For images, rescaling by dividing by 255 is normal. For masks, rescaling is usually wrong because class values such as 0, 1, and 2 must remain discrete labels.
Similarly, brightness shifts, channel shifts, and featurewise normalization should be applied to images only, never to masks. Masks should generally receive only geometric transforms such as flips, translations, or rotations.
When your masks are stored as color images, convert them into integer class maps before training. A segmentation network expects each pixel to represent a class index or a one-hot vector, not arbitrary RGB values.
That conversion is especially important when your loss function assumes categorical targets.
Limits of ImageDataGenerator
ImageDataGenerator can be useful for older projects, but it is not ideal for every segmentation pipeline. It does not give you fine-grained control over interpolation, paired random state, or complex augmentations such as elastic transforms. It is also part of an older preprocessing style compared with modern tf.data pipelines and preprocessing layers.
For new code, many teams prefer tf.data because it lets you load files, decode masks, apply exactly the same random transform to both tensors, and keep the whole pipeline explicit.
Here is a small example using TensorFlow ops directly:
The explicit version is longer, but it makes the synchronization rule impossible to forget.
Common Pitfalls
The most common mistake is augmenting images and masks independently. Even small differences in seed or generator order can scramble supervision.
Another frequent issue is normalizing masks like images. If you divide mask labels by 255, class identities are destroyed.
Interpolation is another hidden source of bugs. Rotating or resizing masks with smoothing can create fractional label values. For masks, use nearest-neighbor style behavior whenever possible.
Finally, avoid augmentations that are semantically invalid for the problem. Horizontal flips may help for road scenes, but not necessarily for medical images where left and right have clinical meaning.
Summary
- For segmentation, images and masks must receive the same geometric transforms in the same order.
- '
ImageDataGeneratorcan work for legacy code if paired flows share the same seed.' - Apply photometric changes to images, not to masks.
- Keep masks as discrete class labels and watch interpolation behavior carefully.
- Prefer
tf.dataor explicit TensorFlow augmentations for new pipelines that need more control.

