Using Keras ImageDataGenerator in a regression model
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Yes, ImageDataGenerator can be used for regression. The important difference from classification is that your target is a continuous value or vector, so you must feed raw numeric labels and choose augmentations that preserve the correctness of those labels.
The Core Rule for Regression
ImageDataGenerator does not care whether the target is a class index or a floating-point number. It simply yields image batches and matching target batches. That means regression works as long as:
- '
yis numeric' - the model output shape matches the target shape
- the augmentation does not invalidate the label
For example, if you are predicting house price from an image, horizontal flip may be harmless. If you are predicting steering angle, a horizontal flip changes the correct target and must be handled carefully.
Example with Numpy Arrays
If your images are already loaded into memory, you can use flow directly:
The key regression-specific details are the final Dense(1) layer and the mse loss.
Example with DataFrames
When images live on disk and labels are in a CSV file, flow_from_dataframe is a common choice. For regression, set class_mode="raw".
class_mode="raw" is the crucial setting. Without it, Keras will try to interpret the target as a class label.
The same idea extends to vector-valued targets. If your regression output is, for example, two coordinates or several continuous measurements, the generator can still work as long as those numeric targets are emitted in the correct shape for the model output layer.
Be Careful with Label-Preserving Augmentation
Regression makes augmentation trickier than classification because not every image transform leaves the target unchanged.
Examples:
- brightness prediction: flips and crops may still be valid
- keypoint coordinates: transforms require target-coordinate updates
- steering angle: horizontal flip usually requires changing the sign of the label
- age estimation from faces: mild geometric transforms may be fine
So the right augmentation policy depends on what the numeric target actually means.
Modern Note
ImageDataGenerator still works, but many newer TensorFlow pipelines use tf.data plus preprocessing layers for augmentation. That approach is often easier to customize when regression targets also need transformation.
Still, if your use case is simple and labels remain valid under the augmentations, ImageDataGenerator is perfectly usable.
Common Pitfalls
- Forgetting
class_mode="raw"when reading regression targets from a dataframe. - Using a classification-style output layer or loss function for a regression problem.
- Applying augmentations that change the meaning of the target without updating the label.
- Assuming any image augmentation that works for classification is safe for regression.
- Normalizing images correctly but leaving target scaling unmanaged when the target range is large.
Summary
- '
ImageDataGeneratorworks for regression as long as the targets are fed as raw numeric values.' - Use a regression output layer such as
Dense(1)and a loss likemseormae. - With
flow_from_dataframe,class_mode="raw"is usually the key setting. - Only use augmentations that preserve or correctly transform the target value.
- For more complex target-aware augmentation,
tf.datapipelines may be more flexible.

