Keras
image augmentation
flow method
deep learning
data preprocessing

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

When working with image data for deep learning tasks such as image classification or semantic segmentation, data augmentation is a crucial step. It not only helps improve the robustness of models but also reduces the chances of overfitting. Keras, a popular high-level neural-network API, provides several utilities for this purpose. While `flow_from_directory` is the more common method for augmenting images and loading them into a model during training, it is indeed possible to use the `flow()` method for this purpose. Below, we explore how you can leverage `flow()` to augment images and masks in Keras.

Technical Explanation and Examples

Understanding `flow()`

  • Purpose: The `flow()` method is used to generate batches of tensor image data with real-time data augmentation. The data is looped over (in batches).
  • Input:
    • Data: Unlike `flow_from_directory`, which requires a directory structure to extract labels, the `flow()` method uses image data directly in the form of Numpy arrays or Python lists.
    • Batch Size: Determines the number of samples per gradient update.
    • Shuffle: Specifies whether to shuffle the data or not.
  • Use Case: `flow()` is perfect for situations where images and their corresponding masks are best handled directly in memory (e.g., from Numpy arrays) rather than being loaded from disk in batches.

Implementing Image and Mask Augmentation

To augment paired image and mask data, we'll typically need to ensure that whatever transformation we perform on an image is identically performed on its corresponding mask. This is where custom data generators become essential.

Example Code

  • Memory Constraints: Since the `flow()` method operates on data already loaded into memory, it's crucial to consider the memory footprint if you're working with a large dataset.
  • Complex Augmentations: Some complex augmentations might require additional processing to ensure transformations are applied consistently across images and corresponding masks.
  • Preprocessing Consistency: Ensure any preprocessing (e.g., normalization) is consistent between training and validation/test datasets.

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