'How to use augmentations with ImageDataGenerator().flow_from_dataframe?

Seems like there is no map method:

aug_ds = train_ds.map(
  lambda x, y: (resize_and_rescale(x, training=True), y))

AttributeError: 'DataFrameIterator' object has no attribute 'map'

Of course I can use preprocessing layers in the model, but I need custom functions for augmentations.



Solution 1:[1]

You can use the preprocessing_function parameter of the ImageDataGenerator to add additional augmentations. The function will run after the image is resized and augmented. However, note the warning in the docs:

Warning: tf.keras.preprocessing.image.ImageDataGenerator is not recommended for new code. Prefer loading images with tf.keras.utils.image_dataset_from_directory and transforming the output tf.data.Dataset with preprocessing layers. For more information, see the tutorials for loading images and augmenting images, as well as the preprocessing layer guide.

So, maybe just work with tf.keras.utils.image_dataset_from_directory.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AloneTogether