Image Generator for 3D volumes in keras with data augmentation
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
Keras does not provide a built-in 3D equivalent of the classic 2D ImageDataGenerator, so training on 3D volumes usually means writing a custom generator or Sequence. The main idea is straightforward: load a batch of 3D arrays, apply augmentation consistently across the whole volume, and return tensors shaped for a 3D CNN.
Why 3D Needs a Custom Generator
A 3D volume is not just a stack of unrelated 2D images. If you rotate, flip, or crop slices independently, you destroy the spatial structure of the volume.
That means good 3D augmentation should preserve geometric consistency across all slices in the sample.
Typical 3D shapes look like:
- '
(depth, height, width, channels)' - or sometimes
(height, width, depth, channels)depending on the pipeline
The generator must keep the shape convention consistent with the model.
A Simple Sequence for 3D Volumes
This example assumes each sample is stored as a NumPy file and that each volume already has the right size.
Important Rule for Augmentation
The transform must be applied to the whole volume, not to each slice independently. For example, flipping a volume across one axis is fine because it preserves 3D structure. Rotating each slice separately with unrelated angles is not.
Good augmentation ideas for 3D volumes include:
- flips along spatial axes
- small consistent rotations
- random cropping
- intensity shifts
- noise injection
The right choice depends heavily on the domain. Medical imaging, microscopy, and industrial scans all have different validity rules.
Example 3D CNN Model
And training:
Memory Considerations
3D volumes are much heavier than 2D images, so batching needs more care. A batch size of 32 that is trivial for 2D images may be impossible for large 3D inputs.
That is why practical 3D pipelines often use:
- smaller batches
- preprocessed
.npyvolumes - cached datasets
- mixed precision when appropriate
The generator should be written with memory pressure in mind from the start.
Common Pitfalls
One common mistake is trying to force 3D data through 2D image augmentation utilities. That usually breaks spatial consistency.
Another issue is applying different random transforms to different slices of the same volume.
A third pitfall is ignoring memory limits and discovering too late that volume loading plus augmentation makes the training job unstable.
Summary
- Keras 3D training usually needs a custom generator or
Sequence. - Apply augmentation consistently to the full volume, not slice by slice.
- Keep tensor shape conventions aligned with the model.
- Expect smaller batch sizes and higher memory usage than in 2D pipelines.
- Choose augmentation types that are valid for the specific 3D domain.
Related reading
- Image recognition using TensorFlow
- Image retraining in tensorflow, changing the simple softmax layer to multilayer CNN
- Implement a N-aryTreeLSTM version of the TreeLSTM in TensorFlow Fold
- Implement custom loss function in Tensorflow 2.0
- Image Segmentation using Mean Shift explained
- Imbalanced classes in multi-class classification problem
- Implement early stopping in tf.estimator.DNNRegressor using the available training hooks
- Implementation difference between TensorFlow Variable and TensorFlow Tensor
.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.