Keras:从ImageDataGenerator或predict_generator获取True标签(y_test) [英] Keras: Get True labels (y_test) from ImageDataGenerator or predict_generator

查看:665
本文介绍了Keras:从ImageDataGenerator或predict_generator获取True标签(y_test)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ImageDataGenerator().flow_from_directory(...)从目录中生成一批数据.

I am using ImageDataGenerator().flow_from_directory(...) to generate batches of data from directories.

在模型成功构建后,我想获得两列True和Predicted类标签的数组.使用model.predict_generator(validation_generator, steps=NUM_STEPS),我可以得到一个预测类的numpy数组. predict_generator是否可以输出相应的True类标签?

After the model builds successfully I'd like to get a two column array of True and Predicted class labels. With model.predict_generator(validation_generator, steps=NUM_STEPS) I can get a numpy array of predicted classes. Is it possible to have the predict_generator output the corresponding True class labels?

要添加的内容:validation_generator.classes确实会打印True标签,但按照从目录中检索标签的顺序,它不会考虑通过扩增进行的批处理或样品扩展.

To add: validation_generator.classes does print the True labels but in the order that they are retrieved from the directory, it doesn't take into account the batching or sample expansion by augmentation.

推荐答案

您可以通过以下方式获取预测标签:

You can get the prediction labels by:

 y_pred = numpy.rint(predictions)

您可以通过以下方式获得真实的标签:

and you can get the true labels by:

y_true = validation_generator.classes

在此之前,您应该在验证生成器中设置shuffle=False.

You should set shuffle=False in the validation generator before this.

最后,您可以通过以下方式打印混淆矩阵

Finally, you can print confusion matrix by

print confusion_matrix(y_true, y_pred)

这篇关于Keras:从ImageDataGenerator或predict_generator获取True标签(y_test)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆