With the current version of Keras, it is not possible to balance your dataset using only the built-in Keras methods. flow_from_directory simply creates a list of all files and their classes, shuffling it (if necessary), and then iterating over it.
But you could do another trick - by writing your own generator that will do the balancing inside python :
def balanced_flow_from_directory(flow_from_directory, options): for x, y in flow_from_directory: yield custom_balance(x, y, options)
Here custom_balance should be a function that provides the package (x, y) , balances it and returns a balanced batch (x', y') . For most applications, the lot size does not have to be the same - but there are some weird use cases (e.g. stateful RNN) - where the lot sizes should have a fixed size).
source share