0

My task is related to multi label classification, and data is imbalance. Therefore, I would like use class_weight.compute_class_weight to address this issues. However, when use it, it shows

AttributeError: 'DataFrameIterator' object has no attribute 'classes' 

the current code is below.

test_generator=test_datagen.flow_from_dataframe(
dataframe=test,
directory="/content/hackathon",
x_col="filename",
batch_size=1,
seed=42,
shuffle=False,
class_mode=None,
target_size=(256,256))

#Class imbalance
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)
class_weights

I have searched in Stackoverflow and Google, and still not found yet (my source as follows).

  1. Google: How to fix"DataFrameIterator' object has no attribute 'num_classes'"?, but not related
  2. Stackoverflow: How to use flow_from_dataframe with compute_class_weight? << same issue, but not answered yet.

1 Answer 1

0

A solution found with 2 easy steps:

  1. Add new codes with modifying from .classes to train['labels']

    from sklearn.utils import class_weight  
    class_weights = class_weight.compute_class_weight(class_weight ='balanced',
                                               classes =   np.unique(train['labels']),
                                               y = train['labels']) 
    class_weights = dict(enumerate(class_weights)) 
    
  2. Add class_weight=class_weights to model.fit

    history = model.fit(train_generator, 
            epochs=epochs,
            validation_data=valid_generator,
            class_weight=class_weights,
            max_queue_size=100,
            callbacks=[ckpt_saver, early_stopping, reduce_lr, csv_logger])
    

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.