Error when using ImageDataGenerator with custom preprocessing function in Keras

34 views Asked by At

I'm encountering an error while trying to use Keras's ImageDataGenerator with a custom preprocessing function. I'm using a custom preprocessing function to apply data augmentation to images before feeding them into my model. Here's a simplified version of my code:

# Define custom data augmentation function
def apply_augmentation(image_data, num_images=10):
    # Create a list to store the augmented images
    augmented_images = []

    # Add the original image to the list
    augmented_images.append(image_data)

    # Create an ImageDataGenerator object for data augmentation
    datagen = ImageDataGenerator(
        rotation_range=45,
        zoom_range=[0.7, 1.2],
        brightness_range=[0.5, 1]
    )

    # Apply augmentation to the image data
    for _ in range(num_images):
        # Add a batch dimension to image_data
        image_data_batch = np.expand_dims(image_data, axis=0)

        # Generate an augmented image
        it = datagen.flow(image_data_batch, batch_size=1)

        for _ in range(10):
            augmented_image = it.next()[0]
            augmented_images.append(augmented_image)

    return augmented_images

# Define custom preprocessing function
def preprocess_image(image, target_size=(224, 224)):
    # Resize the image to the required dimensions
    img = tf.image.resize(image, target_size)
    # Normalize the image pixels to scale them between [0, 1]
    img = tf.cast(img, tf.float32) / 255.0
    return img

def preprocess_and_augment(image):
    # Apply data augmentation
    augmented_images = apply_augmentation(image)
    for augmented_image in augmented_images:
        preprocessed_augmented_image = preprocess_image(augmented_image)
        yield preprocessed_augmented_image

data_augmented_generator = ImageDataGenerator(preprocessing_function=preprocess_and_augment)
train_augmented_generator = data_augmented_generator.flow_from_directory(
    '/path/to/train_data',
    target_size=(image_size, image_size),
    batch_size=BATCH_SIZE_TRAINING,
    shuffle=True,
    class_mode='categorical'
)

# Error occurs here
fit_history = model.fit(
    train_augmented_generator,
    epochs=NUM_EPOCHS,
    validation_data=validation_generator,
    callbacks=[cb_checkpointer, cb_early_stopper]
)

However, when I try to fit my model using the train_augmented_generator, I encounter the following error:

TypeError: float() argument must be a string or a real number, not 'generator'

I'm not sure why this error is occurring, as I've followed the guidelines for using custom preprocessing functions with ImageDataGenerator. Can someone please help me understand why this error is happening and how I can resolve it? Thank you in advance!

I expected that the ImageDataGenerator would apply the custom preprocessing function to each image in the training data generator before passing it to the model for training. However, I'm not sure why I'm getting this error related to floating-point numbers.

Can someone please help me understand why this error is happening and how I can resolve it? Thank you in advance!

0

There are 0 answers