In Keras, a generator is a way to load and preprocess data on the fly while training a model. Rather than loading all the data into memory at once, which can be memory-intensive and slow down training, a generator allows you to load and preprocess data in batches during the training process. This can be particularly useful when working with large datasets that cannot fit into memory at once.
A generator function in Keras should take no arguments and return a tuple (input, target). The input and target can be Numpy arrays or a list of Numpy arrays. The length of the input and target array should be the same, and it corresponds to the number of samples in a given batch.
Here’s an example of a simple generator that loads data from a directory of image files and applies some basic preprocessing:
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
def custom_generator(directory, batch_size):
data_gen = ImageDataGenerator(rescale=1./255)
data_flow = data_gen.flow_from_directory(directory, target_size=(224, 224),
batch_size=batch_size, class_mode='binary')
while True:
input_batch, target_batch = data_flow.next()
yield input_batch, target_batch
In this case, the ‘ImageDataGenerator‘ class applies rescaling to the pixel values of the images. The ‘flow_from_directory‘ method of this class generates batches of preprocessed images from a given directory of image files. The generator function creates an infinite loop using the ‘while True:‘ statement, and yields an input batch and a target batch at each iteration using the ‘next()‘ method of the ‘data_flow‘ generator object.
To use this generator in training a Keras model, you simply pass the ‘custom_generator‘ function to the ‘fit_generator‘ method of your model instance. For example:
model.fit_generator(custom_generator('data/train', batch_size=32),
steps_per_epoch=1000, epochs=10)
Here, ‘steps_per_epoch‘ is the number of batches to yield at each epoch of model training. When using a custom generator, the number of steps per epoch should be set to the number of training samples divided by the batch size.
Overall, using a generator in Keras is a powerful tool for loading and processing data on the fly, and it can be useful for working with large datasets that cannot fit into memory at once. Creating a custom generator is straightforward once you understand the basic structure, and it allows you to customize the preprocessing steps applied to your data before feeding it to a model.