Keras ImageDataGenerator deprecated: replacement (tested)
In Keras 3, from keras.preprocessing.image import ImageDataGenerator raises ImportError, while tensorflow.keras.preprocessing.image.ImageDataGenerator still works as a legacy class. The replacement is keras.utils.image_dataset_from_directory plus Rescaling/RandomFlip layers. Both are shown running below, with a mapping table for each argument.
1. What still works in Keras 3
data/ holds 20 PNGs in two class folders (cats/, dogs/). b1.py tries both import paths:
import warnings
warnings.simplefilter("always")
try:
from keras.preprocessing.image import ImageDataGenerator
except ImportError as e:
print("keras 3:", str(e).split(" (")[0])
from tensorflow.keras.preprocessing.image import ImageDataGenerator
gen = ImageDataGenerator(rescale=1 / 255, validation_split=0.2, horizontal_flip=True)
train = gen.flow_from_directory("data", target_size=(64, 48), batch_size=8, subset="training")
x, y = next(train)
print(type(train).__module__)
print(x.shape, y.shape, float(x.max()) <= 1.0)
keras 3: cannot import name 'ImageDataGenerator' from 'keras.preprocessing.image'
Found 16 images belonging to 2 classes.
keras.src.legacy.preprocessing.image
(8, 64, 48, 3) (8, 2) True
With standalone Keras 3, keras.preprocessing.image.ImageDataGenerator is gone (ImportError). The tensorflow.keras path still imports and flow_from_directory still works, with no warning printed, but the class now lives in keras.src.legacy: it is kept for old code and gets no new features. Keras documents it as deprecated in favour of tf.data pipelines.
2. Replacement: image_dataset_from_directory + preprocessing layers
import keras
from keras import layers
train, val = keras.utils.image_dataset_from_directory(
"data", image_size=(64, 48), batch_size=8,
validation_split=0.2, subset="both", seed=1,
label_mode="categorical", # one-hot, like class_mode="categorical"
)
print(train.class_names)
augment = keras.Sequential([layers.Rescaling(1 / 255), layers.RandomFlip("horizontal")])
train = train.map(lambda x, y: (augment(x, training=True), y))
val = val.map(lambda x, y: (x / 255, y))
x, y = next(iter(train))
print(x.shape, y.shape, float(x.numpy().max()) <= 1.0)
Found 20 files belonging to 2 classes.
Using 16 files for training.
Using 4 files for validation.
['cats', 'dogs']
(8, 64, 48, 3) (8, 2) True
| ImageDataGenerator | Keras 3 replacement |
|---|---|
flow_from_directory("data", target_size=...) | keras.utils.image_dataset_from_directory("data", image_size=...) |
validation_split=0.2 + subset="training"/"validation" | validation_split=0.2, subset="both", seed=1 (returns both datasets; the seed keeps the split consistent) |
class_mode="categorical" / "binary" / "sparse" | label_mode="categorical" / "binary" / "int" (default "int") |
rescale=1/255 | layers.Rescaling(1/255) |
horizontal_flip=True, rotation_range, zoom_range | layers.RandomFlip("horizontal"), layers.RandomRotation, layers.RandomZoom |
Note the class order: both APIs sort folder names alphabetically (['cats', 'dogs'] above). Apply augmentation only to the training set, as in b2.py; you can also put the augmentation layers at the start of the model, where they are active only during fit.
Related: TensorFlow Keras errors after Keras 3, TensorFlow ResourceExhaustedError: OOM.
Frequently asked questions
Is ImageDataGenerator deprecated?
Yes. In Keras 3 it only exists as a legacy class under tensorflow.keras.preprocessing.image; keras.preprocessing.image no longer has it.
What replaces flow_from_directory?
keras.utils.image_dataset_from_directory, which returns a tf.data.Dataset of (images, labels) batches.
How do I do data augmentation without ImageDataGenerator?
Use preprocessing layers such as Rescaling, RandomFlip, RandomRotation and RandomZoom, mapped over the training dataset or placed at the start of the model.
Written with AI assistance. Measured on 2026-10-24 with tensorflow-cpu 2.21.0, keras 3.15.1, Pillow 13.0.0 and Python 3.13.5 on Linux. The scripts ran verbatim and the output was pasted unedited (TensorFlow startup log lines filtered). PlainTables is our extension; Keras and TensorFlow are free.