ImageDataGenerator
data augmentation
dataset expansion
image preprocessing
machine learning

Does ImageDataGenerator add more images to my dataset?

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

Understanding whether ImageDataGenerator adds more images to your dataset requires diving into the mechanics of data augmentation in machine learning. ImageDataGenerator is a powerful utility provided by Keras, part of the TensorFlow library, to perform real-time data augmentation on image datasets. Real-time data augmentation allows on-the-fly transformations of images to increase the diversity of the dataset during training without actually increasing the number of images on disk.

What is ImageDataGenerator?

ImageDataGenerator is a class in Keras that allows you to specify a number of transformations that will be randomly applied to the images during training, effectively augmenting the dataset. It is used to artificially expand the variety of images in the training dataset, which helps in preventing overfitting and improves the model generalization capabilities.

Key Transformations

The ImageDataGenerator can perform multiple transformations such as:

  1. Rotation: Randomly rotating the image by a given degree.
  2. Width Shift: Horizontally shifting the image.
  3. Height Shift: Vertically shifting the image.
  4. Shear: Applying random geometrical transformation along the axes.
  5. Zoom: Randomly zooming inside digital images.
  6. Horizontal Flip: Flipping the image horizontally.
  7. Vertical Flip: Flipping the image vertically.
  8. Brightness Alteration: Adjusting the brightness of the image.

Example of how to initialize the ImageDataGenerator with these transformations:

python
1from tensorflow.keras.preprocessing.image import ImageDataGenerator
2
3datagen = ImageDataGenerator(
4    rotation_range=40,
5    width_shift_range=0.2,
6    height_shift_range=0.2,
7    shear_range=0.2,
8    zoom_range=0.2,
9    horizontal_flip=True,
10    fill_mode='nearest'
11)

Does ImageDataGenerator Add More Images?

Technically, ImageDataGenerator itself does not create new image files on disk, nor does it increase the number of stored images. Instead, it generates augmented images on the fly during the training process. The underlying dataset remains unchanged on disk. These dynamically augmented images are temporarily generated in memory during each iteration of training. Hence, while the number of image files on disk stays the same, the effective diversity of the data during training increases.

Real-Life Example

Consider you have a dataset with only 1000 images. Implementing a data augmentation strategy using ImageDataGenerator could effectively increase the training set by a factor defined by the number of augmentations you apply:

python
1train_generator = datagen.flow_from_directory(
2    'train_data/',
3    target_size=(150, 150),
4    batch_size=32,
5    class_mode='binary'
6)

With the specified augmentations, each mini-batch that is fed to the model during training will be different, giving the illusion of a larger dataset.

Advantages of Using ImageDataGenerator

  1. Prevention of Overfitting: By increasing the diversity of data, models are less likely to memorize the training data.
  2. Better Model Generalization: Experiencing variations in images helps in better handling instances of input data during real-world applications.
  3. Efficient Memory Usage: Augmentation is done in memory, requiring no additional storage.

Limitations

  1. Training Time: Augmentation may increase the computational overhead and thus increase training time.
  2. Quality Control: Poorly chosen transformations could introduce noise and artifacts into the data.
  3. Limited to Training: The augmentation is live only during training and not available for validation or test set evaluations directly unless implemented similarly.

Summary Table

AspectDetails
Storage ImplicationsDoes not increase images on disk
Data Variety ImprovementIncreases diversity in training
Computational OverheadYes, increased due to real-time transformations
Real-Time AugmentationAlterations are applied per batch memory
Key TransformationsRotation, Shift, Shear, Zoom, Flips, etc.
Use Case SuitabilityTraining phase primarily

ImageDataGenerator is a potent tool in a data scientist's arsenal for combating overfitting and enhancing the robustness of computer vision models, offering a virtual increase in dataset size through real-time augmentation techniques.


Related reading
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
Track 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.

Practice ML system design