为keras预测获取真实标签 [英] Getting true labels for keras predictions

查看:77
本文介绍了为keras预测获取真实标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于图像分类的标准CNN,使用以下生成器来获取数据集:

  generator = validate_image_generator.flow_from_directory(batch_size = BATCH_SIZE,directory = val_dir,shuffle = False,target_size =(100,100),class_mode ='categorical') 

我可以轻松地通过以下方式获得预测标签:

  predictions = model.predict(数据集) 

现在,我想获取所有预测的(原始)真实标签图像,以与预测相同的顺序进行比较.我确信信息可以很容易地存储在某个地方,但是我找不到它.

解决方案

您必须从datagenerator获取图像并将其提供给model.predict.如果image_gen是您的ImageDataGenerator,那么您可以使用:

  X,y = image_gen.next()预测= model.predict(X) 

现在X是您的图像(例如,批处理X [0]是第一个图像,X [1]是第二个图像,依此类推),y是它们的相应标签,而预测是您为每个图像输出的模型./p>

这将提供来自ImageDataGenerator的批处理,并显示X,y和预测.要在整个时期内运行它,必须使用for循环:

 用于范围内的步进(step_per_epoch):X,y = image_gen.next()预测= model.predict(X) 

其中step_per_epoch应该为dataset_size/batch_size.

,但请记住ImageDataGenerators是随机工作的.因此,如果您有100张图像且批处理大小为10,则如果从ImageDataGenerator提取10个批处理,则可能会看到两次图像,而不会看到其他图像.

I have a standard CNN for image classification, using the following generator to get the dataset:

generator = validation_image_generator.flow_from_directory(batch_size=BATCH_SIZE,
                                                           directory=val_dir,
                                                           shuffle=False,
                                                           target_size=(100,100),
                                                           class_mode='categorical')

I can easily get the predicted labels with:

predictions = model.predict(dataset)

Now I want to get the (original) true labels and images for all the predictions, in the same order as the predictions in order to compare them. I am sure that information is easily stored somewhere, but I haven't been able to find it.

解决方案

you have to get images from datagenerator and give them to model.predict. if image_gen is your ImageDataGenerator so you can use:

X,y = image_gen.next()
prediction = model.predict(X)

now X is your images (in batch for example X[0] is first image, X[1] is the second image and so on), y is their corresponding labels and prediction is your models output for each image.

this will give a batch from ImageDataGenerator and shows X, y and prediction. to run this for a whole epoch, you have to use a for loop:

for step in range(step_per_epoch):
    X, y = image_gen.next()
    prediction = model.predict(X)

where step_per_epoch should be dataset_size/batch_size.

but remember ImageDataGenerators work randomly. so if you have 100 images and your batch size is 10, if you take 10 batches from your ImageDataGenerator, you may see some images twice and you wont see some other images.

这篇关于为keras预测获取真实标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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