Returning 3 images from data generator
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
Returning three images from one generator usually means you are training a multi-input model. Common examples are triplet networks, anchor-positive-negative pipelines, or models that compare three related images in one training step. The generator must return tensors in the exact structure your model expects.
Match Generator Output to Model Input
The key rule is simple: if the model has three inputs, the generator must yield three image batches in the same order. In Keras, that usually means returning either a list, a tuple, or a dictionary keyed by input layer names.
For a triplet-style setup, the shape is often:
- anchor batch
- positive batch
- negative batch
- optional labels or dummy targets
If you get shape or unpacking errors, the problem is usually a mismatch between the generator's return value and the model definition.
Example with tf.keras.utils.Sequence
Sequence is a good fit because it is deterministic and works well with multiprocessing. This example returns three image tensors and a dummy target array.
This structure is directly consumable by a model with three image inputs.
Example Multi-Input Model
The model below accepts three images and embeds them through a shared encoder. The exact loss is not the point here; the important part is that the generator output structure matches the input structure.
If your generator returns (a, p, n), y, Keras will map those arrays to the three inputs in order.
Use Dictionaries When Order Is Risky
If the model has many inputs or the code is easy to misread, returning a dictionary is safer than relying on positional order.
Named inputs reduce bugs during refactors.
Common Pitfalls
- Returning three single images instead of three batches, which breaks batch semantics.
- Mixing the order of anchor, positive, and negative arrays relative to the model inputs.
- Forgetting that
Sequencemust return NumPy arrays or tensors with consistent shapes. - Using labels shaped incorrectly for the compiled loss.
- Debugging the model first when the real issue is generator structure.
Summary
- A generator can return three images by yielding three batched tensors per step.
- The return structure must match the model's input structure exactly.
- '
tf.keras.utils.Sequenceis a solid choice for multi-input image pipelines.' - Dictionaries keyed by input names are safer than positional tuples in larger models.
- Most errors come from shape mismatches and incorrect ordering, not from the idea of multi-image generation itself.
Related reading
- robust algorithm for surface reconstruction from 3D point cloud?
- Robust Algorithm to detect uneven illumination in images Detection Only Needed
- Robust Line Extraction from Image
- Rotated rectangle rasterisation algorithm
- Returning probabilities in a classification prediction in Keras?
- Returning probabilities in a classification prediction in Keras?
- Rounding colour values to the nearest of a small set of colours
- Save bitmap to location
.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.