How to use ImageDataGenerator with class_weights argument in fit method?

I am working on a multi-class classification problem with an imbalanced dataset.

I am using ImageDataGenerator.flow_from_dataframe method. Which requires class labels to in str, list or tuple format while the class_weight argument in fit method requires a dictionary with keys in int format.

How can I implement this?

Hi @Meghansh_Bansal, First you have to calculate the class weights based on the imbalanced data. For example,

from sklearn.utils import class_weight
class_weights = class_weight.compute_class_weight('balanced', np.unique(y_train), y_train)

Then you can pass calculated class_weights to class_weight argument. For more details please refer to this document. Thank You.

1 Like