9951 explained code solutions for 126 technologies


python-kerasHow can I use Python and Keras to perform data augmentation?


Data augmentation is the process of artificially increasing the size of a dataset by creating modified versions of existing data. It is often used in machine learning to help improve the accuracy of models. Python and Keras can be used together to perform data augmentation.

The following example code shows how to use the ImageDataGenerator class in Keras to perform data augmentation on a set of images. The code will generate new images by randomly rotating, shifting, shearing, zooming, and flipping the original images.

from keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest'
)

The code consists of the following parts:

  1. Importing the ImageDataGenerator class from keras.preprocessing.image.
  2. Instantiating an ImageDataGenerator object with the desired parameters. In this example, the parameters used are rotation_range, width_shift_range, height_shift_range, shear_range, zoom_range, horizontal_flip, and fill_mode.

Once the ImageDataGenerator object is created, it can be used to generate new images from existing images by calling the .flow() or .flow_from_directory() methods.

For more information, see the Keras documentation on data augmentation.

Edit this code on GitHub