Tutorial 4:
Data Augmentation
Objectives:
• Understand the Need for data augmentation
• Understand Operations in data augmentation
• Understand Data augmentation in Keras
Data augmentation is an integral process in deep learning, as in deep
learning we need large amounts of data and in some cases, it is not
feasible to collect thousands or millions of images, so data augmentation
comes to the rescue. It helps us to increase the size of the dataset and
introduce variability in the dataset.
Step 1 — Importing Necessary Libraries
First, we need to import the required libraries.
Step 2 Define the Folder Where Augmented Images
Will Be Saved
Now, we define the directory where the augmented images will be saved. If the
folder doesn't exist, we'll create it.
Step 3: Initialize the ImageDataGenerator with
Augmentation Parameters
Here, we configure the ImageDataGenerator class with the
augmentation techniques we want to apply to the images.
Rotation: This rotates the image within a given range.
Shear: Shear introduces geometric transformations, skewing the image.
Zoom: It zooms in and out randomly on the image.
Flip: Flips the image horizontally.
Brightness: Randomly adjusts the brightness of the image.
Step 4: Load the Image and Convert It to a NumPy Array
We now load a sample image from a file and convert it into a NumPy array, which
is the format Keras works with.
Step 5: Reshape the Image for Batch Processing
ImageDataGenerator expects a 4D array: (batch_size, height, width, channels).
Since we are working with a single image, we add an extra dimension.
Step 6: Generate and Save Augmented Images
We now create a loop that generates augmented images. Each image will be saved
in the specified directory, and optionally, we will display the augmented images
using matplotlib.
Step 7: Confirm the Saved Location of Augmented Images
Finally, we print out the folder location where the augmented images have been
saved.
Task:
Take a single image in JPEG format from your computer.
40 augmented images saved in a folder (e.g., rotated, flipped, zoomed versions of
the original image)