使用predict_generator时如何返回项目的真实标签? [英] How to return true labels of items when using predict_generator?

查看:75
本文介绍了使用predict_generator时如何返回项目的真实标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 predict_generator()函数观察神经网络的输出,但无法看到预测项的真实标签.如何实现一个块以查看输入项的真实标签?

I am observing the output of my neural network with predict_generator() function but I am unable to see true labels of the predicted items. How can I implement a block to see the true labels of the input items?

test_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=45,
width_shift_range=0.25,
height_shift_range=0.25,
horizontal_flip=True,
)
test_generator = test_datagen.flow_from_directory(
    evaluate_path,
    target_size=(width, height),
    batch_size=batch_size,
    class_mode='categorical')

model.compile(optimizer=SGD(lr=0.0001, momentum=0.9),      loss='categorical_crossentropy', metrics=['accuracy'])
x = model.predict_generator(test_generator, val_samples=1)
print(x)

推荐答案

尝试以下功能:

from six import next

def generator_with_true_classes(model, generator):
    while True:
        x, y = next(generator)
        yield x, model.predict(x), y

它将产生原始数据, y_pred y_true .通过以下方式使用它:

It will yield original data, y_pred and y_true. Use it in a following way:

nb_of_samples = 0
nb_of_samples_to_compute = 100 # set your own value
for x, y_pred, y_true in generator_with_true_classes(model, test_generator):
    # do something with data, eg. print it.
    nb_of_samples += 1
    if nb_of_samples == nb_of_samples_to_compute:
         break

这篇关于使用predict_generator时如何返回项目的真实标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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