'AttributeError: 'DataFrameIterator' object has no attribute 'classes'
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).
- Google: How to fix"DataFrameIterator' object has no attribute 'num_classes'"?, but not related
- Stackoverflow: How to use flow_from_dataframe with compute_class_weight? << same issue, but not answered yet.
Solution 1:[1]
A solution found with 2 easy steps:
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))Add
class_weight=class_weightstomodel.fithistory = 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])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
