2

Having unbalanced data, how can I use ImageDataGenerator() to generate enough augmented data for shorter sample to balance all categories?

3
  • 2
    I don't think you can do that with ImageDataGenerator. There is simply no built-in option for that. However, you can use class_weights in ft method to somehow makeup for the contribution of low-count classes. Commented Apr 21, 2020 at 18:38
  • @today would you mind to provide a simple example. Commented Apr 21, 2020 at 20:00
  • This may be your solution. stackoverflow.com/questions/42586475/… Commented Feb 12, 2021 at 5:33

2 Answers 2

1

You can use the following code,

datagen = ImageDataGenerator( featurewise_center=True, featurewise_std_normalization=True, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True) 

This will not affect your dataset at all. It formats the image while feeding into the model.
You may refer the documentation, Image Preprocessing
Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to create a dictionary based on the weights of each class and then feed the model.fit_generator with it:

from sklearn.utils import class_weight import numpy as np class_weights = class_weight.compute_class_weight( 'balanced', np.unique(train_generator.classes), train_generator.classes) train_class_weights = dict(enumerate(class_weights)) model.fit_generator(..., class_weight=train_class_weights) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.