keras flow_from_directory over or undersample a class
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
flow_from_directory is convenient for loading image batches, but it does not have a built-in switch to oversample one class or undersample another. If your dataset is imbalanced, the usual fixes are class weights, a custom generator, or a more modern tf.data pipeline where you control sampling explicitly. The best choice depends on whether you want to rebalance at training time or physically rebalance files on disk.
What flow_from_directory Does and Does Not Do
ImageDataGenerator.flow_from_directory(...) scans subdirectories, assigns class indices, and yields batches. It can shuffle and augment, but it does not provide a class-balancing policy such as “sample minority twice as often.”
That means you cannot ask it directly to oversample class cat or undersample class dog with one argument.
Easiest First Step: Use class_weight
If the goal is better training behavior rather than equal batch composition, try class weights first. This keeps the dataset unchanged but tells Keras to penalize mistakes on minority classes more heavily.
This is often good enough and much simpler than custom sampling.
Oversampling Requires a Custom Input Strategy
If you truly need more minority examples per epoch, you usually move beyond plain flow_from_directory. One practical approach is to build a list of file paths yourself, duplicate minority-class entries, and feed them through a custom Sequence or tf.data.Dataset.
This is a real oversampling strategy because minority file paths appear multiple times.
Undersampling Is Simpler but Loses Data
Undersampling means keeping fewer majority-class samples per epoch. That is easy to implement by trimming the larger class before creating the dataset. The tradeoff is obvious: you discard information.
Undersampling can still be useful when:
- the majority class is extremely large
- training speed matters more than maximum recall
- duplicate-like majority images add little information
Keep Validation and Test Data Unchanged
Whatever balancing strategy you choose for training, do not oversample or undersample the validation and test sets just to make the metrics look cleaner. Those splits should reflect the real-world distribution you care about. Otherwise you can end up with a model that looks balanced in training reports but performs poorly in production.
Prefer tf.data for Fine-Grained Control
Modern Keras works better with tf.data than with ImageDataGenerator for advanced sampling behavior. If the question is specifically about flow_from_directory, the honest answer is that it is not the right abstraction for class-aware over- or under-sampling.
You can still use directory-based loading logic, but you gain much more control once you build the dataset yourself.
Common Pitfalls
- Expecting
flow_from_directoryto perform class balancing automatically. - Oversampling by copying files on disk when dynamic sampling would be cleaner.
- Ignoring
class_weight, which may solve the problem with far less complexity. - Undersampling so aggressively that the model loses important majority-class variety.
- Evaluating on a rebalanced validation set instead of a realistic validation distribution.
Summary
- '
flow_from_directorydoes not directly support over- or under-sampling by class.' - '
class_weightis the simplest fix for many imbalance problems.' - Real oversampling usually requires a custom generator or
tf.datapipeline. - Undersampling is easy but throws away training data.
- For advanced balancing logic,
tf.datais usually the better long-term approach.
Related reading
- Keras flow_from_directory read only from selected sub-directories
- Keras flowFromDirectory get file names as they are being generated
- Keras GaussianNoise layer no effect?
- Keras get labels name of pre-trained models on imagenet
- Keras gives nan when training categorical LSTM sequence-to-sequence model
- Keras history not accessible for loss or accuracy
- Keras How come 'accuracy' is higher than 'val_acc'?
- Keras How is Accuracy Calculated for Multi-Label Classification?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.