[英]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_report
, ytrue
和ypred
应该是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.