Keras ImageDataGenerator for multiple inputs and image based target output
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
ImageDataGenerator works well for simple single-input image classification, but it becomes awkward when a model has multiple inputs and an image-shaped target such as a segmentation mask. The practical solution is usually a custom generator or Sequence that coordinates augmentation across all inputs and targets, rather than trying to force one plain generator call to handle everything automatically.
Why the Simple Pattern Stops Working
The usual single-input pattern looks like:
That assumes:
- one image input
- one simple label target
- augmentation applied only to the input image
For multi-input or image-to-image tasks, you may need:
- two input images
- an auxiliary numeric or categorical input
- a target image that must stay spatially aligned with the input
That alignment requirement is the main reason a custom wrapper is needed.
Core Rule for Image Targets
If the target is an image mask or another spatial output, geometric augmentation must be synchronized between input and target.
For example, if the input image is rotated or flipped, the target mask must receive the exact same transform. Otherwise, the training pair becomes invalid.
That is why using one generator for the image and a separate unsynchronized generator for the mask is wrong.
A Practical Sequence Pattern
Here is a simple custom Sequence for two image inputs and one mask target.
The shared seed is the important piece. It keeps geometric transforms aligned across the inputs and masks.
Example Model with Multiple Inputs
This model takes two images and predicts an image-like output.
Then train with the custom sequence:
Important Mask Handling Note
Masks are not ordinary images. You usually do not want color jitter, brightness changes, or normalization intended for RGB inputs applied to the target mask. For masks, keep transformations limited to spatial transforms that preserve label meaning.
That is why the image generator and mask generator are not identical even when they share the same seed.
When tf.data Is the Better Choice
For complex pipelines, tf.data is often easier to scale and reason about than ImageDataGenerator, especially for:
- large datasets
- custom decoding
- mixed image and tabular inputs
- deterministic augmentation control
Still, if your codebase already uses ImageDataGenerator, wrapping it in a Sequence is a reasonable bridge solution.
Common Pitfalls
The most common mistake is augmenting input images and image targets independently, which destroys label alignment. Another is assuming ImageDataGenerator.flow can natively express any multi-input structure without a wrapper. Teams also often apply image normalization and color augmentation to segmentation masks, which corrupts target values. Finally, for complex multimodal pipelines, insisting on ImageDataGenerator alone can create brittle code where tf.data would be clearer.
Summary
- Plain
ImageDataGenerator.flowis built for simpler single-input cases. - For multiple inputs and image targets, use a custom generator or
Sequence. - Keep geometric augmentation synchronized across inputs and target images.
- Do not apply ordinary image color transforms blindly to masks.
- Consider
tf.datawhen the pipeline becomes too complex forImageDataGenerator.
Related reading
- Keras images with no subfolders
- keras implementation of Levenberg-Marquardt optimization algorithm as a custom optimizer
- Keras inconsistent prediction time
- Keras initialize large embeddings layer with pretrained embeddings
- Keras load_model with custom objects doesn't work properly
- keras loss function for 360 degree prediction
- Keras input_shape for conv2d and manually loaded images
- Keras Lambda layer has no output tensor shape, error when compiling model
.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.