Keras
image augmentation
flow
flow_from_directory
deep learning

Can I augment images and masks in Keras just using flow instead of flow_from_directory?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

In the realm of deep learning with Keras, data augmentation is a critical technique that enhances the robustness of models by artificially enlarging the training dataset. This is primarily achieved by transforming existing images through operations such as rotating, flipping, or cropping. When working especially with images and their associated masks, it becomes essential to ensure that both image and mask undergo the same transformations. This article explores the feasibility of augmenting images and corresponding masks using the `flow()` method in contrast to the more typical `flow_from_directory()` method, coupled with practical insights and code snippets.

Understanding `ImageDataGenerator`

The `ImageDataGenerator` class in Keras is an elegant utility designed for both real-time augmentation and loading of image data during model training. While `flow_from_directory()` is prevalent for seamlessly loading images from directories, `flow()` offers more direct control and is well-suited for cases where image data and corresponding masks reside in memory rather than filesystem directories.

Key `Parameters`

Both methods allow similar augmentation operations. Below are some crucial parameters that can be applied to image and mask augmentation:

  • Rotation Range: Degree range for random rotations.
  • Width/Height Shift Range: Fraction of total width/height for random horizontal/vertical shifts.
  • Shear Range: Shear intensity (in degrees).
  • Zoom Range: Range for random zoom.
  • Horizontal/Vertical Flip: Randomly flip inputs horizontally or vertically.

Using `flow()` for Image and Mask Augmentation

The `flow()` method is powerful when dealing with data in numpy array format. It is especially beneficial when image and mask datasets are closely coupled. Here's how you can augment them simultaneously:

Code Example

  • Random Seed: When using `flow()`, it's vital to synchronize the random transformations applied to images and their corresponding masks. This synchronization can be accomplished by using the same `seed` parameter in both image and mask `flow()` calls.
  • Batch Size: Ensure both image and mask generators use the same batch size to maintain the alignment.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design