AttributeError:“DataFrameIterator”对象没有属性“类”

原文标题AttributeError: ‘DataFrameIterator’ object has no attribute ‘classes’

我的任务与多标签分类有关,数据不平衡。因此,我想使用class_weight.compute_class_weight来解决这个问题。但是,当使用它时,它显示

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

当前代码如下。

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

我在 Stackoverflow 和 Google 中搜索过,仍然没有找到(我的来源如下)。

  1. 谷歌:如何修复“DataFrameIterator”对象没有属性“num_classes”? , 但不相关
  2. Stackoverflow:如何将 flow_from_dataframe 与 compute_class_weight 一起使用? <<同样的问题,但还没有回答。

原文链接:https://stackoverflow.com//questions/71506835/attributeerror-dataframeiterator-object-has-no-attribute-classes

回复

我来回复
  • Setthawut Kulsrisuwan的头像
    Setthawut Kulsrisuwan 评论

    通过 2 个简单步骤找到的解决方案:

    1. 添加新代码,从 .classes 修改为 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. 将 class_weight=class_weights 添加到 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])
    2年前 0条评论