简体   繁体   English

使用 keras ImageDataGenerator - AttributeError: 'DirectoryIterator' object has no attribute 'argmax' 时如何获取分类报告(F1 分数)

[英]How to get classification report (F1 score) when using keras ImageDataGenerator - AttributeError: 'DirectoryIterator' object has no attribute 'argmax'

Since I do not have the training set and label separated when using Keras ImageDataGenerator but rather I rely on the folder structure.由于在使用 Keras ImageDataGenerator 时我没有训练集和 label 分离,而是我依赖于文件夹结构。 How can I get the classification report, ie how can I get/calculate the Precision, Recall, and F1?如何获取分类报告,即如何获取/计算 Precision、Recall 和 F1?

This is what I have tried and I get the error that is complaining that the train_generator has not argmax.这是我尝试过的,我得到了抱怨 train_generator 没有 argmax 的错误。 How can I solve this?我该如何解决这个问题?

AttributeError: 'DirectoryIterator' object has no attribute 'argmax' AttributeError: 'DirectoryIterator' object 没有属性 'argmax'

train_datagen = ImageDataGenerator(
    rescale=1. / 255,
)

train_generator = train_datagen.flow_from_directory(
    train_dir, 
    target_size=(150, 150),  
)

predIdxs = model.predict(train_generator)

predIdxs = np.argmax(predIdxs, axis=1)

print(classification_report(train_generator.argmax(axis=1), predIdxs,
                            target_names=["class 1", "class 2"]))

This solution worked for me since if you use flow_from_directory then you must use predict_generator method on model and speaking about sklearn.metrics.classification_report , ytrue and ypred should be 1-d numpy array hence used train_generator.labels这个解决方案对我有用,因为如果你使用flow_from_directory那么你必须在 model 上使用predict_generator方法,并且谈到sklearn.metrics.classification_reportytrueypred应该是1-d numpy数组,因此使用train_generator.labels

train_datagen = ImageDataGenerator(
    rescale=1. / 255,
)

train_generator = train_datagen.flow_from_directory(
    train_dir, 
    target_size=(150, 150), shuffle = False,   # Must
)

predIdxs = model.predict_generator(train_generator)

predIdxs = np.argmax(predIdxs, axis=1)

print(classification_report(train_generator.labels, predIdxs,
                            target_names=["class 1", "class 2"]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Keras ImageDataGenerator:AttributeError:'super'对象没有属性'init' - Python Keras ImageDataGenerator: AttributeError: 'super' object has no attribute 'init' 如何提高分类的 F1 分数 - How to improve F1 score for classification F1 分数指标和分类报告的 F1 分数值不同 sklearn - F1 score values different for F1 score metric and classification report sklearn 计算多 label 分类 keras 的召回精度和 F1 分数 - compute the recall precision and F1 score for a multi label classification keras 如何在 Keras 模型中使用 F1 Score? - How to use F1 Score with Keras model? 错误-AttributeError:'DirectoryIterator'对象在keras中的自动编码器设计中没有属性'ndim - Error- AttributeError: 'DirectoryIterator' object has no attribute 'ndim in autoencoder design in keras AttributeError: 模块'keras.utils' 没有使用classification_models.keras 的属性'get_file' - AttributeError: module 'keras.utils' has no attribute 'get_file' using classification_models.keras 如何使用 Sklearn 的 cross_validation(多标签分类)获得每个标签的 F1 分数 - How to get F1 score per label using Sklearn's cross_validation (multi-label classification) AttributeError: 'ImageDataGenerator' 对象没有属性 'shape' - AttributeError: 'ImageDataGenerator' object has no attribute 'shape' 为什么 sklearns 分类报告中的“加权”平均 F1 分数与根据公式计算的 F1 分数不同? - Why is the 'weighted' average F1 score from sklearns classification report different from the F1 score calculated from the formula?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM